-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebCr1.py
132 lines (97 loc) · 2.79 KB
/
WebCr1.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
import RIW # RIW.py
import index # index.py
import os
import sys
# from bs4 import BeautifulSoup
from urllib.request import urlopen # python 3!
class Queue:
def __init__(self):
self.items = []
def __contains__(self, key):
return key in self.items
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.insert(0, item)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
pathToStore = "storage"
forbidden = ['/', '\\', ':', '*', '?', '"', '<', '>', '|']
def main(argv): # {
q = Queue()
storedPages = []
if len(argv) > 0:
if isinstance(argv, list):
for a in argv:
q.push(a)
else:
q.push(argv)
else:
q.push("https://en.wikipedia.org/wiki/String_theory")
# q.push("http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array")
while len(storedPages) < 100 and not q.isEmpty(): # {
url = q.pop()
if url in storedPages:
continue
urlLen = len(url)
protocol = ''
if ':' in url:
protocol = url.split(':', 1)[0]
else:
continue
if not protocol or 'http' not in protocol:
continue
try:
page = RIW.main(url)[0] # get an parse the page with own module "./RIW.py"
except:
e = sys.exc_info()
type = e[0]
msg = e[1]
print(type)
print(msg)
continue
urlContent = page["content"].decode()
urlText = page["text"].decode()
urlLinks = page["a"]
for link in urlLinks:
if link not in storedPages and link not in q:
q.push(link)
if url[urlLen - 5:urlLen] != '.html':
url += '.html'
path_file = url.split('//', 2)
path_file = path_file[1] if len(path_file) == 2 else path_file[0]
indexOfFilenameStart = len(path_file) - 1 - path_file[::-1].index(
'/') + 1 # last index of '/' + 1 is the start of the filename
path_folder = pathToStore + '/' + protocol + '/' + path_file[:indexOfFilenameStart - 1]
filename = path_file[indexOfFilenameStart:]
for f in forbidden:
if f in filename:
continue
if not os.path.exists(path_folder):
os.makedirs(path_folder)
path_file = path_folder + '/' + filename
file_page = open(path_file, mode='w', encoding='utf-8')
file_page.write(urlContent)
file_page.close()
file_page = open(path_file[:len(path_file)-4]+'txt', mode='w', encoding='utf-8')
file_page.write(urlText)
file_page.close()
if os.path.exists(path_file) and os.path.exists(path_file[:len(path_file)-4]+'txt'):
storedPages.append(url[:urlLen])
print(len(storedPages))
# } while not empty or 100
# call index.py to index stored pages "./storage"
index.main(pathToStore) # index.py for downloaded content to be searchable
# } main
if __name__ == "__main__":
main(sys.argv[1:])
"""
./storage
/host
/./.
/./address.
/./.
/filename.html
"""