-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage_crawler.py
251 lines (174 loc) · 7.32 KB
/
page_crawler.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from pyquery import PyQuery as pq
import re
import page_gatherer
from utils import print_dot, get_number
class CharacterWikiCrawler:
def __init__(self, subdomain: str, character, category: str):
self.character = character
self.category = category
self.base_url = f"https://{subdomain}.fandom.com"
def get_glossary(self):
return page_gatherer.v1(self.base_url, self.category)
def crawl(self):
pass
def get_pages(self):
return self.crawl()
class TokyoGhoulWikiCrawler(CharacterWikiCrawler):
def __init__(self, subdomain, character, category):
CharacterWikiCrawler.__init__(self, subdomain, character, category)
self.glossary = self.get_glossary()
def crawl(self):
pages = []
count = 1
print(f"Crawling pages for {self.character}")
for chapter in self.glossary:
print_dot(count, 50)
q = pq(url=chapter)
links = q("#Characters").parent().next().children()
for link in links:
children = link.getchildren()
num_children = len(children)
if num_children < 1:
continue
if self.character in children[0].text:
if len(children) > 1:
pages.append([chapter, children[1].text])
else:
pages.append([chapter, "(Appears)"])
count += 1
print("\nDone crawling.")
return pages
def get_pages(self):
pages = self.crawl()
cleaned_pages = []
re_cleaned_pages = []
for page in pages:
split_page = page[0].split(f"{self.base_url}/wiki/")
if len(split_page) != 2:
cleaned_pages.append(page)
continue
if split_page[1].startswith("Re:"):
re_cleaned_pages.append(f"{split_page[1].replace('_', ' ')} {page[1]}: {page[0]}")
else:
cleaned_pages.append(f"{split_page[1].replace('_', ' ')} {page[1]}: {page[0]}")
return sorted(cleaned_pages, key=get_number) + sorted(re_cleaned_pages, key=get_number)
class NarutoWikiCrawler(CharacterWikiCrawler):
def __init__(self, subdomain, character, category):
CharacterWikiCrawler.__init__(self, subdomain, character, category)
self.glossary = self.get_glossary()
def crawl(self):
pages = []
count = 1
print(f"Crawling pages for {self.character}")
for chapter in self.glossary:
print_dot(count, 50)
q = pq(url=chapter)
links = q("b a")
for link in links:
if link.text and self.character in link.text:
text = q("div.mw-parser-output").text()
matches = re.search("episode \d+ of the .+ anime", text)
pages.append([chapter, matches.group() if matches is not None else "Could not find episode number"])
break
count += 1
print("\nDone crawling.")
return pages
def get_pages(self):
pages = self.crawl()
original_cleaned_pages = []
shippuden_cleaned_pages = []
other_cleaned_pages = []
for page in pages:
if "of the original Naruto anime" in page[1]:
original_cleaned_pages.append(f"{page[1]}: {page[0]}")
elif "of the Naruto: Shippūden anime" in page[1]:
shippuden_cleaned_pages.append(f"{page[1]}: {page[0]}")
else:
other_cleaned_pages.append(f"{page[1]}: {page[0]}")
return sorted(original_cleaned_pages, key=get_number) + sorted(shippuden_cleaned_pages, key=get_number) + sorted(other_cleaned_pages, key=get_number)
class JJKWikiCrawler(CharacterWikiCrawler):
def __init__(self, subdomain, character, category):
CharacterWikiCrawler.__init__(self, subdomain, character, category)
self.glossary = self.get_glossary()
def crawl(self):
pages = []
count = 1
print(f"Crawling pages for {self.character}")
for chapter in self.glossary:
print_dot(count, 50)
q = pq(url=chapter)
links = q("div.mw-parser-output a")
for link in links:
if link.text and self.character == link.text.strip():
pages.append(chapter)
break
count += 1
print("\nDone crawling.")
return pages
def get_pages(self):
pages = self.crawl()
cleaned_pages = []
for page in pages:
split_page = page.split(f"{self.base_url}/wiki/")
if len(split_page) != 2:
cleaned_pages.append(page)
continue
cleaned_pages.append(f"{split_page[1].replace('_', ' ')}: {page}")
return sorted(cleaned_pages, key=get_number)
class DungeonMeshiWikiCrawler(CharacterWikiCrawler):
def __init__(self, subdomain, character, category):
if "," in character:
character_list = [c.strip() for c in character.split(",")]
CharacterWikiCrawler.__init__(self, subdomain, character_list, category)
else:
CharacterWikiCrawler.__init__(self, subdomain, character, category)
self.glossary = self.get_glossary()
def crawl(self):
pages = []
count = 1
print(f"Crawling pages for {self.character}")
for chapter in self.glossary:
print_dot(count, 50)
q = pq(url=chapter)
found = False
links = q("#mw-content-text > div > ul > li")
for link in links:
children = link.getchildren()
for child in children:
text = child.text
if text and text.strip() in self.character:
pages.append(chapter)
found = True
break
if not found:
links = q("#mw-content-text > div > table.article-table > tbody > tr > td > ul > li")
for link in links:
children = link.getchildren()
for child in children:
text = child.text
if text and text.strip() in self.character:
pages.append(chapter)
found = True
break
if not found:
links = q("#mw-content-text > div > table > tbody > tr > td > ul > li")
for link in links:
children = link.getchildren()
for child in children:
text = child.text
if text and text.strip() in self.character:
pages.append(chapter)
break
count += 1
print("\nDone crawling.")
return pages
def get_pages(self):
pages = self.crawl()
cleaned_pages = []
for page in pages:
split_page = page.split(f"{self.base_url}/wiki/")
if len(split_page) != 2:
cleaned_pages.append(page)
continue
cleaned_pages.append(f"{split_page[1].replace('_', ' ')}: {page}")
return sorted(cleaned_pages, key=get_number)