diff --git a/music_kraken/connection/connection.py b/music_kraken/connection/connection.py index fe7f2ff..f648fa1 100644 --- a/music_kraken/connection/connection.py +++ b/music_kraken/connection/connection.py @@ -125,11 +125,14 @@ class Connection: 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 = {} if error: 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) def request( @@ -145,6 +148,7 @@ class Connection: sleep_after_404: float = None, is_heartbeat: bool = False, disable_cache: bool = None, + enable_cache_readonly: bool = False, method: str = None, name: str = "", exclude_headers: List[str] = None, @@ -178,7 +182,7 @@ class Connection: 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) if cached is not None: diff --git a/music_kraken/pages/youtube_music/youtube_music.py b/music_kraken/pages/youtube_music/youtube_music.py index 5e82a22..107f882 100644 --- a/music_kraken/pages/youtube_music/youtube_music.py +++ b/music_kraken/pages/youtube_music/youtube_music.py @@ -171,7 +171,7 @@ class YoutubeMusic(SuperYouTube): def __init__(self, *args, ydl_opts: dict = None, **kwargs): self.yt_music_connection: YoutubeMusicConnection = YoutubeMusicConnection( logger=self.LOGGER, - accept_language="en-US,en;q=0.5" + accept_language="en-US,en;q=0.5", ) self.credentials: YouTubeMusicCredentials = YouTubeMusicCredentials( api_key=youtube_settings["youtube_music_api_key"], @@ -212,7 +212,7 @@ class YoutubeMusic(SuperYouTube): 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: return @@ -232,7 +232,7 @@ class YoutubeMusic(SuperYouTube): 'set_ytc': 'true', 'set_apyt': 'true', 'set_eom': 'false' - }) + }, disable_cache=True) if r is None: return @@ -247,9 +247,9 @@ class YoutubeMusic(SuperYouTube): # save cookies in settings youtube_settings["youtube_music_consent_cookies"] = cookie_dict 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: return @@ -374,7 +374,8 @@ class YoutubeMusic(SuperYouTube): }, headers={ "Referer": get_youtube_url(path=f"/search", query=f"q={urlescaped_query}") - } + }, + name=f"search_{search_query}.json" ) if r is None: @@ -411,7 +412,8 @@ class YoutubeMusic(SuperYouTube): json={ "browseId": browse_id, "context": {**self.credentials.context, "adSignalsInfo": {"params": []}} - } + }, + name=f"fetch_artist_{browse_id}.json" ) if r is None: return artist @@ -454,7 +456,8 @@ class YoutubeMusic(SuperYouTube): json={ "browseId": browse_id, "context": {**self.credentials.context, "adSignalsInfo": {"params": []}} - } + }, + name=f"fetch_album_{browse_id}.json" ) if r is None: return album