feat: added caching to youtube
This commit is contained in:
parent
e2b7c5475f
commit
e77afa584b
@ -125,11 +125,14 @@ class Connection:
|
|||||||
|
|
||||||
return headers
|
return headers
|
||||||
|
|
||||||
def save(self, r: requests.Response, name: str, error: bool = False, **kwargs):
|
def save(self, r: requests.Response, name: str, error: bool = False, no_update_if_valid_exists: bool = False, **kwargs):
|
||||||
n_kwargs = {}
|
n_kwargs = {}
|
||||||
if error:
|
if error:
|
||||||
n_kwargs["module"] = "failed_requests"
|
n_kwargs["module"] = "failed_requests"
|
||||||
|
|
||||||
|
if self.cache.get(name) is not None and no_update_if_valid_exists:
|
||||||
|
return
|
||||||
|
|
||||||
self.cache.set(r.content, name, expires_in=kwargs.get("expires_in", self.cache_expiring_duration), **n_kwargs)
|
self.cache.set(r.content, name, expires_in=kwargs.get("expires_in", self.cache_expiring_duration), **n_kwargs)
|
||||||
|
|
||||||
def request(
|
def request(
|
||||||
@ -145,6 +148,7 @@ class Connection:
|
|||||||
sleep_after_404: float = None,
|
sleep_after_404: float = None,
|
||||||
is_heartbeat: bool = False,
|
is_heartbeat: bool = False,
|
||||||
disable_cache: bool = None,
|
disable_cache: bool = None,
|
||||||
|
enable_cache_readonly: bool = False,
|
||||||
method: str = None,
|
method: str = None,
|
||||||
name: str = "",
|
name: str = "",
|
||||||
exclude_headers: List[str] = None,
|
exclude_headers: List[str] = None,
|
||||||
@ -178,7 +182,7 @@ class Connection:
|
|||||||
|
|
||||||
request_url = parsed_url.geturl() if not raw_url else url
|
request_url = parsed_url.geturl() if not raw_url else url
|
||||||
|
|
||||||
if name != "" and not disable_cache:
|
if name != "" and (not disable_cache or enable_cache_readonly):
|
||||||
cached = self.cache.get(name)
|
cached = self.cache.get(name)
|
||||||
|
|
||||||
if cached is not None:
|
if cached is not None:
|
||||||
|
@ -171,7 +171,7 @@ class YoutubeMusic(SuperYouTube):
|
|||||||
def __init__(self, *args, ydl_opts: dict = None, **kwargs):
|
def __init__(self, *args, ydl_opts: dict = None, **kwargs):
|
||||||
self.yt_music_connection: YoutubeMusicConnection = YoutubeMusicConnection(
|
self.yt_music_connection: YoutubeMusicConnection = YoutubeMusicConnection(
|
||||||
logger=self.LOGGER,
|
logger=self.LOGGER,
|
||||||
accept_language="en-US,en;q=0.5"
|
accept_language="en-US,en;q=0.5",
|
||||||
)
|
)
|
||||||
self.credentials: YouTubeMusicCredentials = YouTubeMusicCredentials(
|
self.credentials: YouTubeMusicCredentials = YouTubeMusicCredentials(
|
||||||
api_key=youtube_settings["youtube_music_api_key"],
|
api_key=youtube_settings["youtube_music_api_key"],
|
||||||
@ -212,7 +212,7 @@ class YoutubeMusic(SuperYouTube):
|
|||||||
search for: "innertubeApiKey"
|
search for: "innertubeApiKey"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
r = self.yt_music_connection.get("https://music.youtube.com/")
|
r = self.yt_music_connection.get("https://music.youtube.com/", name="youtube_music_index.html", disable_cache=True, enable_cache_readonly=True)
|
||||||
if r is None:
|
if r is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -232,7 +232,7 @@ class YoutubeMusic(SuperYouTube):
|
|||||||
'set_ytc': 'true',
|
'set_ytc': 'true',
|
||||||
'set_apyt': 'true',
|
'set_apyt': 'true',
|
||||||
'set_eom': 'false'
|
'set_eom': 'false'
|
||||||
})
|
}, disable_cache=True)
|
||||||
if r is None:
|
if r is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -247,9 +247,9 @@ class YoutubeMusic(SuperYouTube):
|
|||||||
# save cookies in settings
|
# save cookies in settings
|
||||||
youtube_settings["youtube_music_consent_cookies"] = cookie_dict
|
youtube_settings["youtube_music_consent_cookies"] = cookie_dict
|
||||||
else:
|
else:
|
||||||
self.yt_music_connection.save(r, "index.html")
|
self.yt_music_connection.save(r, "youtube_music_index.html", no_update_if_valid_exists=True)
|
||||||
|
|
||||||
r = self.yt_music_connection.get("https://music.youtube.com/", name="index.html")
|
r = self.yt_music_connection.get("https://music.youtube.com/", name="youtube_music_index.html")
|
||||||
if r is None:
|
if r is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -374,7 +374,8 @@ class YoutubeMusic(SuperYouTube):
|
|||||||
},
|
},
|
||||||
headers={
|
headers={
|
||||||
"Referer": get_youtube_url(path=f"/search", query=f"q={urlescaped_query}")
|
"Referer": get_youtube_url(path=f"/search", query=f"q={urlescaped_query}")
|
||||||
}
|
},
|
||||||
|
name=f"search_{search_query}.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
if r is None:
|
if r is None:
|
||||||
@ -411,7 +412,8 @@ class YoutubeMusic(SuperYouTube):
|
|||||||
json={
|
json={
|
||||||
"browseId": browse_id,
|
"browseId": browse_id,
|
||||||
"context": {**self.credentials.context, "adSignalsInfo": {"params": []}}
|
"context": {**self.credentials.context, "adSignalsInfo": {"params": []}}
|
||||||
}
|
},
|
||||||
|
name=f"fetch_artist_{browse_id}.json"
|
||||||
)
|
)
|
||||||
if r is None:
|
if r is None:
|
||||||
return artist
|
return artist
|
||||||
@ -454,7 +456,8 @@ class YoutubeMusic(SuperYouTube):
|
|||||||
json={
|
json={
|
||||||
"browseId": browse_id,
|
"browseId": browse_id,
|
||||||
"context": {**self.credentials.context, "adSignalsInfo": {"params": []}}
|
"context": {**self.credentials.context, "adSignalsInfo": {"params": []}}
|
||||||
}
|
},
|
||||||
|
name=f"fetch_album_{browse_id}.json"
|
||||||
)
|
)
|
||||||
if r is None:
|
if r is None:
|
||||||
return album
|
return album
|
||||||
|
Loading…
Reference in New Issue
Block a user