Browse Source

zdf_mediathek: add error handling for http errors and socket timeouts

Helmut Pozimski 5 years ago
parent
commit
72eaf169a1
1 changed files with 27 additions and 18 deletions
  1. 27 18
      lib_stov/zdf_mediathek.py

+ 27 - 18
lib_stov/zdf_mediathek.py

@@ -21,6 +21,8 @@
 import json
 import logging
 import urllib.request
+import urllib.error
+import socket
 from datetime import datetime, timedelta
 
 from lib_stov import stov_exceptions
@@ -75,26 +77,33 @@ class Connector(object):
         new_videos = []
         today = datetime.today()
         for i in range(7, -1, -1):
-            connection = urllib.request.urlopen(
-                "https://zdf-cdn.live.cellular.de/mediathekV2/broadcast-"
-                "missed/%s"
-                % (today-timedelta(days=i)).strftime("%Y-%m-%d"))
-            data = connection.read().decode("utf-8")
-            response = json.loads(data)
-            for cluster in response["broadcastCluster"]:
-                for broadcast in cluster["teaser"]:
-                    try:
-                        name_found = self._name in broadcast["brandTitle"]
-                    except KeyError:
-                        name_found = self._name in broadcast["headline"]
-                    if name_found:
-                        if self._search:
-                            if self._search in broadcast["titel"]:
+            day = (today - timedelta(days=i)).strftime("%Y-%m-%d")
+            try:
+                connection = urllib.request.urlopen(
+                    "https://zdf-cdn.live.cellular.de/mediathekV2/broadcast-"
+                    "missed/%s" % day, timeout=10)
+            except urllib.error.HTTPError:
+                LOGGER.error("Error while fetching ZDF data for %s", day)
+            except socket.timeout:
+                LOGGER.error("Connection timed out while fetching ZDF data "
+                             "for %s", day)
+            else:
+                data = connection.read().decode("utf-8")
+                response = json.loads(data)
+                for cluster in response["broadcastCluster"]:
+                    for broadcast in cluster["teaser"]:
+                        try:
+                            name_found = self._name in broadcast["brandTitle"]
+                        except KeyError:
+                            name_found = self._name in broadcast["headline"]
+                        if name_found:
+                            if self._search:
+                                if self._search in broadcast["titel"]:
+                                    new_videos.append((broadcast["sharingUrl"],
+                                                       broadcast["titel"]))
+                            else:
                                 new_videos.append((broadcast["sharingUrl"],
                                                    broadcast["titel"]))
-                        else:
-                            new_videos.append((broadcast["sharingUrl"],
-                                               broadcast["titel"]))
         if new_videos:
             for broadcast in new_videos:
                 video_exists = False