Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import re
|
|
7 |
import requests
|
8 |
import io
|
9 |
|
10 |
-
|
11 |
redis = Redis(
|
12 |
host="amused-walleye-31373.upstash.io",
|
13 |
port=31373,
|
@@ -16,10 +16,10 @@ redis = Redis(
|
|
16 |
db=0
|
17 |
)
|
18 |
|
19 |
-
# Cloudflare Worker URL
|
20 |
-
worker_base_url = "https://server.jessejesse.workers.dev" # Your Cloudflare Worker URL
|
21 |
|
22 |
-
|
|
|
|
|
23 |
os.makedirs("downloads", exist_ok=True)
|
24 |
|
25 |
def sanitize_filename(filename):
|
@@ -31,15 +31,15 @@ def save_ringtone_to_worker(song_name, file_path):
|
|
31 |
try:
|
32 |
sanitized_song_name = sanitize_filename(song_name)
|
33 |
|
34 |
-
|
35 |
with open(file_path, 'rb') as file:
|
36 |
file_data = file.read()
|
37 |
|
38 |
-
|
39 |
response = requests.put(
|
40 |
f"{worker_base_url}/{sanitized_song_name}",
|
41 |
data=file_data,
|
42 |
-
headers={"Content-Type": "audio/mpeg"}
|
43 |
)
|
44 |
|
45 |
if response.status_code == 200:
|
@@ -61,17 +61,17 @@ def process_youtube_url(url, uploaded_file):
|
|
61 |
ydl_opts = {
|
62 |
'format': 'bestaudio/best',
|
63 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
64 |
-
'cookiefile': 'cookies.txt' #
|
65 |
}
|
66 |
|
67 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
68 |
info = ydl.extract_info(url, download=True)
|
69 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
70 |
-
song_name = info['title']
|
71 |
|
72 |
elif uploaded_file:
|
73 |
filename = uploaded_file.name
|
74 |
-
song_name = os.path.splitext(uploaded_file.name)[0]
|
75 |
|
76 |
if not filename or not os.path.exists(filename):
|
77 |
return None, None # No file, no output
|
@@ -82,17 +82,17 @@ def process_youtube_url(url, uploaded_file):
|
|
82 |
audio = AudioSegment.from_file(filename)
|
83 |
audio.export(mp3_filename, format="mp3")
|
84 |
|
85 |
-
|
86 |
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
|
87 |
if not os.path.exists(ringtone_filename_m4r):
|
88 |
-
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] #
|
89 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
90 |
|
91 |
-
|
92 |
save_ringtone_to_worker(f"{song_name}.mp3", mp3_filename)
|
93 |
save_ringtone_to_worker(f"{song_name}.m4r", ringtone_filename_m4r)
|
94 |
|
95 |
-
return mp3_filename, ringtone_filename_m4r
|
96 |
|
97 |
except Exception as e:
|
98 |
print(f"Error: {e}")
|
|
|
7 |
import requests
|
8 |
import io
|
9 |
|
10 |
+
|
11 |
redis = Redis(
|
12 |
host="amused-walleye-31373.upstash.io",
|
13 |
port=31373,
|
|
|
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):
|
|
|
31 |
try:
|
32 |
sanitized_song_name = sanitize_filename(song_name)
|
33 |
|
34 |
+
|
35 |
with open(file_path, 'rb') as file:
|
36 |
file_data = file.read()
|
37 |
|
38 |
+
|
39 |
response = requests.put(
|
40 |
f"{worker_base_url}/{sanitized_song_name}",
|
41 |
data=file_data,
|
42 |
+
headers={"Content-Type": "audio/mpeg"}
|
43 |
)
|
44 |
|
45 |
if response.status_code == 200:
|
|
|
61 |
ydl_opts = {
|
62 |
'format': 'bestaudio/best',
|
63 |
'outtmpl': 'downloads/%(id)s.%(ext)s',
|
64 |
+
'cookiefile': 'cookies.txt' #cookies
|
65 |
}
|
66 |
|
67 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
68 |
info = ydl.extract_info(url, download=True)
|
69 |
filename = os.path.join('downloads', f"{info['id']}.webm")
|
70 |
+
song_name = info['title']
|
71 |
|
72 |
elif uploaded_file:
|
73 |
filename = uploaded_file.name
|
74 |
+
song_name = os.path.splitext(uploaded_file.name)[0]
|
75 |
|
76 |
if not filename or not os.path.exists(filename):
|
77 |
return None, None # No file, no output
|
|
|
82 |
audio = AudioSegment.from_file(filename)
|
83 |
audio.export(mp3_filename, format="mp3")
|
84 |
|
85 |
+
|
86 |
ringtone_filename_m4r = f"downloads/{song_name}.m4r"
|
87 |
if not os.path.exists(ringtone_filename_m4r):
|
88 |
+
ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] #20 seconds
|
89 |
ringtone_audio.export(ringtone_filename_m4r, format="mp4")
|
90 |
|
91 |
+
|
92 |
save_ringtone_to_worker(f"{song_name}.mp3", mp3_filename)
|
93 |
save_ringtone_to_worker(f"{song_name}.m4r", ringtone_filename_m4r)
|
94 |
|
95 |
+
return mp3_filename, ringtone_filename_m4r
|
96 |
|
97 |
except Exception as e:
|
98 |
print(f"Error: {e}")
|