Update app.py
Browse files
app.py
CHANGED
|
@@ -33,14 +33,40 @@ def searcht():
|
|
| 33 |
first_song = next((song for song in search_results if 'videoId' in song and song['videoId']), {}) if search_results else {}
|
| 34 |
return jsonify(first_song)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
async def get_track_download_url(track_id: str) -> str:
|
| 37 |
-
apis =
|
| 38 |
-
"https://cobalt-api.kwiatekmiki.com/",
|
| 39 |
-
"https://cobalt-api.ayo.tf/",
|
| 40 |
-
"https://dwnld.nichind.dev",
|
| 41 |
-
"https://yt.edd1e.xyz/",
|
| 42 |
-
"http://34.107.254.11"
|
| 43 |
-
]
|
| 44 |
session = cloudscraper.create_scraper() # Requires cloudscraper package
|
| 45 |
headers = {
|
| 46 |
"Accept": "application/json",
|
|
@@ -64,6 +90,7 @@ async def get_track_download_url(track_id: str) -> str:
|
|
| 64 |
if response.status_code == 200:
|
| 65 |
download_url = response.json().get("url")
|
| 66 |
if download_url:
|
|
|
|
| 67 |
logger.info(f"Successfully obtained download URL from {api_url}")
|
| 68 |
return download_url
|
| 69 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 33 |
first_song = next((song for song in search_results if 'videoId' in song and song['videoId']), {}) if search_results else {}
|
| 34 |
return jsonify(first_song)
|
| 35 |
|
| 36 |
+
|
| 37 |
+
class ApiRotator:
|
| 38 |
+
def __init__(self, apis):
|
| 39 |
+
self.apis = apis
|
| 40 |
+
self.last_successful_index = None
|
| 41 |
+
|
| 42 |
+
def get_prioritized_apis(self):
|
| 43 |
+
if self.last_successful_index is not None:
|
| 44 |
+
# Move the last successful API to the front
|
| 45 |
+
rotated_apis = (
|
| 46 |
+
[self.apis[self.last_successful_index]] +
|
| 47 |
+
self.apis[:self.last_successful_index] +
|
| 48 |
+
self.apis[self.last_successful_index+1:]
|
| 49 |
+
)
|
| 50 |
+
return rotated_apis
|
| 51 |
+
return self.apis
|
| 52 |
+
|
| 53 |
+
def update_last_successful(self, index):
|
| 54 |
+
self.last_successful_index = index
|
| 55 |
+
|
| 56 |
+
# In your function:
|
| 57 |
+
api_rotator = ApiRotator([
|
| 58 |
+
"https://cobalt-api.ayo.tf",
|
| 59 |
+
"http://34.107.254.11",
|
| 60 |
+
"https://dwnld.nichind.dev",
|
| 61 |
+
"https://cobalt-api.kwiatekmiki.com",
|
| 62 |
+
"https://yt.edd1e.xyz/"
|
| 63 |
+
|
| 64 |
+
])
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
async def get_track_download_url(track_id: str) -> str:
|
| 69 |
+
apis = api_rotator.get_prioritized_apis()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
session = cloudscraper.create_scraper() # Requires cloudscraper package
|
| 71 |
headers = {
|
| 72 |
"Accept": "application/json",
|
|
|
|
| 90 |
if response.status_code == 200:
|
| 91 |
download_url = response.json().get("url")
|
| 92 |
if download_url:
|
| 93 |
+
api_rotator.update_last_successful(apis.index(api_url))
|
| 94 |
logger.info(f"Successfully obtained download URL from {api_url}")
|
| 95 |
return download_url
|
| 96 |
except requests.exceptions.RequestException as e:
|