-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreSearch.py
More file actions
executable file
·243 lines (205 loc) · 7.45 KB
/
Copy pathreSearch.py
File metadata and controls
executable file
·243 lines (205 loc) · 7.45 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!venv/bin/python
# -*- coding: utf-8 -*-
import urllib2
import re
import argparse
import timeit
# for david implementation
import json
import requests
from lxml import html
from bs4 import BeautifulSoup, SoupStrainer
#TODO also for English and other languages
HITLINK = "/wiki/Adolf_Hitler"
class wiki_site():
"""class for searching him"""
def __init__(self, url, **kwargs):
# the url of the current site
self.url = url
self.links = None
self.heading = ''
self.clicked_name = kwargs.get('clicked_name', self.get_heading())
# Text of the site already visited
#self.road = kwargs.get('road', '')
self.road = ''
# how many sites have already been visited
#self.level = level
# how many links have to be clicked
self.distance = None
#
self.country_string = re.search('(?<=https://)\w+(?=\.wikipedia.org)', url).group(0)
def load_soup(self):
if not self.links:
# an urllib2 object of the site
website = urllib2.urlopen(self.url)
# html text of the website
html = website.read()
# BeautifulSoup object that stores site
soup = BeautifulSoup(html, 'lxml')
# Masterpiece...not,
self.links, self.link_names = zip(*[[li.get('href'), ''.join(li.stripped_strings)] for li in soup.findAll('a', attrs={'href': re.compile('^/wiki/(?!\w+:).+$((?<!png)|(?<!jpg))')})])
#self.links = list(set(self.links))
self.heading = ''.join(soup.find(id='firstHeading').stripped_strings)
def get_wiki_links(self):
"""get all internal links"""
self.load_soup()
return self.links
def get_heading(self):
self.load_soup()
return self.heading
#def hit_gen(url, links_gen, **kwargs):
def hit_gen(url, **kwargs):
"""
masterpiece generator, for recursive search for him
args:
url: real Url
kwargs[link_name]: the name the link shown on site
"""
# an wiki_site obj
site = wiki_site(url, **kwargs)
second_part_links = site.get_wiki_links()
link_names = site.link_names
# 2D-list with the links and the link-names
#second_part_links_and_link_names = site.get_wiki_links()
# the first part of the wikipedia url
first_part_links = 'https://' + site.country_string + '.wikipedia.org'
# somebody has entered his site directly
if url == first_part_links + HITLINK:
site.distance = 0
site.road = '"' + site.get_heading() + '". Ziemlich schlau direkt seine Seite einzugeben'
yield [site]
return
# first search on site
print('Searching in ' + url)
for spl, li_name in zip(second_part_links, link_names):
# found him?
if spl == HITLINK:
hit_site = wiki_site(first_part_links + HITLINK, link_name=li_name)
hit_site.get_heading()
hit_site.distance = 0
site.distance = 1
hit_site.road = '"' + hit_site.heading + '". Ziemlich schlau direkt seine Seite einzugeben'
site.road = '"' + site.clicked_name + '" dann "' + hit_site.clicked_name + '"'
yield [site, hit_site]
return
# Nothing found
yield None
print('Going one layer deeper')
# search in all links untill he is found
gen_list = [hit_gen(first_part_links + spl, clicked_name=li_name) for spl, li_name in zip(second_part_links, link_names)]
#print('and here')
tmp_result = None
while tmp_result is None:
for gen in gen_list:
tmp_result = next(gen)
if tmp_result:
site.road = '"' + site.clicked_name + '" dann ' + tmp_result[0].road
site.distance = tmp_result[0].distance + 1
tmp_result.insert(0, site)
yield tmp_result
return
# Nothing found, so ge to next level
yield None
def hit_search(url):
#total_links = links_handle()
#total_links.send(None)
total_hit_generator = hit_gen(mobile_to_desktop(url))
result = None
for res in total_hit_generator:
if res:
result = res
break
return res
def mobile_to_desktop(url):
res = re.search('(https://\w+)\.m(\.wikipedia.org.*)', url)
return res.group(1) + res.group(2) if res else url
def main():
parser = my_parser()
args = parser.parse_args()
url4 = 'https://de.wikipedia.org/wiki/Hambach_an_der_Weinstra%C3%9Fe'
url2 = 'https://de.wikipedia.org/wiki/Stra%C3%9Fburg'
url3 = 'https://de.wikipedia.org/wiki/Neustadt_an_der_Weinstra%C3%9Fe'
url1 = 'https://de.wikipedia.org/wiki/Wasserstoff'
url = 'https://de.wikipedia.org/wiki/Ettore_Majorana'
res = hit_search(args.url if args.url else url)
if res:
print('Ich habe ihn gefunden. Klicke ' + res[0].road)
def my_parser():
parser = argparse.ArgumentParser()
parser.add_argument('url', type=str,
help=('The wikipedia url to search from,'
+ 'right now only german'))
return parser
#if __name__ == '__main__':
# start = timeit.default_timer()
# main()
# end = timeit.default_timer()
# print(end - start)
#HOST = 'https://de.wikipedia.org'
#REGEX = re.compile('/wiki/(?!\w+:).+$((?<!png)|(?<!jpg))')
CACHE = {}
class cachedSearch:
"""cc David Lukwata"""
def __init__(self, url, **kwargs):
self.url = url
self.stop = kwargs.get('stop', '/wiki/Adolf_Hitler')
self.host = self.getHost()
self.regex = re.compile('/wiki/(?!\w+:).+$((?<!png)|(?<!jpg))')
self.result = ()
def getPage(self, path):
print(path + '...')
r = requests.get(self.host + path)
return r.content
def getLinks(self, path):
if path in CACHE:
print(path + '... (cached)')
return CACHE[path]
tree = html.fromstring(self.getPage(path))
links = tree.xpath('//div[@role="main"]//a/@href')
links = filter(unicode, links)
links = filter(self.regex.match, links)
CACHE[path] = links
return links
def search(self, start):
"""
args:
start url to start search from
stop url to which to find
"""
start = self.getWiki(start)
fifo = [(start,)]
for each in fifo:
# retrieve links from page
links = self.getLinks(each[-1])
for path in links:
# check for him
if path == self.stop:
self.result = each + (self.stop,)
# add path to fifo
new = each + (path,)
fifo.append(new)
def getHost(self):
regexSearch = re.search('https://\w+\.wikipedia.org', self.url)
return regexSearch.group(0)
def getWiki(self, url):
#print(self.host)
regexSearch = re.search(self.host + '(.*)', url)
return regexSearch.group(1)
def getResult(self):
print(self.result)
if __name__ == '__main__':
parser = my_parser()
args = parser.parse_args()
try:
with open('cache', 'rb') as file:
CACHE = json.load(file)
except IOError as e:
print('Warning: Konnte Cache nicht lesen')
#host = getHost(args.url)
cSearch = cachedSearch(args.url)
print(cSearch.getWiki(args.url))
cSearch.search(args.url)
print(cSearch.getResult())
#print(getHost(args.url))
with open('cache', 'wb') as file:
json.dump(CACHE, file)