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|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import re
|
| 4 |
+
import json
|
| 5 |
+
from urllib.parse import urljoin, quote
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
class SoundgasmScraper:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.base_url = "https://soundgasm.net"
|
| 11 |
+
self.session = requests.Session()
|
| 12 |
+
self.session.headers.update({
|
| 13 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 14 |
+
})
|
| 15 |
+
|
| 16 |
+
def search_audio(self, query, max_results=10):
|
| 17 |
+
"""
|
| 18 |
+
Search for audio on Soundgasm.net
|
| 19 |
+
Since Soundgasm doesn't have a built-in search, we'll use external search engines
|
| 20 |
+
"""
|
| 21 |
+
results = []
|
| 22 |
+
|
| 23 |
+
# Use Google to search for Soundgasm content
|
| 24 |
+
search_query = f"site:soundgasm.net {query}"
|
| 25 |
+
google_url = f"https://www.google.com/search?q={quote(search_query)}"
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
response = self.session.get(google_url)
|
| 29 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 30 |
+
|
| 31 |
+
# Extract Soundgasm links from Google search results
|
| 32 |
+
links = soup.find_all('a', href=True)
|
| 33 |
+
soundgasm_links = []
|
| 34 |
+
|
| 35 |
+
for link in links:
|
| 36 |
+
href = link.get('href')
|
| 37 |
+
if href and 'soundgasm.net/u/' in href:
|
| 38 |
+
# Clean up the URL
|
| 39 |
+
if href.startswith('/url?q='):
|
| 40 |
+
href = href.split('/url?q=')[1].split('&')[0]
|
| 41 |
+
if href.startswith('http') and 'soundgasm.net/u/' in href:
|
| 42 |
+
soundgasm_links.append(href)
|
| 43 |
+
|
| 44 |
+
# Remove duplicates and limit results
|
| 45 |
+
soundgasm_links = list(set(soundgasm_links))[:max_results]
|
| 46 |
+
|
| 47 |
+
# Get details for each audio
|
| 48 |
+
for link in soundgasm_links:
|
| 49 |
+
audio_info = self.get_audio_info(link)
|
| 50 |
+
if audio_info:
|
| 51 |
+
results.append(audio_info)
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Search error: {e}")
|
| 55 |
+
|
| 56 |
+
return results
|
| 57 |
+
|
| 58 |
+
def get_audio_info(self, url):
|
| 59 |
+
"""
|
| 60 |
+
Extract audio information from a Soundgasm page
|
| 61 |
+
"""
|
| 62 |
+
try:
|
| 63 |
+
response = self.session.get(url)
|
| 64 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 65 |
+
|
| 66 |
+
# Extract title
|
| 67 |
+
title_elem = soup.find('title')
|
| 68 |
+
title = title_elem.text.strip() if title_elem else "Unknown Title"
|
| 69 |
+
|
| 70 |
+
# Extract description
|
| 71 |
+
description = ""
|
| 72 |
+
desc_elem = soup.find('div', class_='jp-description')
|
| 73 |
+
if desc_elem:
|
| 74 |
+
description = desc_elem.get_text(strip=True)
|
| 75 |
+
|
| 76 |
+
# Extract audio file URL from JavaScript
|
| 77 |
+
audio_url = None
|
| 78 |
+
scripts = soup.find_all('script')
|
| 79 |
+
for script in scripts:
|
| 80 |
+
if script.string:
|
| 81 |
+
# Look for the audio file URL in the JavaScript
|
| 82 |
+
match = re.search(r'["\']([^"\']*\.m4a)["\']', script.string)
|
| 83 |
+
if match:
|
| 84 |
+
audio_url = match.group(1)
|
| 85 |
+
if not audio_url.startswith('http'):
|
| 86 |
+
audio_url = urljoin(self.base_url, audio_url)
|
| 87 |
+
break
|
| 88 |
+
|
| 89 |
+
# Extract username from URL
|
| 90 |
+
username = ""
|
| 91 |
+
url_match = re.search(r'/u/([^/]+)/', url)
|
| 92 |
+
if url_match:
|
| 93 |
+
username = url_match.group(1)
|
| 94 |
+
|
| 95 |
+
# Extract audio title from URL
|
| 96 |
+
audio_title = ""
|
| 97 |
+
title_match = re.search(r'/u/[^/]+/(.+)$', url)
|
| 98 |
+
if title_match:
|
| 99 |
+
audio_title = title_match.group(1).replace('-', ' ').replace('_', ' ')
|
| 100 |
+
|
| 101 |
+
return {
|
| 102 |
+
'title': title,
|
| 103 |
+
'audio_title': audio_title,
|
| 104 |
+
'username': username,
|
| 105 |
+
'description': description,
|
| 106 |
+
'url': url,
|
| 107 |
+
'audio_url': audio_url,
|
| 108 |
+
'duration': None # Would need to download file to get duration
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
print(f"Error getting audio info for {url}: {e}")
|
| 113 |
+
return None
|
| 114 |
+
|
| 115 |
+
def search_by_username(self, username):
|
| 116 |
+
"""
|
| 117 |
+
Get all audios from a specific user
|
| 118 |
+
"""
|
| 119 |
+
user_url = f"{self.base_url}/u/{username}"
|
| 120 |
+
try:
|
| 121 |
+
response = self.session.get(user_url)
|
| 122 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 123 |
+
|
| 124 |
+
# Find all audio links on the user page
|
| 125 |
+
audio_links = []
|
| 126 |
+
links = soup.find_all('a', href=True)
|
| 127 |
+
|
| 128 |
+
for link in links:
|
| 129 |
+
href = link.get('href')
|
| 130 |
+
if href and f'/u/{username}/' in href and href != f'/u/{username}':
|
| 131 |
+
full_url = urljoin(self.base_url, href)
|
| 132 |
+
audio_links.append(full_url)
|
| 133 |
+
|
| 134 |
+
# Get info for each audio
|
| 135 |
+
results = []
|
| 136 |
+
for link in audio_links:
|
| 137 |
+
audio_info = self.get_audio_info(link)
|
| 138 |
+
if audio_info:
|
| 139 |
+
results.append(audio_info)
|
| 140 |
+
|
| 141 |
+
return results
|
| 142 |
+
|
| 143 |
+
except Exception as e:
|
| 144 |
+
print(f"Error searching by username {username}: {e}")
|
| 145 |
+
return []
|
| 146 |
+
|
| 147 |
+
# Test the scraper
|
| 148 |
+
if __name__ == "__main__":
|
| 149 |
+
scraper = SoundgasmScraper()
|
| 150 |
+
|
| 151 |
+
# Test search
|
| 152 |
+
print("Testing search functionality...")
|
| 153 |
+
results = scraper.search_audio("ASMR", max_results=3)
|
| 154 |
+
|
| 155 |
+
for i, result in enumerate(results, 1):
|
| 156 |
+
print(f"\n--- Result {i} ---")
|
| 157 |
+
print(f"Title: {result['title']}")
|
| 158 |
+
print(f"Audio Title: {result['audio_title']}")
|
| 159 |
+
print(f"Username: {result['username']}")
|
| 160 |
+
print(f"URL: {result['url']}")
|
| 161 |
+
print(f"Audio URL: {result['audio_url']}")
|
| 162 |
+
print(f"Description: {result['description'][:100]}..." if result['description'] else "No description")
|
| 163 |
+
|