Move date parsing to __main__
authorJakob Cornell <jakob@jcornell.net>
Sat, 4 Jan 2020 04:47:49 +0000 (22:47 -0600)
committerJakob Cornell <jakob@jcornell.net>
Sat, 4 Jan 2020 04:47:49 +0000 (22:47 -0600)
hls_watch/__init__.py
hls_watch/__main__.py

index 114656ec0d9e9b16c5c09647fd846a9a6604b449..3b86061c84980fe2d3c567cad183ad6d414fdb5d 100644 (file)
@@ -116,16 +116,6 @@ def parse(resp):
                r'#(?!EXT)', # comment
        }
 
-       time_str = resp.info().get('Date')
-       time = None
-       if time_str:
-               try:
-                       time = datetime.datetime.strptime(time_str, '%a, %d %b %Y %H:%M:%S %Z')
-               except ValueError:
-                       pass
-               else:
-                       time = time.replace(tzinfo = datetime.timezone.utc)
-
        def gen_parsed(lines):
                lines = iter(lines)
                while True:
@@ -172,8 +162,7 @@ def parse(resp):
 
        body = resp.read().decode('utf-8')
        parsed = list(gen_parsed(body.splitlines()))
-       playlist = Playlist.from_entries(parsed)
-       return Capture(time, playlist)
+       return Playlist.from_entries(parsed)
 
 def _unparse(entry):
        if isinstance(entry, Segment):
index 25c4e97dac08dc00f9c58995b845d0d6cc004f79..fd24ad8108e84e39fde25065dde9d12063be08d9 100644 (file)
@@ -1,9 +1,25 @@
+import datetime
+
 from hls_watch import *
 
 
 _DELAY = datetime.timedelta(seconds = 10)
 
 
+def get_time(resp):
+       time_str = resp.info().get('Date')
+       time = None
+       if time_str:
+               print(time_str)
+               try:
+                       time = datetime.datetime.strptime(time_str, '%a, %d %b %Y %H:%M:%S %Z')
+               except ValueError:
+                       pass
+               else:
+                       return time.replace(tzinfo = datetime.timezone.utc)
+       return None
+
+
 if __name__ == '__main__':
        import argparse
        import contextlib
@@ -45,5 +61,8 @@ if __name__ == '__main__':
                                        handler.on_404()
                                else:
                                        assert resp.status == 200
-                                       handler.update(parse(resp))
+                                       handler.update(Capture(
+                                               time = get_time(resp),
+                                               playlist = parse(resp),
+                                       ))
                        time.sleep(_DELAY.total_seconds())