From 037b824cb1644a65f8b67e1300700df689ea1a99 Mon Sep 17 00:00:00 2001 From: garret Date: Mon, 25 Sep 2023 13:10:09 +0100 Subject: Cache station metadata for a day Station metadata changes extremely rarely, if ever, so it seems awfully silly to keep downloading the exact same metadata every time we extract. Wastes requests, wastes time (300ms round trip for me) This commit makes it so station metadata is cached for a day, and cached data is used in place of downloaded metadata when available. Closes #16 --- yt_dlp_plugins/extractor/radiko.py | 40 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/yt_dlp_plugins/extractor/radiko.py b/yt_dlp_plugins/extractor/radiko.py index d035cfd..6852453 100755 --- a/yt_dlp_plugins/extractor/radiko.py +++ b/yt_dlp_plugins/extractor/radiko.py @@ -502,21 +502,31 @@ class _RadikoBaseIE(InfoExtractor): return token def _get_station_meta(self, region, station_id): - region = self._download_xml(f"https://radiko.jp/v3/station/list/{region}.xml", region, note="Downloading station listings") - station = region.find(f'.//station/id[.="{station_id}"]/..') # a with an of our station_id - station_name = station.find("name").text - station_url = url_or_none(station.find("href").text) - return { - "title": station_name, - "channel": station_name, - "uploader": station_name, - "channel_id": station_id, - "channel_url": station_url, - "thumbnail": url_or_none(station.find("banner").text), - "alt_title": station.find("ascii_name").text, - "uploader_url": station_url, - "id": station_id, - } + cachedata = self.cache.load("rajiko", station_id) + now = datetime.datetime.now() + if cachedata is None or cachedata.get("expiry") < now.timestamp(): + region = self._download_xml(f"https://radiko.jp/v3/station/list/{region}.xml", region, + note="Downloading station metadata") + station = region.find(f'.//station/id[.="{station_id}"]/..') # a with an of our station_id + station_name = station.find("name").text + station_url = url_or_none(station.find("href").text) + meta = { + "title": station_name, + "channel": station_name, + "uploader": station_name, + "channel_id": station_id, + "channel_url": station_url, + "thumbnail": url_or_none(station.find("banner").text), + "alt_title": station.find("ascii_name").text, + "uploader_url": station_url, + "id": station_id, + } + self.cache.store("rajiko", station_id, { + "expiry": (now + datetime.timedelta(days=1)).timestamp(), "meta": meta}) + return meta + else: + self.to_screen(f"{station_id}: Using cached station metadata") + return cachedata.get("meta") def _get_station_formats(self, station, timefree, auth_data, start_at=None, end_at=None): # smartphone formats api = always happy path -- cgit v1.2.3-70-g09d2