haraberget commited on
Commit
f3e046a
·
verified ·
1 Parent(s): 1069a63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -14
app.py CHANGED
@@ -1,7 +1,9 @@
1
- # app.py
2
  import re, requests, uuid, os
3
  import gradio as gr
4
 
 
 
 
5
  def normalize_udio_input(input_text):
6
  """Accept song page URL, embed URL, or iframe snippet, return embed URL"""
7
  match = re.search(r'src=["\'](https://www\.udio\.com/embed/[^"\']+)["\']', input_text)
@@ -16,8 +18,9 @@ def normalize_udio_input(input_text):
16
  return None
17
 
18
  def get_mp3(embed_url):
 
19
  if not embed_url:
20
- return "Invalid input", None, None
21
 
22
  try:
23
  r = requests.get(embed_url)
@@ -25,31 +28,47 @@ def get_mp3(embed_url):
25
  mp3_match = re.search(r'https://storage\.googleapis\.com/.*?\.mp3', html)
26
  if mp3_match:
27
  mp3_url = mp3_match.group(0)
28
- return (
29
- f"✅ Found MP3: {mp3_url}",
30
- embed_url,
31
- mp3_url
32
- )
33
  else:
34
- return "⚠️ MP3 link not found in embed page.", embed_url, None
35
  except Exception as e:
36
- return f"❌ Error fetching embed: {e}", embed_url, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  def process_input(user_input):
39
  embed_url = normalize_udio_input(user_input)
40
  msg, embed, mp3 = get_mp3(embed_url)
41
- return msg, embed, mp3
 
 
 
 
 
42
 
43
  demo = gr.Interface(
44
  fn=process_input,
45
- inputs=gr.Textbox(label="Udio Song / Embed / Snippet", placeholder="Paste Udio URL or embed iframe here"),
46
  outputs=[
47
  gr.Textbox(label="Status / Message"),
48
  gr.HTML(label="Embed Preview"),
49
- gr.HTML(label="MP3 Download Link")
 
50
  ],
51
- title="🎵 Udio Fetcher",
52
- description="Paste any Udio song link or embed code and get the playable embed + direct MP3 download link."
53
  )
54
 
55
  if __name__ == "__main__":
 
 
1
  import re, requests, uuid, os
2
  import gradio as gr
3
 
4
+ DOWNLOAD_FOLDER = "downloads"
5
+ os.makedirs(DOWNLOAD_FOLDER, exist_ok=True)
6
+
7
  def normalize_udio_input(input_text):
8
  """Accept song page URL, embed URL, or iframe snippet, return embed URL"""
9
  match = re.search(r'src=["\'](https://www\.udio\.com/embed/[^"\']+)["\']', input_text)
 
18
  return None
19
 
20
  def get_mp3(embed_url):
21
+ """Extract MP3 URL from Udio embed"""
22
  if not embed_url:
23
+ return "Invalid or missing URL", None, None
24
 
25
  try:
26
  r = requests.get(embed_url)
 
28
  mp3_match = re.search(r'https://storage\.googleapis\.com/.*?\.mp3', html)
29
  if mp3_match:
30
  mp3_url = mp3_match.group(0)
31
+ return f"✅ Found MP3: {mp3_url}", embed_url, mp3_url
 
 
 
 
32
  else:
33
+ return "⚠️ MP3 link not found on embed page.", embed_url, None
34
  except Exception as e:
35
+ return f"❌ Error fetching embed page: {e}", embed_url, None
36
+
37
+ def download_mp3(mp3_url):
38
+ """Download MP3 and return local path"""
39
+ if not mp3_url:
40
+ return None
41
+ try:
42
+ filename = f"{uuid.uuid4()}.mp3"
43
+ filepath = os.path.join(DOWNLOAD_FOLDER, filename)
44
+ r = requests.get(mp3_url)
45
+ with open(filepath, "wb") as f:
46
+ f.write(r.content)
47
+ return filepath
48
+ except Exception:
49
+ return None
50
 
51
  def process_input(user_input):
52
  embed_url = normalize_udio_input(user_input)
53
  msg, embed, mp3 = get_mp3(embed_url)
54
+
55
+ embed_html = f'<iframe src="{embed}" width="400" height="200" frameborder="0"></iframe>' if embed else ""
56
+ mp3_html = f'<a href="{mp3}" target="_blank">{mp3}</a>' if mp3 else ""
57
+ local_file = download_mp3(mp3) if mp3 else None
58
+
59
+ return msg, embed_html, mp3_html, local_file
60
 
61
  demo = gr.Interface(
62
  fn=process_input,
63
+ inputs=gr.Textbox(label="🎶 Udio Song / Embed / Snippet", placeholder="Paste Udio URL or embed iframe here"),
64
  outputs=[
65
  gr.Textbox(label="Status / Message"),
66
  gr.HTML(label="Embed Preview"),
67
+ gr.HTML(label="Direct MP3 Link"),
68
+ gr.File(label="Download MP3")
69
  ],
70
+ title="🎵 Udio Fetcher + Downloader",
71
+ description="Paste any Udio song link or embed code and get the playable embed, direct MP3 link, and downloadable file."
72
  )
73
 
74
  if __name__ == "__main__":