File size: 3,440 Bytes
72116c0 8b0f313 72116c0 6e9813d 8b0f313 6e9813d 72116c0 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 72116c0 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 72116c0 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 8b0f313 6e9813d 72116c0 8b0f313 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
import gradio as gr
import requests
from bs4 import BeautifulSoup
import os
import re
import tempfile
import shutil
def download_soundgasm_audio(url, output_dir='downloaded_audio'):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
try:
response = requests.get(url)
response.raise_for_status() # Raise an exception for HTTP errors
except requests.exceptions.RequestException as e:
return None, f'Error fetching URL: {e}'
# Try to find the audio source directly in the HTML using regex
match = re.search(r'(https?://media\.soundgasm\.net/sounds/[^\"]+\.m4a)', response.text)
audio_url = None
if match:
audio_url = match.group(1)
if not audio_url:
# Fallback to BeautifulSoup if regex fails
soup = BeautifulSoup(response.text, 'html.parser')
audio_tag = soup.find('audio')
if audio_tag and 'src' in audio_tag.attrs:
audio_url = audio_tag['src']
if not audio_url.startswith('http'):
audio_url = f"https://soundgasm.net{audio_url}"
if audio_url:
try:
audio_response = requests.get(audio_url, stream=True)
audio_response.raise_for_status()
except requests.exceptions.RequestException as e:
return None, f'Error fetching audio file: {e}'
file_name = os.path.join(output_dir, os.path.basename(audio_url).split('?')[0])
with open(file_name, 'wb') as f:
for chunk in audio_response.iter_content(chunk_size=8192):
f.write(chunk)
return file_name, f'Audio downloaded successfully!'
else:
return None, 'No audio source found on the page.'
def process_soundgasm_url(url):
if not url:
return None, "Please enter a soundgasm.net URL"
if "soundgasm.net" not in url:
return None, "Please enter a valid soundgasm.net URL"
audio_file, message = download_soundgasm_audio(url)
if audio_file:
return audio_file, message
else:
return None, message
# Create Gradio interface using Blocks
with gr.Blocks(title="Soundgasm Audio Downloader", theme=gr.themes.Soft()) as demo:
gr.Markdown("# Soundgasm Audio Downloader")
gr.Markdown("Enter a soundgasm.net link to download and play the audio file.")
with gr.Row():
with gr.Column():
url_input = gr.Textbox(
label="Soundgasm URL",
placeholder="https://soundgasm.net/u/username/audio-title",
lines=1
)
download_btn = gr.Button("Download Audio", variant="primary")
with gr.Column():
status_output = gr.Textbox(
label="Status",
interactive=False,
lines=2
)
with gr.Row():
audio_output = gr.Audio(
label="Downloaded Audio",
type="filepath",
interactive=False
)
# Event handlers
download_btn.click(
fn=process_soundgasm_url,
inputs=[url_input],
outputs=[audio_output, status_output]
)
# Also allow Enter key to trigger download
url_input.submit(
fn=process_soundgasm_url,
inputs=[url_input],
outputs=[audio_output, status_output]
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|