haraberget's picture
Update app.py
68003b2 verified
raw
history blame
1.89 kB
# app.py
import re, requests, uuid, os
import gradio as gr
def normalize_udio_input(input_text):
"""Accept song page URL, embed URL, or iframe snippet, return embed URL"""
match = re.search(r'src=["\'](https://www\.udio\.com/embed/[^"\']+)["\']', input_text)
if match:
return match.group(1)
if input_text.startswith("https://www.udio.com/embed/"):
return input_text.split()[0]
song_match = re.search(r'https://www\.udio\.com/songs/([A-Za-z0-9]+)', input_text)
if song_match:
song_id = song_match.group(1)
return f"https://www.udio.com/embed/{song_id}"
return None
def get_mp3(embed_url):
if not embed_url:
return "Invalid input", None, None
try:
r = requests.get(embed_url)
html = r.text
mp3_match = re.search(r'https://storage\.googleapis\.com/.*?\.mp3', html)
if mp3_match:
mp3_url = mp3_match.group(0)
return (
f"✅ Found MP3: {mp3_url}",
embed_url,
mp3_url
)
else:
return "⚠️ MP3 link not found in embed page.", embed_url, None
except Exception as e:
return f"❌ Error fetching embed: {e}", embed_url, None
def process_input(user_input):
embed_url = normalize_udio_input(user_input)
msg, embed, mp3 = get_mp3(embed_url)
return msg, embed, mp3
demo = gr.Interface(
fn=process_input,
inputs=gr.Textbox(label="Udio Song / Embed / Snippet", placeholder="Paste Udio URL or embed iframe here"),
outputs=[
gr.Textbox(label="Status / Message"),
gr.HTML(label="Embed Preview"),
gr.HTML(label="MP3 Download Link")
],
title="🎵 Udio Fetcher",
description="Paste any Udio song link or embed code and get the playable embed + direct MP3 download link."
)
if __name__ == "__main__":
demo.launch()