forked from yannikism/map-of-knowledge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikipediaArticle.py
214 lines (164 loc) · 6.03 KB
/
WikipediaArticle.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
'''
Requires:
pip install wikipedia
OR
pip install git+https://github.com/lucasdnd/Wikipedia.git
This is backwards COMPATIBLE with versions 4.5.1_main.py to newest
Helpful:
https://stackabuse.com/getting-started-with-pythons-wikipedia-api/
Issues:
- Filter smartly
- By links in summary => parse HTML
- Maybe with "Backlinks" or "links point to this site"
- Let user choose starting node if there is disambiguation -JUP-
- starting article not existent -JUP-
- Is "wikipedia-api" API faster? -NOP-
'''
import wikipedia as wiki
from bs4 import BeautifulSoup
import requests
import urllib.parse
def suggest_article(text):
try:
return wiki.search(text)
except:
return [] #When error is encountered, return nothing
class WikipediaArticle():
"""
-- An Wikipedia Article --
USE LIKE DESCRIBED HERE:
article = WikipediaArticle(article_name)
article.get_wikipedia_object()
#This way access links, links_filtered e.g.
article.page.links_filtered
#and summary
article.summary
article.get_links_in_summary()
#Very fast way to access important links and summary as html
article.links_from_summary
article.summary_html
"""
def __init__(self, page_name, language = "en", is_starting_article = False):
wiki.set_lang(language)
self.page_name = page_name
self.language = language
self.is_starting_article = is_starting_article
#These will be set through 'get_wikipedia_object()'
self.links_filtered = None
self.summary = None
self.page = type('', (), {})() #This somehow just creates an empty object
self.page.links = None
# Access links, references, content, title and url through self.page.links
#These will be set through 'get_links_in_summary()'
self.summary_html = None
self.links_from_summary = None
self.filtered_links_from_summary = None
#Error thrown if article couldn't be found
self.error = False
def get_wikipedia_object(self):
try:
print("[*] TRYING (api): ", self.page_name)
self._set_page()
except wiki.DisambiguationError as e:
best_guess = e.options[1] # Take the first of the options
print("[*] GUESSING: ", best_guess)
self.page_name = best_guess
except wiki.PageError as e:
print("[!] ERROR! Page doesnt exist:", self.page_name)
self.error = True
return
def _set_page(self): # Can be used directly if there is no disambiguation for sure
self.page = wiki.page(self.page_name, auto_suggest = False, preload=False)
self.summary = wiki.summary(self.page_name, auto_suggest = False)
# Auto suggestion causes weird errors. Like "Dog" turning to "Do" in the search
def get_links_in_summary(self): # Via "requests" and HTML parsing
print("[*] page_name: ", self.page_name)
phrase_formatted = self.page_name.replace(" ", "_")
print("[*] phrase_formatted: ", phrase_formatted)
phrase_formatted = urllib.parse.quote(phrase_formatted)
print("[*] phrase_formatted: ", phrase_formatted)
self.url = "https://" + self.language + ".wikipedia.org/wiki/" + phrase_formatted
print("[*] TRYING (html): ", self.url)
try:
raw_html = requests.get(self.url)
except Exception as e:
print("[!] ERROR! request error", e)
return
html = BeautifulSoup(raw_html.text, 'html.parser')
# This is the page in HTML in parseable format
parent = html.find('div', class_ = "mw-parser-output")
if parent == None: #Parent is None if the page isnt found
self.error = True
print("[!] ERROR! Page doesnt exist:", self.page_name)
else:
summary = []
links_filtered = []
reached_summary = False
for child in parent.children:
if child.name == "p" and child.text.strip() != "": #There can be empty <p> tags
reached_summary = True
summary.append(child)
elif reached_summary == True: #Then the end of summary is reached
break
self.summary_html = ""
for part in summary: #Extract links of painfully found <a> tags
self.summary_html += str(part)
links = part.find_all("a")
for link in links:
try:
link_string = link["href"].replace("/wiki/", "")
#This takes the 'href' attribute of the <p> and removes "/wiki/"
#And sometimes it just doesnt work
except KeyError as e:
print("[!] ERROR: " + e)
link_string = urllib.parse.unquote(link_string)
# This will replace %27 with ' and %E2%80%93 with - and so on
if self._is_real_link(link_string) and link_string not in links_filtered:
link_string = link_string.replace("_", " ") # _ represent Spaces in the URLs
links_filtered.append(link_string)
print("[+] success, links found: " + str(len(links_filtered)))
self.links_from_summary = links_filtered
self.summary_text = BeautifulSoup(self.summary_html, 'html.parser').get_text()
def _is_real_link(self, link_string):
if link_string[0:11] == "#cite_note-":
return False
if link_string[0:5] == "Help:":
return False
if link_string[0:5] == "File:":
return False
if link_string[0:2] == "//":
return False
if link_string[0:7] == "http://":
return False
if link_string[0:8] == "https://":
return False
return True
def filter(self, num):
# Filter links
if self.page.links is not None:
self.links_filtered = self.page.links[:num] # Get first ... links
elif self.links_from_summary is not None:
self.filtered_links_from_summary = self.links_from_summary[:num] # Get first ... links
else:
print("[*] Nothing to filter found")
#self.links_filtered = numpy.array(self.page.links)[:num]
# Using numpy should be faster but isnt ... hmmm
def toJSON(self):
json = {
'page_name': self.page_name,
'language': self.language,
#These will be set through 'get_links_in_summary()'
'is_starting_article': self.is_starting_article,
'summary_html': self.summary_html,
'summary_text': self.summary_text,
'links_from_summary': self.links_from_summary,
'filtered_links_from_summary': self.filtered_links_from_summary
}
"""
#These will be set through 'get_wikipedia_object()'
'links_filtered': self.links_filtered,
'summary': self.summary,
'page': self.page,
'page.links': self.page.links,
"""
return json