1
0
mirror of https://git.sr.ht/~cadence/NewLeaf synced 2024-11-21 15:17:29 +00:00

Channel path fixes I'm pretty sure I already did?

- use "channels" as default path, not "user"
- cache based on the combination of the path and the id
- fix channel latest
This commit is contained in:
Cadence Ember 2022-09-12 23:13:37 +12:00
parent ac1aa07108
commit 714f1030fb
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
2 changed files with 10 additions and 8 deletions

View File

@ -13,9 +13,11 @@ channel_latest_cache = TTLCache(maxsize=500, ttl=300)
channel_latest_cache_lock = Lock() channel_latest_cache_lock = Lock()
def extract_channel(ucid, second__path="user"): def extract_channel(ucid, second__path="user"):
cache_key = (ucid, second__path)
with channel_cache_lock: with channel_cache_lock:
if ucid in channel_cache: if cache_key in channel_cache:
return channel_cache[ucid] return channel_cache[cache_key]
channel_type = "channel" if len(ucid) == 24 and ucid[:2] == "UC" else second__path channel_type = "channel" if len(ucid) == 24 and ucid[:2] == "UC" else second__path
r = requests.get("https://www.youtube.com/{}/{}/videos?hl=en".format(channel_type, ucid), cookies=eu_consent_cookie()) r = requests.get("https://www.youtube.com/{}/{}/videos?hl=en".format(channel_type, ucid), cookies=eu_consent_cookie())
@ -154,12 +156,12 @@ def extract_channel(ucid, second__path="user"):
} }
with channel_cache_lock: with channel_cache_lock:
channel_cache[ucid] = channel channel_cache[cache_key] = channel
return channel return channel
def extract_channel_videos(ucid): def extract_channel_videos(ucid, second__path="channel"):
channel = extract_channel(ucid) channel = extract_channel(ucid, second__path)
if "error" in channel: if "error" in channel:
return channel return channel
else: else:

View File

@ -58,7 +58,7 @@ class NewLeaf(object):
@cherrypy.expose @cherrypy.expose
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
def channels(self, *suffix, second__path="user", **kwargs): def channels(self, *suffix, second__path="channel", **kwargs):
ucid = "" ucid = ""
part = "" part = ""
possible_parts = ("videos", "latest", "playlists") possible_parts = ("videos", "latest", "playlists")
@ -74,7 +74,7 @@ class NewLeaf(object):
"error": "Two components specified in URL, but neither component was recognised as a part keyword.", "error": "Two components specified in URL, but neither component was recognised as a part keyword.",
"identifier": "PART_KEYWORD_NOT_RECOGNISED" "identifier": "PART_KEYWORD_NOT_RECOGNISED"
} }
possible_paths = ("channel", "c", "user") possible_paths = ("channel",) if part == "latest" else ("channel", "c", "user")
if second__path not in possible_paths: if second__path not in possible_paths:
return { return {
"error": "second__path parameter must be one of: " + str(possible_paths), "error": "second__path parameter must be one of: " + str(possible_paths),
@ -84,7 +84,7 @@ class NewLeaf(object):
if part == "playlists": if part == "playlists":
return [] return []
elif part == "latest": elif part == "latest":
return extract_channel_latest(ucid, second__path) return extract_channel_latest(ucid)
elif part == "videos": elif part == "videos":
return extract_channel_videos(ucid, second__path) return extract_channel_videos(ucid, second__path)
else: # part == "", so extract whole channel else: # part == "", so extract whole channel