Remove delay after master fetch, handle connection resets master
authorJakob Cornell <jakob+gpg@jcornell.net>
Wed, 7 Oct 2020 15:37:00 +0000 (10:37 -0500)
committerJakob Cornell <jakob+gpg@jcornell.net>
Wed, 7 Oct 2020 15:37:00 +0000 (10:37 -0500)
hls_watch/__main__.py

index 638eea9aa6a75bb39fbfce4105bad641ee88e97a..2567747d480b15a67759915a0fd6802c60f410fd 100644 (file)
@@ -7,6 +7,8 @@ from hls_watch.playlist import parse_master, parse_media
 
 _DELAY = datetime.timedelta(seconds = 10)
 
+IGNORE_EXCEPTIONS = {ConnectionResetError}
+
 
 def _get_time(resp):
        time_str = resp.info().get('Date')
@@ -88,29 +90,38 @@ if __name__ == '__main__':
                                or stream_spec.variant is None and not handler.in_sess
                        )
                        if locate_stream:
-                               with opener.open(master_url) as resp:
-                                       if resp.status == 200:
-                                               lines = resp.read().decode('utf-8').splitlines()
-                                               streams = parse_master(lines)
-                                               if streams:
-                                                       winner = max(streams, key = lambda s: s.attrs['RESOLUTION'].vertical)
-                                                       (ep, curr_variant) = re.match(r'(.*)_(.*?)\.m3u8$', winner.uri).groups()
-                                                       assert ep == stream_spec.endpoint
-                                       else:
-                                               assert resp.status == 404
-                               time.sleep(_DELAY.total_seconds())
+                               try:
+                                       resp = opener.open(master_url)
+                               except tuple(IGNORE_EXCEPTIONS):
+                                       pass
+                               else:
+                                       with resp:
+                                               if resp.status == 200:
+                                                       lines = resp.read().decode('utf-8').splitlines()
+                                                       streams = parse_master(lines)
+                                                       if streams:
+                                                               winner = max(streams, key = lambda s: s.attrs['RESOLUTION'].vertical)
+                                                               (ep, curr_variant) = re.match(r'(.*)_(.*?)\.m3u8$', winner.uri).groups()
+                                                               assert ep == stream_spec.endpoint
+                                               else:
+                                                       assert resp.status == 404
 
                        if curr_variant is not None:
                                writer.stream = stream_spec._replace(variant = curr_variant)
                                media_url = url_for_stream('{}.m3u8'.format(writer.stream.full()))
-                               with opener.open(media_url) as resp:
-                                       if resp.status == 404:
-                                               handler.on_404()
-                                       else:
-                                               assert resp.status == 200
-                                               lines = resp.read().decode('utf-8').splitlines()
-                                               handler.update(Capture(
-                                                       time = _get_time(resp),
-                                                       playlist = parse_media(lines),
-                                               ))
+                               try:
+                                       resp = opener.open(media_url)
+                               except tuple(IGNORE_EXCEPTIONS):
+                                       pass
+                               else:
+                                       with resp:
+                                               if resp.status == 404:
+                                                       handler.on_404()
+                                               else:
+                                                       assert resp.status == 200
+                                                       lines = resp.read().decode('utf-8').splitlines()
+                                                       handler.update(Capture(
+                                                               time = _get_time(resp),
+                                                               playlist = parse_media(lines),
+                                                       ))
                                time.sleep(_DELAY.total_seconds())