diff options
author | garret <garret@airmail.cc> | 2023-09-25 13:10:09 +0100 |
---|---|---|
committer | garret <garret@airmail.cc> | 2023-09-25 13:10:09 +0100 |
commit | 037b824cb1644a65f8b67e1300700df689ea1a99 (patch) | |
tree | f8c67092fb7c1a6c20899374a308f20b7ca8dc16 /yt_dlp_plugins/extractor | |
parent | 15012800ad2049ca7ea3ea0a17fe5f7755d5c268 (diff) | |
download | yt-dlp-rajiko-037b824cb1644a65f8b67e1300700df689ea1a99.tar.gz yt-dlp-rajiko-037b824cb1644a65f8b67e1300700df689ea1a99.tar.bz2 yt-dlp-rajiko-037b824cb1644a65f8b67e1300700df689ea1a99.zip |
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
Diffstat (limited to 'yt_dlp_plugins/extractor')
-rwxr-xr-x | yt_dlp_plugins/extractor/radiko.py | 40 |
1 files 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 <station> with an <id> 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 <station> with an <id> 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 |