Spaces:
Sleeping
Sleeping
Commit
·
83371d8
1
Parent(s):
8e4ba0a
patch 2
Browse files
api.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import requests
|
| 2 |
import logging
|
|
|
|
| 3 |
|
| 4 |
class InstancesAPI:
|
| 5 |
def __init__(self, instances):
|
|
@@ -16,6 +17,7 @@ class InstancesAPI:
|
|
| 16 |
logging.error(f"Error contacting instance {instance_url}: {e}")
|
| 17 |
return reports
|
| 18 |
|
|
|
|
| 19 |
def download_music(self, instance_url, title):
|
| 20 |
"""
|
| 21 |
Download a music file to an instance.
|
|
@@ -29,7 +31,10 @@ class InstancesAPI:
|
|
| 29 |
"""
|
| 30 |
data = {}
|
| 31 |
try:
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
response.raise_for_status()
|
| 34 |
data = response.json()
|
| 35 |
|
|
|
|
| 1 |
import requests
|
| 2 |
import logging
|
| 3 |
+
from urllib.parse import quote
|
| 4 |
|
| 5 |
class InstancesAPI:
|
| 6 |
def __init__(self, instances):
|
|
|
|
| 17 |
logging.error(f"Error contacting instance {instance_url}: {e}")
|
| 18 |
return reports
|
| 19 |
|
| 20 |
+
|
| 21 |
def download_music(self, instance_url, title):
|
| 22 |
"""
|
| 23 |
Download a music file to an instance.
|
|
|
|
| 31 |
"""
|
| 32 |
data = {}
|
| 33 |
try:
|
| 34 |
+
# URL-encode the title to handle special characters
|
| 35 |
+
encoded_title = quote(title)
|
| 36 |
+
|
| 37 |
+
response = requests.get(f"{instance_url}/api/get/music/{encoded_title}")
|
| 38 |
response.raise_for_status()
|
| 39 |
data = response.json()
|
| 40 |
|
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import os
|
|
| 5 |
from typing import List
|
| 6 |
from LoadBalancer import LoadBalancer
|
| 7 |
from utils import is_valid_url
|
|
|
|
| 8 |
|
| 9 |
CACHE_DIR = os.getenv("CACHE_DIR")
|
| 10 |
TOKEN = os.getenv("TOKEN")
|
|
@@ -93,24 +94,30 @@ async def get_instances_health():
|
|
| 93 |
@app.get("/api/get/music/{file_name}")
|
| 94 |
async def get_music_api(file_name: str):
|
| 95 |
"""Endpoint to get the music file by title."""
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
raise HTTPException(status_code=400, detail="file_name parameter is required")
|
| 98 |
|
| 99 |
# Check if the music file is already cached
|
| 100 |
-
if
|
| 101 |
-
url = load_balancer.MUSIC_STORE[
|
| 102 |
return JSONResponse(content={"url": url})
|
| 103 |
|
| 104 |
-
music_path = load_balancer.find_music_path(
|
| 105 |
|
| 106 |
if not music_path:
|
| 107 |
raise HTTPException(status_code=404, detail="Music file not found")
|
| 108 |
|
| 109 |
# Start the download in an instance
|
| 110 |
-
response = load_balancer.download_music_to_best_instance(file_name=
|
|
|
|
| 111 |
if response:
|
| 112 |
return JSONResponse(content=response)
|
| 113 |
|
|
|
|
| 114 |
@app.get('/api/get/category/all')
|
| 115 |
async def get_categories():
|
| 116 |
return load_balancer.get_all_categories()
|
|
|
|
| 5 |
from typing import List
|
| 6 |
from LoadBalancer import LoadBalancer
|
| 7 |
from utils import is_valid_url
|
| 8 |
+
from urllib.parse import unquote
|
| 9 |
|
| 10 |
CACHE_DIR = os.getenv("CACHE_DIR")
|
| 11 |
TOKEN = os.getenv("TOKEN")
|
|
|
|
| 94 |
@app.get("/api/get/music/{file_name}")
|
| 95 |
async def get_music_api(file_name: str):
|
| 96 |
"""Endpoint to get the music file by title."""
|
| 97 |
+
|
| 98 |
+
# Decode the file_name from URL encoding
|
| 99 |
+
decoded_file_name = unquote(file_name)
|
| 100 |
+
|
| 101 |
+
if not decoded_file_name:
|
| 102 |
raise HTTPException(status_code=400, detail="file_name parameter is required")
|
| 103 |
|
| 104 |
# Check if the music file is already cached
|
| 105 |
+
if decoded_file_name in load_balancer.MUSIC_STORE:
|
| 106 |
+
url = load_balancer.MUSIC_STORE[decoded_file_name]
|
| 107 |
return JSONResponse(content={"url": url})
|
| 108 |
|
| 109 |
+
music_path = load_balancer.find_music_path(decoded_file_name)
|
| 110 |
|
| 111 |
if not music_path:
|
| 112 |
raise HTTPException(status_code=404, detail="Music file not found")
|
| 113 |
|
| 114 |
# Start the download in an instance
|
| 115 |
+
response = load_balancer.download_music_to_best_instance(file_name=decoded_file_name)
|
| 116 |
+
|
| 117 |
if response:
|
| 118 |
return JSONResponse(content=response)
|
| 119 |
|
| 120 |
+
|
| 121 |
@app.get('/api/get/category/all')
|
| 122 |
async def get_categories():
|
| 123 |
return load_balancer.get_all_categories()
|