Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
| 2 |
+
from urllib.parse import urlparse, parse_qs
|
| 3 |
+
import subprocess
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
import time
|
| 7 |
+
import threading
|
| 8 |
+
|
| 9 |
+
PORT = 7830
|
| 10 |
+
DOWNLOAD_DIR = "downloads"
|
| 11 |
+
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
def delete_after(filepath, delay):
|
| 14 |
+
def delete():
|
| 15 |
+
time.sleep(delay)
|
| 16 |
+
if os.path.exists(filepath):
|
| 17 |
+
os.remove(filepath)
|
| 18 |
+
print(f"[AUTO DELETE] Removed {filepath}")
|
| 19 |
+
threading.Thread(target=delete, daemon=True).start()
|
| 20 |
+
|
| 21 |
+
class SpotDLServer(BaseHTTPRequestHandler):
|
| 22 |
+
def do_GET(self):
|
| 23 |
+
parsed = urlparse(self.path)
|
| 24 |
+
|
| 25 |
+
if parsed.path == "/sptdl":
|
| 26 |
+
query = parse_qs(parsed.query)
|
| 27 |
+
url = query.get("url", [None])[0]
|
| 28 |
+
|
| 29 |
+
if not url:
|
| 30 |
+
self.respond_json({"error": "Missing 'url' parameter"})
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
+
timestamp = str(int(time.time()))
|
| 34 |
+
print(f"[REQUEST] Spotify URL: {url}")
|
| 35 |
+
|
| 36 |
+
before_files = set(os.listdir(DOWNLOAD_DIR))
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
subprocess.run(
|
| 40 |
+
["spotdl", url, "--output", DOWNLOAD_DIR + "/"],
|
| 41 |
+
check=True,
|
| 42 |
+
stdout=subprocess.DEVNULL,
|
| 43 |
+
stderr=subprocess.DEVNULL
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
after_files = set(os.listdir(DOWNLOAD_DIR))
|
| 47 |
+
new_files = list(after_files - before_files)
|
| 48 |
+
if not new_files:
|
| 49 |
+
raise Exception("No file downloaded")
|
| 50 |
+
|
| 51 |
+
original_file = new_files[0]
|
| 52 |
+
original_path = os.path.join(DOWNLOAD_DIR, original_file)
|
| 53 |
+
renamed_path = os.path.join(DOWNLOAD_DIR, f"{timestamp}.mp3")
|
| 54 |
+
|
| 55 |
+
os.rename(original_path, renamed_path)
|
| 56 |
+
|
| 57 |
+
real_title = os.path.splitext(original_file)[0]
|
| 58 |
+
host = self.headers.get("Host")
|
| 59 |
+
download_url = f"http://{host}/{renamed_path}"
|
| 60 |
+
|
| 61 |
+
print(f"[SUCCESS] Downloaded: {real_title}")
|
| 62 |
+
print(f"[SAVED AS] {timestamp}.mp3")
|
| 63 |
+
|
| 64 |
+
delete_after(renamed_path, 120)
|
| 65 |
+
|
| 66 |
+
self.respond_json({
|
| 67 |
+
"title": real_title,
|
| 68 |
+
"download_link": download_url
|
| 69 |
+
})
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"[ERROR] {str(e)}")
|
| 73 |
+
self.respond_json({"error": str(e)})
|
| 74 |
+
|
| 75 |
+
elif self.path.startswith(f"/{DOWNLOAD_DIR}/"):
|
| 76 |
+
file_path = self.path.lstrip("/")
|
| 77 |
+
if os.path.isfile(file_path):
|
| 78 |
+
with open(file_path, "rb") as f:
|
| 79 |
+
self.send_response(200)
|
| 80 |
+
self.send_header("Content-Type", "audio/mpeg")
|
| 81 |
+
self.end_headers()
|
| 82 |
+
self.wfile.write(f.read())
|
| 83 |
+
else:
|
| 84 |
+
self.send_error(404, "File not found.")
|
| 85 |
+
else:
|
| 86 |
+
self.send_error(404, "Invalid endpoint.")
|
| 87 |
+
|
| 88 |
+
def respond_json(self, data):
|
| 89 |
+
self.send_response(200)
|
| 90 |
+
self.send_header("Content-Type", "application/json")
|
| 91 |
+
self.end_headers()
|
| 92 |
+
self.wfile.write(json.dumps(data).encode())
|
| 93 |
+
|
| 94 |
+
def run_server():
|
| 95 |
+
print(f"[SERVER] SpotDL server running at http://0.0.0.0:{PORT}")
|
| 96 |
+
HTTPServer(("0.0.0.0", PORT), SpotDLServer).serve_forever()
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
run_server()
|