Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,7 @@ from redis import Redis
|
|
6 |
import re
|
7 |
import requests
|
8 |
|
9 |
-
|
10 |
-
#Redis
|
11 |
redis = Redis(
|
12 |
host="amused-walleye-31373.upstash.io",
|
13 |
port=31373,
|
@@ -16,10 +15,10 @@ redis = Redis(
|
|
16 |
db=0
|
17 |
)
|
18 |
|
19 |
-
#
|
20 |
worker_base_url = "https://server.jessejesse.workers.dev"
|
21 |
|
22 |
-
|
23 |
os.makedirs("downloads", exist_ok=True)
|
24 |
|
25 |
def sanitize_filename(filename):
|
@@ -42,9 +41,9 @@ def save_ringtone_to_worker(song_name, file_path):
|
|
42 |
|
43 |
if response.status_code == 200:
|
44 |
file_url = f"{worker_base_url}/{sanitized_song_name}"
|
45 |
-
redis.set(sanitized_song_name, file_url)
|
46 |
print(f"Saved {sanitized_song_name} URL to Redis: {file_url}")
|
47 |
-
return file_url
|
48 |
else:
|
49 |
print(f"Failed to upload file. Status code: {response.status_code}")
|
50 |
return None
|
@@ -59,12 +58,12 @@ def process_youtube_url(url, uploaded_file):
|
|
59 |
filename = None
|
60 |
song_name = None
|
61 |
|
62 |
-
|
63 |
if url:
|
64 |
ydl_opts = {
|
65 |
'format': 'bestaudio/best',
|
66 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
67 |
-
'cookiefile': 'cookies.txt' #cookies
|
68 |
}
|
69 |
|
70 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
@@ -72,28 +71,28 @@ def process_youtube_url(url, uploaded_file):
|
|
72 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
73 |
song_name = info['title']
|
74 |
|
75 |
-
|
76 |
elif uploaded_file:
|
77 |
filename = uploaded_file.name
|
78 |
song_name = os.path.splitext(uploaded_file.name)[0]
|
79 |
|
80 |
-
|
81 |
if not filename or not os.path.exists(filename):
|
82 |
return None, None
|
83 |
|
84 |
-
|
85 |
mp3_filename = f"downloads/{song_name}.mp3"
|
86 |
if not os.path.exists(mp3_filename):
|
87 |
audio = AudioSegment.from_file(filename)
|
88 |
audio.export(mp3_filename, format="mp3")
|
89 |
|
90 |
-
|
91 |
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
|
92 |
if not os.path.exists(ringtone_filename_m4r):
|
93 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
|
94 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
95 |
|
96 |
-
|
97 |
mp3_url = save_ringtone_to_worker(f"{song_name}.mp3", mp3_filename)
|
98 |
m4r_url = save_ringtone_to_worker(f"{song_name}.m4r", ringtone_filename_m4r)
|
99 |
|
@@ -137,4 +136,3 @@ interface.launch(share=True)
|
|
137 |
|
138 |
|
139 |
|
140 |
-
|
|
|
6 |
import re
|
7 |
import requests
|
8 |
|
9 |
+
# Redis configuration
|
|
|
10 |
redis = Redis(
|
11 |
host="amused-walleye-31373.upstash.io",
|
12 |
port=31373,
|
|
|
15 |
db=0
|
16 |
)
|
17 |
|
18 |
+
# Cloudflare Worker base URL
|
19 |
worker_base_url = "https://server.jessejesse.workers.dev"
|
20 |
|
21 |
+
# Create downloads directory if it doesn't exist
|
22 |
os.makedirs("downloads", exist_ok=True)
|
23 |
|
24 |
def sanitize_filename(filename):
|
|
|
41 |
|
42 |
if response.status_code == 200:
|
43 |
file_url = f"{worker_base_url}/{sanitized_song_name}"
|
44 |
+
redis.set(sanitized_song_name, file_url) # Save the URL to Redis
|
45 |
print(f"Saved {sanitized_song_name} URL to Redis: {file_url}")
|
46 |
+
return file_url # Return the URL for later use
|
47 |
else:
|
48 |
print(f"Failed to upload file. Status code: {response.status_code}")
|
49 |
return None
|
|
|
58 |
filename = None
|
59 |
song_name = None
|
60 |
|
61 |
+
# Download the song if a URL is provided
|
62 |
if url:
|
63 |
ydl_opts = {
|
64 |
'format': 'bestaudio/best',
|
65 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
66 |
+
'cookiefile': 'cookies.txt' # Optional: use cookies for authentication
|
67 |
}
|
68 |
|
69 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
|
71 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
72 |
song_name = info['title']
|
73 |
|
74 |
+
# Use uploaded file if URL is not provided
|
75 |
elif uploaded_file:
|
76 |
filename = uploaded_file.name
|
77 |
song_name = os.path.splitext(uploaded_file.name)[0]
|
78 |
|
79 |
+
# Ensure file exists
|
80 |
if not filename or not os.path.exists(filename):
|
81 |
return None, None
|
82 |
|
83 |
+
# Convert to MP3 if not already converted
|
84 |
mp3_filename = f"downloads/{song_name}.mp3"
|
85 |
if not os.path.exists(mp3_filename):
|
86 |
audio = AudioSegment.from_file(filename)
|
87 |
audio.export(mp3_filename, format="mp3")
|
88 |
|
89 |
+
# Convert to M4R (iPhone ringtone format), limiting length to 20 seconds
|
90 |
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
|
91 |
if not os.path.exists(ringtone_filename_m4r):
|
92 |
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
|
93 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
94 |
|
95 |
+
# Upload both files to Worker and get URLs
|
96 |
mp3_url = save_ringtone_to_worker(f"{song_name}.mp3", mp3_filename)
|
97 |
m4r_url = save_ringtone_to_worker(f"{song_name}.m4r", ringtone_filename_m4r)
|
98 |
|
|
|
136 |
|
137 |
|
138 |
|
|