From: Jakob Cornell Date: Sat, 4 Jan 2020 04:47:49 +0000 (-0600) Subject: Move date parsing to __main__ X-Git-Url: https://jcornell.net/gitweb/gitweb.cgi?a=commitdiff_plain;h=2bbb5536d15bd47182607a76fdc1f23b214a2b2c;p=hls-watch.git Move date parsing to __main__ --- diff --git a/hls_watch/__init__.py b/hls_watch/__init__.py index 114656e..3b86061 100644 --- a/hls_watch/__init__.py +++ b/hls_watch/__init__.py @@ -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): diff --git a/hls_watch/__main__.py b/hls_watch/__main__.py index 25c4e97..fd24ad8 100644 --- a/hls_watch/__main__.py +++ b/hls_watch/__main__.py @@ -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())