-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlol_voiceline_scraper.py
190 lines (136 loc) · 4.03 KB
/
lol_voiceline_scraper.py
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
from email.mime import audio
from fileinput import filename
import requests
from bs4 import BeautifulSoup as bs
import os
import subprocess
import wget
import csv
import shutil
champ = 'Yasuo'
url = "https://leagueoflegends.fandom.com/wiki/*-*/LoL/Audio"
seactionsBlacklist = ['AbilityCasting', 'Death']
skinBlacklist = ['Nightbringer']
print("Champion name")
inp = input()
if not inp == '':
champ = inp
url = url.replace('*-*', champ)
request = requests.get(url)
if not request:
print("Didnt get a response")
exit()
html = bs(request.text, 'html.parser')
sections = request.text.split('<h2>')
h2s = html.findAll('h2')
i = 1
for h2 in h2s:
h2 = h2.text.replace(' ', '')
if i >= len(sections):
continue
if h2 in seactionsBlacklist:
sections.pop(i)
continue
if i >= len(sections):
break
sections[i] = '<h2>' + sections[i]
i += 1
html = bs("".join(sections), 'html.parser')
audios = {}
i = 1
for li in html.findAll('li'):
# print(li.find(class_='audio-button'), not li.find(class_='audio=button'))
if not li.find(class_='audio-button'):
continue
span = li.find('span')
if span and not span['data-skin'] == 'Original':
continue
# print(span)
source = li.find('source')
if not source:
continue
source = source['src']
text = li.find('i').text
noSkin = True
for skin in skinBlacklist:
if champ + '_' + skin in source:
noSkin = False
if not noSkin:
continue
if not '"' in text:
continue
text = text.replace('"', '')
audios[source] = text
print(f"{i}. {text} - {source}")
i += 1
download = True
def checkInput(print_=False):
global download
global audios
if print_:
i = 1
for source, name in audios.items():
print(f'{i}. {name} - {source}')
i += 1
print(
f'\nTo download: {len(audios)} files\n------------------------------\n')
print('Type numbers which you would like to remove from the download or hit enter to proceed.')
remove = input()
if remove == '':
return
if remove.startswith('skip'):
download = False
return
if remove.startswith('max='):
max = int(remove[4:])
keys = list(audios.keys())
if max >= len(keys):
checkInput(True)
else:
tmp = {}
for i in range(max):
tmp[keys[i]] = audios[keys[i]]
audios = tmp
else:
remove = sorted(remove.split(), key=int, reverse=True)
for x in remove:
audios.pop(list(audios.keys())[int(x) - 1])
checkInput(True)
checkInput()
path = f'../LJSpeech-1.1/loltts/{champ}/'
tmpPath = path + 'tmp/'
savePath = path + 'wavs/'
os.makedirs(tmpPath, exist_ok=True)
os.makedirs(savePath, exist_ok=True)
num = 1
rows = []
wget.name = champ
for source, text in audios.items():
fileName = champ + "-" + str(num).zfill(3)
wget.number = num
if download:
wget.download(source, tmpPath + fileName + '.ogg')
print(f'\nConverting {fileName}.ogg to {fileName}.wav...', end=' ')
converter = subprocess.Popen(
['ffmpeg', '-i', f'{tmpPath + fileName}.ogg', '-ar', '22050', '-ac', '1', f'{savePath + fileName}.wav', '-y', '-loglevel', '0'], stdout=subprocess.PIPE)
converter.communicate()[0]
print(' COMPLETE')
rows.append([f'{fileName}|{text}|{text}'])
num += 1
num -= 1
if num < 105:
missing = 105 - num
for j in range(1, missing + 1):
fileName = champ + '-' + str(num + j).zfill(3)
print(fileName)
shutil.copy(savePath + champ + '-' + str(j).zfill(3) + '.wav',
savePath + fileName + '.wav')
key = list(audios.keys())[j]
rows.append([f"{fileName}|'{audios[key]}'|'{audios[key]}'"])
csvFile = path + 'metadata.csv'
with open(csvFile, 'w') as f:
writer = csv.writer(f)
writer.writerows(rows)
for f in os.listdir(tmpPath):
os.remove(os.path.join(tmpPath, f))
os.rmdir(tmpPath)