Implement multi-stream endpoints
authorJakob Cornell <jakob+gpg@jcornell.net>
Fri, 17 Apr 2020 02:53:49 +0000 (21:53 -0500)
committerJakob Cornell <jakob+gpg@jcornell.net>
Fri, 17 Apr 2020 02:53:49 +0000 (21:53 -0500)
hls_watch/__main__.py

index f95f218887b10c65c51247d9aa5fbe153b559ee6..b0e1ec6bfbb840e0716db30da6e8bca6008db73a 100644 (file)
@@ -20,12 +20,12 @@ def get_time(resp):
 
 
 if __name__ == '__main__':
+       from urllib.parse import urlparse, urlunparse
+       from pathlib import Path, PurePosixPath
        import argparse
        import contextlib
        import os
-       import pathlib
        import time
-       import urllib.parse
        import urllib.request
 
        import toml
@@ -36,20 +36,25 @@ if __name__ == '__main__':
 
        args = ap.parse_args()
 
-       base_path = pathlib.Path(args.base_path)
+       base_path = Path(args.base_path)
        assert base_path.is_dir()
 
        cfg_path = (
-               pathlib.Path(
-                       os.getenv('XDG_CONFIG_HOME', os.getenv('HOME') + '/.config')
-               )
+               Path(os.getenv('XDG_CONFIG_HOME', os.getenv('HOME') + '/.config'))
                .joinpath('oberlin_webcast2')
        )
        with cfg_path.joinpath('config.toml').open() as f:
                config = toml.load(f)
 
-       endpoint = args.endpoint
-       url = config['endpoints'][endpoint]
+       try:
+               (endpoint, s_num) = args.endpoint.rsplit('_', 1)
+       except:
+               raise ValueError("The endpoint arg doesn't look correct")
+       base_url = urlparse(config['endpoints'][endpoint])
+       path_seg = '{}_{}.m3u8'.format(endpoint, s_num)
+       url = urlunparse(base_url._replace(
+               path = str(PurePosixPath(base_url.path).joinpath(path_seg)),
+       ))
 
        # Follow redirects but don't throw on 4XX
        opener = urllib.request.OpenerDirector()