sudo-soldier commited on
Commit
7d50d22
·
verified ·
1 Parent(s): 1ac0332

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -7,7 +7,7 @@ import re
7
  import requests
8
  import io
9
 
10
- # Set up Redis connection
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
- # Ensure downloads directory exists
 
 
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
- # Read file contents
35
  with open(file_path, 'rb') as file:
36
  file_data = file.read()
37
 
38
- # Upload file to the worker storage (e.g., R2 or KV)
39
  response = requests.put(
40
  f"{worker_base_url}/{sanitized_song_name}",
41
  data=file_data,
42
- headers={"Content-Type": "audio/mpeg"} # For MP3, adjust for other formats
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' # YT cookies (if needed)
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'] # Use YouTube video title as the song name
71
 
72
  elif uploaded_file:
73
  filename = uploaded_file.name
74
- song_name = os.path.splitext(uploaded_file.name)[0] # Use file name as song name
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
- # Create iPhone ringtone (Apple M4R format)
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] # Clip to 20 seconds for ringtone
89
  ringtone_audio.export(ringtone_filename_m4r, format="mp4")
90
 
91
- # Save ringtones to Worker and Redis
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 # Returning the file paths for download
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}")