diff --git a/src/Utils/__init__.py b/src/Utils/__init__.py index 48720e8..a61d8b6 100644 --- a/src/Utils/__init__.py +++ b/src/Utils/__init__.py @@ -22,7 +22,8 @@ BASEPATH = path.join(str(environ.get("HOME")), '.config/cast') DB_FILE_NAME = 'cast.db' SUB_TABLE = 'subscriptions' VIDEO_TABLE = 'videos' -USE_CACHED_CATEGORIES = False +CAT_CACHE = None +CHAN_CACHE = None def add_video(video_id: str, @@ -77,27 +78,42 @@ def get_svt_thumb_from_id_changed(id: str, size, id, changed) -def get_all_svt_categories(basepath=BASEPATH) -> list: - global USE_CACHED_CATEGORIES - categoryfile = path.join(basepath,"categories.json") - if USE_CACHED_CATEGORIES: - if path.isfile(categoryfile): - with open(categoryfile, 'r') as jfile: - return(json.loads(jfile.read())) - +def get_all_svt_categories() -> list: + global CAT_CACHE + if CAT_CACHE: + categories = CAT_CACHE + else: + categories: list = list() + url = "https://www.svtplay.se/kategori" + data = get_svt_data(url) + for entry in data: + if 'genres' in entry.keys(): + categories = entry['genres'] + break + CAT_CACHE = categories + return categories - categories: list = list() - url = "https://www.svtplay.se/kategori" +def get_all_svt_channels() -> dict: + url = "https://www.svtplay.se/kanaler" + result: dict = dict() data = get_svt_data(url) for entry in data: - if 'genres' in entry.keys(): - categories = entry['genres'] - break - with open(categoryfile, 'w') as jfile: - jfile.write(json.dumps(categories)) - USE_CACHED_CATEGORIES = True - return categories + if "channels" in entry: + for channel in entry["channels"]: + if type(entry["channels"][channel]) == type(list()): + for item in entry["channels"][channel]: + if item["__typename"] == "Channel" and "running" in item: + result[item["id"]] = { + "thumbnail": + make_bitmap_from_url( + get_svt_thumb_from_id_changed( + item["running"]["image"]['id'], + item["running"]["image"]['changed'])), + "name": + item["name"] + } + return result def get_svt_category(category: str) -> list: @@ -118,8 +134,8 @@ def get_svt_data(url: str) -> list: result: list = list() res = requests.get(url) soup = BeautifulSoup(res.text, features="lxml") - data = json.loads( - soup.find(id="__NEXT_DATA__").string)["props"]["urqlState"] + data = json.loads(soup.find( + id="__NEXT_DATA__").string)["props"]["urqlState"] # type: ignore for key in data.keys(): result.append(json.loads(data[key]["data"])) return result @@ -232,9 +248,9 @@ def get_svt_thumbnail(link: str) -> str: page = requests.get(link) soup = BeautifulSoup(page.text, 'html.parser') meta = soup.find(property="og:image") - image_link = meta["content"] + image_link = meta["content"] #type: ignore - return image_link + return image_link # type: ignore def get_videos(channel_id: str, @@ -308,7 +324,9 @@ def make_sized_button(parent_pnl: wx.Panel, bitmap_or_str: Union[wx.Bitmap, return btn_sizer -def make_bitmap_from_url(logo_url: str, size: wx.Size = SIZE, video_id: str = "") -> wx.Bitmap: +def make_bitmap_from_url(logo_url: str, + size: wx.Size = SIZE, + video_id: str = "") -> wx.Bitmap: if not video_id: video_id = hash_string(logo_url) thumbpath = path.join(BASEPATH, 'thumbnails') @@ -344,55 +362,21 @@ def make_bitmap_from_file(path, size: wx.Size = SIZE) -> wx.Bitmap: def resolve_svt_channel(svt_id: str, path: str = '/usr/share/cast') -> dict: + global CHAN_CACHE - channels = { - "ch-barnkanalen": { - "name": - "Barnkanalen", - "thumbnail": - make_bitmap_from_file('{}/assets/Barnkanalen.png'.format(path)) - }, - "ch-svt1": { - "name": "SVT 1", - "thumbnail": - make_bitmap_from_file('{}/assets/SVT1.png'.format(path)) - }, - "ch-svt2": { - "name": "SVT 2", - "thumbnail": - make_bitmap_from_file('{}/assets/SVT2.png'.format(path)) - }, - "ch-svt24": { - "name": "SVT 24", - "thumbnail": - make_bitmap_from_file('{}/assets/SVT24.png'.format(path)) - }, - "ch-kunskapskanalen": { - "name": - "Kunskapskanalen", - "thumbnail": - make_bitmap_from_file('{}/assets/Kunskapskanalen.png'.format(path)) - }, - "feed": { + if CHAN_CACHE: + channels = CHAN_CACHE + else: + channels = get_all_svt_channels() + channels["feed"] = { "name": "Senaste program", "thumbnail": make_bitmap_from_file('{}/assets/SVT.png'.format(path)) - }, - "allprograms": { + } + channels["allprograms"] = { "name": "Alla program", "thumbnail": make_bitmap_from_file('{}/assets/SVT.png'.format(path)) - }, - } - - for category in get_all_svt_categories(): - channels[category['id']] = { - "name": - category["name"], - "thumbnail": - make_bitmap_from_url( - get_svt_thumb_from_id_changed(category['image']['id'], - category['image']['changed'])) } return channels[svt_id]