From 449eb9ba73e7dd7e23b884ca3021ebf42e817917 Mon Sep 17 00:00:00 2001 From: WorldObservationLog Date: Mon, 20 May 2024 20:32:20 +0800 Subject: [PATCH] fix: code format --- src/api.py | 104 +++++++++++++++++++---------------------------------- 1 file changed, 37 insertions(+), 67 deletions(-) diff --git a/src/api.py b/src/api.py index 27bdeed..cf07e1e 100644 --- a/src/api.py +++ b/src/api.py @@ -2,7 +2,6 @@ import asyncio import logging from ssl import SSLError -import httpcore import httpx import regex from async_lru import alru_cache @@ -15,6 +14,7 @@ from src.models.song_data import Datum client: httpx.AsyncClient download_lock: asyncio.Semaphore request_lock: asyncio.Semaphore +retry_times = 10 user_agent_browser = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" user_agent_itunes = "iTunes/12.11.3 (Windows; Microsoft Windows 10 x64 Professional Edition (Build 19041); x64) AppleWebKit/7611.1022.4001.1 (dt:2)" user_agent_app = "Music/5.7 Android/10 model/Pixel6GR1YH build/1234 (dt:66)" @@ -30,9 +30,8 @@ def init_client_and_lock(proxy: str, parallel_num: int): request_lock = asyncio.Semaphore(64) -@retry(retry=retry_if_exception_type((httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_m3u8_from_api(endpoint: str, song_id: str, wait_and_retry: bool = False, recursion_times: int = 0) -> str: async with request_lock: resp = (await client.get(endpoint, params={"songid": song_id})).text @@ -44,9 +43,8 @@ async def get_m3u8_from_api(endpoint: str, song_id: str, wait_and_retry: bool = return resp -@retry(retry=retry_if_exception_type((httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def upload_m3u8_to_api(endpoint: str, m3u8_url: str, song_info: Datum): async with request_lock: await client.post(endpoint, json={ @@ -61,9 +59,8 @@ async def upload_m3u8_to_api(endpoint: str, m3u8_url: str, song_info: Datum): }) -@retry(retry=retry_if_exception_type((httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_token(): async with request_lock: req = await client.get("https://beta.music.apple.com", follow_redirects=True) @@ -74,20 +71,16 @@ async def get_token(): @alru_cache -@retry(retry=retry_if_exception_type((httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, - httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def download_song(url: str) -> bytes: async with download_lock: return (await client.get(url)).content @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_album_info(album_id: str, token: str, storefront: str, lang: str): async with request_lock: req = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/albums/{album_id}", @@ -101,10 +94,8 @@ async def get_album_info(album_id: str, token: str, storefront: str, lang: str): @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_playlist_info_and_tracks(playlist_id: str, token: str, storefront: str, lang: str): async with request_lock: resp = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/playlists/{playlist_id}", @@ -119,10 +110,8 @@ async def get_playlist_info_and_tracks(playlist_id: str, token: str, storefront: @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_playlist_tracks(playlist_id: str, token: str, storefront: str, lang: str, offset: int = 0): async with request_lock: resp = await client.get( @@ -139,10 +128,8 @@ async def get_playlist_tracks(playlist_id: str, token: str, storefront: str, lan @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_cover(url: str, cover_format: str, cover_size: str): async with request_lock: formatted_url = regex.sub('bb.jpg', f'bb.{cover_format}', url) @@ -152,10 +139,8 @@ async def get_cover(url: str, cover_format: str, cover_size: str): @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_song_info(song_id: str, token: str, storefront: str, lang: str): async with request_lock: req = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/songs/{song_id}", @@ -170,10 +155,8 @@ async def get_song_info(song_id: str, token: str, storefront: str, lang: str): @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_song_lyrics(song_id: str, storefront: str, token: str, dsid: str, account_token: str, lang: str) -> str: async with request_lock: req = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/songs/{song_id}/lyrics", @@ -186,10 +169,8 @@ async def get_song_lyrics(song_id: str, storefront: str, token: str, dsid: str, @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_albums_from_artist(artist_id: str, storefront: str, token: str, lang: str, offset: int = 0): async with request_lock: resp = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/artists/{artist_id}/albums", @@ -205,10 +186,8 @@ async def get_albums_from_artist(artist_id: str, storefront: str, token: str, la @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_songs_from_artist(artist_id: str, storefront: str, token: str, lang: str, offset: int = 0): async with request_lock: resp = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/artists/{artist_id}/songs", @@ -224,10 +203,8 @@ async def get_songs_from_artist(artist_id: str, storefront: str, token: str, lan @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_artist_info(artist_id: str, storefront: str, token: str, lang: str): async with request_lock: resp = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/artists/{artist_id}", @@ -238,10 +215,8 @@ async def get_artist_info(artist_id: str, storefront: str, token: str, lang: str @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def download_m3u8(m3u8_url: str) -> str: async with request_lock: resp = await client.get(m3u8_url) @@ -249,17 +224,16 @@ async def download_m3u8(m3u8_url: str) -> str: @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_real_url(url: str): req = await client.get(url, follow_redirects=True, headers={"User-Agent": user_agent_browser}) return str(req.url) @alru_cache -@retry(retry=retry_if_exception_type(httpx.HTTPError), stop=stop_after_attempt(5)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def get_album_by_upc(upc: str, storefront: str, token: str): req = await client.get(f"https://amp-api.music.apple.com/v1/catalog/{storefront}/albums", params={"filter[upc]": upc}, @@ -276,10 +250,8 @@ async def get_album_by_upc(upc: str, storefront: str, token: str): @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def exist_on_storefront_by_song_id(song_id: str, storefront: str, check_storefront: str, token: str, lang: str): if storefront == check_storefront: return True @@ -292,10 +264,8 @@ async def exist_on_storefront_by_song_id(song_id: str, storefront: str, check_st @alru_cache -@retry(retry=retry_if_exception_type( - (httpx.TimeoutException, httpcore.ConnectError, SSLError, FileNotFoundError, httpcore.RemoteProtocolError)), - stop=stop_after_attempt(5), - before_sleep=before_sleep_log(logger, logging.WARN)) +@retry(retry=retry_if_exception_type((httpx.HTTPError, SSLError, FileNotFoundError)), + stop=stop_after_attempt(retry_times), before_sleep=before_sleep_log(logger, logging.WARN)) async def exist_on_storefront_by_album_id(album_id: str, storefront: str, check_storefront: str, token: str, lang: str): album = await get_album_info(album_id, token, storefront, lang) upc = album.data[0].attributes.upc