|
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() |
|
except requests.exceptions.RequestException as e: |
|
return None, f'Error fetching URL: {e}' |
|
|
|
|
|
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: |
|
|
|
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 |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
download_btn.click( |
|
fn=process_soundgasm_url, |
|
inputs=[url_input], |
|
outputs=[audio_output, status_output] |
|
) |
|
|
|
|
|
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) |
|
|
|
|