-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_dataset.py
207 lines (162 loc) · 7.12 KB
/
get_dataset.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import os
from pprint import pprint
import json
from pySmartDL import SmartDL
from time import sleep
from selenium.common import exceptions
from pprint import pprint
import chromedriver_autoinstaller
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.common import exceptions
from selenium.webdriver.common.by import By
from pySmartDL import SmartDL
class GetDataURL:
def __init__(self, driver) -> None:
self.driver = driver
def get_all_data(self):
lst = []
lst.extend(self.character_stories())
lst.extend(self.card_stories())
lst.extend(self.area_talk())
lst.extend(self.unit_story())
# lst.extend(self.special_story())
# lst.extend(self.event_story())
return lst
def character_stories(self):
url_lst = []
self.driver.get('https://sekai.best/storyreader/charaStory')
sleep(5)
container = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
for i in container:
if character in i.text:
url = i.find_element(By.TAG_NAME, 'a').get_attribute('href')
url_lst.append(url)
pprint(url_lst)
return url_lst
def card_stories(self):
url_lst = []
self.driver.get('https://sekai.best/storyreader/cardStory')
sleep(10)
container = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
for i in container:
if character in i.text:
i.click()
break
con = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
for j in [i.find_element(By.TAG_NAME, 'a').get_attribute('href') for i in con]:
self.driver.get(j)
sleep(5)
c = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
url_lst.extend([i.find_element(By.TAG_NAME, 'a').get_attribute('href') for i in c])
pprint(url_lst)
return url_lst
def area_talk(self):
url_lst = []
self.driver.get('https://sekai.best/storyreader/areaTalk/5') # MapTalk에서 에어리어 '교실 세카이'
sleep(5)
container = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
for i in container:
for j in i.find_elements(By.TAG_NAME, 'img'):
if char_code in j.get_attribute('src'):
url_lst.append(i.find_elements(By.TAG_NAME, 'a')[-1].get_attribute('href'))
break
pprint(url_lst)
return url_lst
def unit_story(self):
url_lst = []
self.driver.get('https://sekai.best/storyreader/unitStory/light_sound') # Leo/need 스토리
sleep(5)
container = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
for j in [i.find_element(By.TAG_NAME, 'a').get_attribute('href') for i in container]:
self.driver.get(j)
sleep(5)
c = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
url_lst.extend([i.find_element(By.TAG_NAME, 'a').get_attribute('href') for i in c])
pprint(url_lst)
return url_lst
def special_story(self):
url_lst = []
self.driver.get('https://sekai.best/storyreader/specialStory')
sleep(5)
container = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
for j in [i.find_element(By.TAG_NAME, 'a').get_attribute('href') for i in container]:
self.driver.get(j)
sleep(5)
c = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
url_lst.extend([i.find_element(By.TAG_NAME, 'a').get_attribute('href') for i in c])
pprint(url_lst)
return url_lst
def event_story(self):
url_lst = []
self.driver.get('https://sekai.best/storyreader/eventStory')
sleep(10)
container = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
for j in [i.find_element(By.TAG_NAME, 'a').get_attribute('href') for i in container]:
self.driver.get(j)
sleep(5)
try:
c = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
url_lst.extend([i.find_element(By.TAG_NAME, 'a').get_attribute('href') for i in c])
except exceptions.StaleElementReferenceException:
print("StaleElementReferenceException")
pprint(url_lst)
return url_lst
class GetProsekaDataset:
def __init__(self, driver) -> None:
if not os.path.isdir(f'./tts_dataset/{name}/'): os.mkdir(f'./tts_dataset/{name}/')
if not os.path.isdir(f'./tts_dataset/{name}/mp3s/'): os.mkdir(f'./tts_dataset/{name}/mp3s/')
if not os.path.isdir(f'./tts_dataset/{name}/wavs/'): os.mkdir(f'./tts_dataset/{name}/wavs/')
self.metadata = open(f'./tts_dataset/{name}/metadata.txt', 'a', encoding='utf-8')
self.driver = driver
def get_data(self, talk_urls):
for tu in talk_urls:
self.driver.get(tu)
sleep(10)
containers = self.driver.find_elements(By.XPATH, '//*[@id="root"]/div/div[3]/div[2]/div')
for i in containers:
if self.is_character(i):
try:
transcript = i.find_element(By.TAG_NAME, 'p').text.strip()
mp3Link = i.find_element(By.TAG_NAME, 'a').get_attribute('href')
fname = mp3Link.split('/')[-1].replace('.mp3', '.wav')
print(f'{transcript}\n{mp3Link}')
self.download_mp3(mp3Link)
print('\n')
self.metadata.writelines(f'../tts_dataset/{name}/wavs/{fname}|{transcript}\n')
except exceptions.NoSuchElementException:
print("요소가 없습니다!")
def is_character(self, element):
try:
if element.text.strip().startswith(character):
return True
else:
return False
except:
return False
def download_mp3(self, mp3Url):
while True:
try:
obj = SmartDL(mp3Url, f'./tts_dataset/{name}/mp3s/')
obj.start()
path = obj.get_dest()
break
except:
print("1초 후 다시 요청")
sleep(1)
if __name__ == '__main__':
import sys
char_json = json.loads(
open('character_code.json', 'r', encoding='utf-8').read()
)['code'] [int(sys.argv[1])] # Speaker ID
name = char_json['name']
character = char_json['character']
char_code = char_json['char_code']
print(name, character, char_code)
options = Options()
path = chromedriver_autoinstaller.install()
driver = Chrome()
g = GetDataURL(driver)
data = g.get_all_data()
p = GetProsekaDataset(driver)
p.get_data(data)