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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from soundgasm_scraper import SoundgasmScraper
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# Initialize the scraper
|
| 7 |
+
scraper = SoundgasmScraper()
|
| 8 |
+
|
| 9 |
+
def search_soundgasm(query, max_results=10):
|
| 10 |
+
"""
|
| 11 |
+
Search for audio on Soundgasm and return results in a formatted way
|
| 12 |
+
"""
|
| 13 |
+
if not query.strip():
|
| 14 |
+
return "Please enter a search query.", None
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
# Show loading message
|
| 18 |
+
results = scraper.search_audio(query, max_results=int(max_results))
|
| 19 |
+
|
| 20 |
+
if not results:
|
| 21 |
+
return "No results found. Try a different search term.", None
|
| 22 |
+
|
| 23 |
+
# Format results for display
|
| 24 |
+
formatted_results = []
|
| 25 |
+
for i, result in enumerate(results, 1):
|
| 26 |
+
formatted_result = f"""
|
| 27 |
+
### Result {i}: {result['audio_title'] or 'Untitled'}
|
| 28 |
+
|
| 29 |
+
**Username:** {result['username'] or 'Unknown'}
|
| 30 |
+
|
| 31 |
+
**Page URL:** [{result['url']}]({result['url']})
|
| 32 |
+
|
| 33 |
+
**Direct Audio URL:** {result['audio_url'] or 'Not found'}
|
| 34 |
+
|
| 35 |
+
**Description:** {result['description'][:200] + '...' if result['description'] and len(result['description']) > 200 else result['description'] or 'No description available'}
|
| 36 |
+
|
| 37 |
+
---
|
| 38 |
+
"""
|
| 39 |
+
formatted_results.append(formatted_result)
|
| 40 |
+
|
| 41 |
+
# Create a DataFrame for the table view
|
| 42 |
+
df_data = []
|
| 43 |
+
for result in results:
|
| 44 |
+
df_data.append({
|
| 45 |
+
'Title': result['audio_title'] or 'Untitled',
|
| 46 |
+
'Username': result['username'] or 'Unknown',
|
| 47 |
+
'Page URL': result['url'],
|
| 48 |
+
'Audio URL': result['audio_url'] or 'Not found',
|
| 49 |
+
'Description': (result['description'][:100] + '...' if result['description'] and len(result['description']) > 100 else result['description']) or 'No description'
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
df = pd.DataFrame(df_data)
|
| 53 |
+
|
| 54 |
+
return "\n".join(formatted_results), df
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error occurred during search: {str(e)}", None
|
| 58 |
+
|
| 59 |
+
def search_by_user(username):
|
| 60 |
+
"""
|
| 61 |
+
Search for all audios by a specific username
|
| 62 |
+
"""
|
| 63 |
+
if not username.strip():
|
| 64 |
+
return "Please enter a username.", None
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
results = scraper.search_by_username(username.strip())
|
| 68 |
+
|
| 69 |
+
if not results:
|
| 70 |
+
return f"No audios found for user '{username}' or user doesn't exist.", None
|
| 71 |
+
|
| 72 |
+
# Format results for display
|
| 73 |
+
formatted_results = []
|
| 74 |
+
for i, result in enumerate(results, 1):
|
| 75 |
+
formatted_result = f"""
|
| 76 |
+
### Audio {i}: {result['audio_title'] or 'Untitled'}
|
| 77 |
+
|
| 78 |
+
**Page URL:** [{result['url']}]({result['url']})
|
| 79 |
+
|
| 80 |
+
**Direct Audio URL:** {result['audio_url'] or 'Not found'}
|
| 81 |
+
|
| 82 |
+
**Description:** {result['description'][:200] + '...' if result['description'] and len(result['description']) > 200 else result['description'] or 'No description available'}
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
"""
|
| 86 |
+
formatted_results.append(formatted_result)
|
| 87 |
+
|
| 88 |
+
# Create a DataFrame for the table view
|
| 89 |
+
df_data = []
|
| 90 |
+
for result in results:
|
| 91 |
+
df_data.append({
|
| 92 |
+
'Title': result['audio_title'] or 'Untitled',
|
| 93 |
+
'Page URL': result['url'],
|
| 94 |
+
'Audio URL': result['audio_url'] or 'Not found',
|
| 95 |
+
'Description': (result['description'][:100] + '...' if result['description'] and len(result['description']) > 100 else result['description']) or 'No description'
|
| 96 |
+
})
|
| 97 |
+
|
| 98 |
+
df = pd.DataFrame(df_data)
|
| 99 |
+
|
| 100 |
+
return "\n".join(formatted_results), df
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
return f"Error occurred during user search: {str(e)}", None
|
| 104 |
+
|
| 105 |
+
# Create the Gradio interface
|
| 106 |
+
with gr.Blocks(title="Soundgasm Audio Search", theme=gr.themes.Base()) as app:
|
| 107 |
+
gr.Markdown("""
|
| 108 |
+
# π§ Soundgasm Audio Search
|
| 109 |
+
|
| 110 |
+
Search for audio content on Soundgasm.net and get direct links to the audio files.
|
| 111 |
+
|
| 112 |
+
**Features:**
|
| 113 |
+
- Search by keywords
|
| 114 |
+
- Search by username
|
| 115 |
+
- Get direct audio file URLs
|
| 116 |
+
- View audio descriptions and metadata
|
| 117 |
+
""")
|
| 118 |
+
|
| 119 |
+
with gr.Tabs():
|
| 120 |
+
# Search by keywords tab
|
| 121 |
+
with gr.TabItem("π Search by Keywords"):
|
| 122 |
+
with gr.Row():
|
| 123 |
+
with gr.Column(scale=3):
|
| 124 |
+
search_input = gr.Textbox(
|
| 125 |
+
label="Search Query",
|
| 126 |
+
placeholder="Enter keywords (e.g., ASMR, relaxing, etc.)",
|
| 127 |
+
lines=1
|
| 128 |
+
)
|
| 129 |
+
with gr.Column(scale=1):
|
| 130 |
+
max_results_input = gr.Slider(
|
| 131 |
+
minimum=1,
|
| 132 |
+
maximum=20,
|
| 133 |
+
value=10,
|
| 134 |
+
step=1,
|
| 135 |
+
label="Max Results"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
search_button = gr.Button("π Search", variant="primary")
|
| 139 |
+
|
| 140 |
+
with gr.Row():
|
| 141 |
+
with gr.Column():
|
| 142 |
+
search_output = gr.Markdown(label="Search Results")
|
| 143 |
+
with gr.Column():
|
| 144 |
+
search_table = gr.Dataframe(
|
| 145 |
+
label="Results Table",
|
| 146 |
+
interactive=False,
|
| 147 |
+
wrap=True
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Search by username tab
|
| 151 |
+
with gr.TabItem("π€ Search by Username"):
|
| 152 |
+
username_input = gr.Textbox(
|
| 153 |
+
label="Username",
|
| 154 |
+
placeholder="Enter Soundgasm username",
|
| 155 |
+
lines=1
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
user_search_button = gr.Button("π Search User", variant="primary")
|
| 159 |
+
|
| 160 |
+
with gr.Row():
|
| 161 |
+
with gr.Column():
|
| 162 |
+
user_output = gr.Markdown(label="User's Audios")
|
| 163 |
+
with gr.Column():
|
| 164 |
+
user_table = gr.Dataframe(
|
| 165 |
+
label="User's Audios Table",
|
| 166 |
+
interactive=False,
|
| 167 |
+
wrap=True
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# Instructions
|
| 171 |
+
with gr.Accordion("π How to Use", open=False):
|
| 172 |
+
gr.Markdown("""
|
| 173 |
+
### Search by Keywords:
|
| 174 |
+
1. Enter keywords related to the audio you're looking for
|
| 175 |
+
2. Adjust the maximum number of results if needed
|
| 176 |
+
3. Click "Search" to find matching audios
|
| 177 |
+
|
| 178 |
+
### Search by Username:
|
| 179 |
+
1. Enter a Soundgasm username
|
| 180 |
+
2. Click "Search User" to see all audios from that user
|
| 181 |
+
|
| 182 |
+
### Results:
|
| 183 |
+
- **Page URL**: Link to the Soundgasm page
|
| 184 |
+
- **Audio URL**: Direct link to the audio file (m4a format)
|
| 185 |
+
- **Description**: Audio description if available
|
| 186 |
+
|
| 187 |
+
### Note:
|
| 188 |
+
- This tool searches using external search engines, so results may vary
|
| 189 |
+
- Some audio URLs might not be extractable due to page structure changes
|
| 190 |
+
- Always respect the content creators' rights and terms of service
|
| 191 |
+
""")
|
| 192 |
+
|
| 193 |
+
# Event handlers
|
| 194 |
+
search_button.click(
|
| 195 |
+
fn=search_soundgasm,
|
| 196 |
+
inputs=[search_input, max_results_input],
|
| 197 |
+
outputs=[search_output, search_table]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
user_search_button.click(
|
| 201 |
+
fn=search_by_user,
|
| 202 |
+
inputs=[username_input],
|
| 203 |
+
outputs=[user_output, user_table]
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# Allow Enter key to trigger search
|
| 207 |
+
search_input.submit(
|
| 208 |
+
fn=search_soundgasm,
|
| 209 |
+
inputs=[search_input, max_results_input],
|
| 210 |
+
outputs=[search_output, search_table]
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
username_input.submit(
|
| 214 |
+
fn=search_by_user,
|
| 215 |
+
inputs=[username_input],
|
| 216 |
+
outputs=[user_output, user_table]
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# Launch the app
|
| 220 |
+
if __name__ == "__main__":
|
| 221 |
+
app.launch(
|
| 222 |
+
server_name="0.0.0.0",
|
| 223 |
+
server_port=7860,
|
| 224 |
+
share=False,
|
| 225 |
+
debug=True
|
| 226 |
+
)
|
| 227 |
+
|