Repository navigation
Expand file tree
/
Copy pathscraper.py
More file actions
220 lines (183 loc) · 7.82 KB
/
Copy pathscraper.py
File metadata and controls
220 lines (183 loc) · 7.82 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
"""
WoW Sounds Scraper - Downloads sounds from Wowhead organized by category
For personal music production use only.
"""
import requests
import json
import os
import time
import re
from pathlib import Path
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError
# Wowhead base URLs
BASE_URL = "https://www.wowhead.com"
SOUND_URL_BASE = "https://wow.zamimg.com"
# Output directory
OUTPUT_DIR = Path("./sounds")
DATA_DIR = Path("./data")
def get_sound_types():
"""Fetch available sound categories from Wowhead"""
# Complete list of all 28 categories from Wowhead
return {
1: "Spells",
2: "User Interface",
3: "Footsteps",
4: "Weapons Impacts",
6: "Weapons Misses",
9: "Pick Up/Put Down",
10: "NPC Combat",
12: "Errors",
13: "Nature",
14: "Objects",
16: "Death",
17: "NPC Greetings",
19: "Armor",
20: "Footstep Splash",
21: "Water (Character)",
22: "Water",
23: "Tradeskills",
24: "Misc Ambience",
25: "Doodads",
26: "Spell Fizzle",
27: "NPC Loops",
28: "Zone Music",
29: "Emotes",
30: "Narration Music",
31: "Narration",
50: "Zone Ambience",
52: "Emitters",
53: "Vehicles"
}
def extract_sounds_with_browser(max_sounds=100):
"""Use Playwright to load Wowhead and extract sound data"""
print("Launching browser to extract sound data...")
DATA_DIR.mkdir(exist_ok=True)
sounds_data = []
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
try:
print(f"Loading Wowhead sounds page...")
page.goto(f"{BASE_URL}/sounds", wait_until="networkidle", timeout=30000)
# Wait for the listview to load
print("Waiting for sound list to load...")
page.wait_for_selector(".listview-row", timeout=10000)
# Scroll to load more sounds (lazy loading)
print("Scrolling to load sounds...")
for i in range(5): # Scroll a few times to load more
page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
time.sleep(1)
# Extract sound data from the page
print("Extracting sound data from page...")
# Get all listview rows
rows = page.query_selector_all(".listview-row")
print(f"Found {len(rows)} sound rows on initial page")
for row in rows[:max_sounds]:
try:
# Extract sound ID from the link
link = row.query_selector("a[href*='sound=']")
if link:
href = link.get_attribute("href")
sound_id_match = re.search(r'sound=(\d+)', href)
if sound_id_match:
sound_id = sound_id_match.group(1)
name = link.inner_text().strip()
# Try to find the audio player for this row
audio_controls = row.query_selector(".audio-controls-wrapper")
# Try to extract category/type from the row
# Look for type column or icon that indicates category
category = "Uncategorized"
try:
# Try to get all cells in the row
cells = row.query_selector_all("td")
if len(cells) >= 2:
# Type is usually in the second column
type_text = cells[1].inner_text().strip()
if type_text and type_text != "":
category = type_text
except:
pass
sounds_data.append({
"id": sound_id,
"name": name,
"category": category,
"has_audio": audio_controls is not None
})
if len(sounds_data) % 10 == 0:
print(f" Extracted {len(sounds_data)} sounds...")
except Exception as e:
print(f" Error extracting row: {e}")
continue
print(f"\nExtracted {len(sounds_data)} sounds from page")
except PlaywrightTimeoutError:
print("Timeout loading page")
except Exception as e:
print(f"Error: {e}")
finally:
browser.close()
# Save extracted data
if sounds_data:
output_file = DATA_DIR / "extracted_sounds.json"
with open(output_file, 'w') as f:
json.dump(sounds_data, f, indent=2)
print(f"Saved {len(sounds_data)} sound entries to {output_file}")
return sounds_data
def test_sound_download_urls(sound_data):
"""Test different URL patterns to find working download URLs"""
print("\nTesting sound download URL patterns...")
# Test with first few sounds
test_sounds = sound_data[:5]
url_patterns = [
lambda sid: f"https://wow.zamimg.com/wowhead/sounds/live/{sid}.ogg",
lambda sid: f"https://wow.zamimg.com/sounds/{sid}.ogg",
lambda sid: f"https://wow.zamimg.com/wowhead/sound/{sid}.mp3",
]
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
}
for sound in test_sounds:
sound_id = sound['id']
print(f"\nTesting sound ID {sound_id}: {sound['name']}")
for i, pattern_func in enumerate(url_patterns):
url = pattern_func(sound_id)
try:
response = requests.head(url, headers=headers, timeout=5)
print(f" Pattern {i+1}: {response.status_code} - {url}")
if response.status_code == 200:
content_type = response.headers.get('content-type', '')
if 'audio' in content_type:
print(f" ✓ SUCCESS! Found working pattern")
return pattern_func
except Exception as e:
print(f" Pattern {i+1}: Error - {url}")
print("\nNo working URL pattern found. Sounds may not be directly downloadable.")
return None
def main():
print("WoW Sounds Scraper v2.0")
print("=" * 50)
print("\nThis will download sounds from Wowhead for personal use.")
print("Using browser automation to extract sound data.\n")
OUTPUT_DIR.mkdir(exist_ok=True)
# Step 1: Extract sound data using browser automation
print("Step 1: Extracting sound data from Wowhead...")
sounds_data = extract_sounds_with_browser(max_sounds=50) # Start with 50 for testing
if not sounds_data:
print("Failed to extract sound data. Exiting.")
return
print(f"\nSuccessfully extracted {len(sounds_data)} sounds")
# Step 2: Test download URL patterns
print("\nStep 2: Testing download URL patterns...")
working_pattern = test_sound_download_urls(sounds_data)
if working_pattern:
print("\n✓ Found working download pattern!")
print("Ready to proceed with bulk download.")
else:
print("\n⚠ Could not find direct download URLs.")
print("Wowhead may require browser-based downloading or additional authentication.")
print("\nNext steps:")
print("1. Investigate Wowhead's audio player implementation")
print("2. Check browser DevTools Network tab for actual audio file requests")
print("3. May need to extract fileDataID and construct URLs differently")
if __name__ == "__main__":
main()