NeoPy commited on
Commit
72116c0
·
verified ·
1 Parent(s): bfb77b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import re
4
+ from urllib.parse import urlparse
5
+ import os
6
+
7
+ def validate_url(url):
8
+ """Validate if the URL is a valid soundgasm.net link."""
9
+ pattern = r'^https?://soundgasm\.net/u/[\w-]+/[\w-]+'
10
+ return bool(re.match(pattern, url.strip()))
11
+
12
+ def extract_audio_url(page_content):
13
+ """Extract the direct audio URL from the Soundgasm page content."""
14
+ audio_pattern = r'(https?://media\.soundgasm\.net/sounds/[\w-]+\.(?:mp3|m4a))'
15
+ match = re.search(audio_pattern, page_content)
16
+ return match.group(1) if match else None
17
+
18
+ def download_audio(url):
19
+ """Download audio from a soundgasm.net link."""
20
+ try:
21
+ # Validate URL
22
+ if not validate_url(url):
23
+ return "Invalid Soundgasm.net URL. Please provide a valid link."
24
+
25
+ # Get the page content
26
+ headers = {
27
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
28
+ }
29
+ response = requests.get(url.strip(), headers=headers, timeout=10)
30
+ response.raise_for_status()
31
+
32
+ # Extract audio URL
33
+ audio_url = extract_audio_url(response.text)
34
+ if not audio_url:
35
+ return "Could not find audio file in the provided link."
36
+
37
+ # Download the audio
38
+ audio_response = requests.get(audio_url, headers=headers, stream=True, timeout=10)
39
+ audio_response.raise_for_status()
40
+
41
+ # Extract filename from URL
42
+ parsed_url = urlparse(audio_url)
43
+ filename = os.path.basename(parsed_url.path)
44
+
45
+ # Save the audio file temporarily
46
+ temp_dir = "downloads"
47
+ os.makedirs(temp_dir, exist_ok=True)
48
+ file_path = os.path.join(temp_dir, filename)
49
+
50
+ with open(file_path, 'wb') as f:
51
+ for chunk in audio_response.iter_content(chunk_size=8192):
52
+ if chunk:
53
+ f.write(chunk)
54
+
55
+ return file_path
56
+
57
+ except requests.exceptions.RequestException as e:
58
+ return f"Error downloading audio: {str(e)}"
59
+ except Exception as e:
60
+ return f"An unexpected error occurred: {str(e)}"
61
+
62
+ # Gradio Blocks UI
63
+ with gr.Blocks(title="Soundgasm Audio Downloader") as demo:
64
+ gr.Markdown("# Soundgasm Audio Downloader")
65
+ gr.Markdown("Enter a Soundgasm.net link to download the audio file.")
66
+
67
+ with gr.Row():
68
+ url_input = gr.Textbox(label="Soundgasm URL", placeholder="https://soundgasm.net/u/username/audio-title")
69
+ download_button = gr.Button("Download Audio")
70
+
71
+ output = gr.File(label="Downloaded Audio")
72
+ error_message = gr.Textbox(label="Status", interactive=False)
73
+
74
+ download_button.click(
75
+ fn=download_audio,
76
+ inputs=url_input,
77
+ outputs=[output, error_message]
78
+ )
79
+
80
+ if __name__ == "__main__":
81
+ demo.launch()