-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawl.py
More file actions
executable file
·203 lines (188 loc) · 6.06 KB
/
Copy pathcrawl.py
File metadata and controls
executable file
·203 lines (188 loc) · 6.06 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
#!/usr/bin/env python
import HTMLParser
import urllib
import sys
from parser import *
import threading
import time
class writer_wrapper(object):
def __init__(self,dest=sys.stdout):
self.lock = threading.Lock()
if type(dest) == type(''):
self.dest = open(dest,'w')
else: self.dest = dest
def write(self,msg):
with self.lock:
self.dest.write(msg)
def close(self):
with self.lock:
self.dest.close()
class UrlPool(object):
def __init__(self):
self.lock =threading.Lock()
self.urlpool = []
self.urldone = {}
self.urlworking = {}
def next(self):
while True:
with self.lock:
if len(self.urlpool) ==0 and len(self.urlworking) == 0:
raise StopIteration
if len(self.urlpool) > 0:
url = self.urlpool.pop()
self.urlworking[url] = 0
return url
time.sleep(1)
def __iter__(self):
return self
def done(self,url):
with self.lock:
if url in self.urlworking:
del self.urlworking[url]
self.urldone[url]=0
def addUrl(self,url):
with self.lock:
if url not in self.urlworking and\
url not in self.urldone:
self.urlpool.append(url)
def reStack(self,url):
with self.lock:
self.urlpool.append(url)
del self.urlworking[url]
def checkpoint(self):
with self.lock:
print "Writing CHECKPOINT"
with open("checkpoint","w") as f:
print >> f , "#DONE"
for done in self.urldone:
print >> f, done
print >> f,"#POOL"
for p in self.urlpool:
print >> f, p
print >> f,"#WORKING"
for w in self.urlworking:
print >>f, w
print "CHECKPOINT written"
def readcheckpoint(self):
with open("checkpoint","r") as f:
print "Reading CHECKPOINT"
u = None
for line in f:
line = line.strip()
if line.startswith("#DONE"):
u = self.urldone
elif line.startswith("#POOL"):
u = self.urlpool
elif line.startswith("#WORKING"):
u = self.urlpool
else:
if u:
if type(u) == type([]):
u.append(line)
else:
u[line] = 0
class crawler:
def __init__(self,seed,accepted_pattern,MAX_THREADS=1,checkpoint = False,checkpoint_freq=60):
self.accepted=accepted_pattern
self.seed = seed
self.pool = UrlPool()
self.pool.addUrl(self.seed)
self.MAX_THREADS = MAX_THREADS
self.checkpoint_freq = checkpoint_freq
if checkpoint:
self.pool.readcheckpoint()
def run(self,writer):
for url in self.pool:
try:
parser = ParseMSDN("https://msdn.microsoft.com")
#writer.write("visiting "+ url+"\n")
parser.feed( urllib.urlopen(url).read())
if parser.isCode:
module = parser.dll.lower()
if module.find(".dll") != -1:
module=module[:module.find(".dll")]
else: module = "None"
msg = ""
msg += "{} {} {}.{}".format(parser.ret,parser.conv,module,parser.name)
msg += "("
for i,a in enumerate(parser.arguments):
if i>0:
msg += ","
msg += "{}".format(a)
msg +=")\n"
for v in parser.var:
msg += "{} {} {}.{}".format(parser.ret,parser.conv,module,v)
msg += "("
for i,a in enumerate(parser.arguments):
if i>0:
msg += ","
msg += "{}".format(a)
msg += ")\n"
writer.write(msg)
parser.close()
self.pool.done(url)
for item in parser.links:
if re.match(self.accepted,item) and item.find("newlocale=") == -1:
self.pool.addUrl( item )
except IOError as e:
print >>sys.stderr, "Error1 {} {}".format(e,url)
self.pool.reStack(url)
except TypeError as e:
print >> sys.stderr, "Error {}".format(e)
print >> sys.stderr, url
self.pool.done(url)
except UnicodeDecodeError as e:
print >> sys.stderr, "Error {}".format(e)
print >> sys.stderr, url
self.pool.done(url)
except Exception as e:
print >> sys.stderr, "Error {} {}".format(e,type(e))
def pool_write(self):
time.sleep(60)
while len(self.pool.urlpool) > 0:
self.pool.checkpoint()
time.sleep(self.checkpoint_freq)
def start(self,writer):
t = []
for i in range(0,self.MAX_THREADS):
d = threading.Thread(target=self.run,args=(writer,) )
t.append( d )
d.start()
w = threading.Thread(target=self.pool_write,args=())
w.start()
for i in t:
i.join()
import ConfigParser
def main():
Config = ConfigParser.ConfigParser()
Config.read("config")
options = Config.options("parameters")
checkpoint_freq = 60 #Save checkpoint every 60 seconds
for o in options:
if o.upper() == "MAX_THREADS":
MAX_THREADS = int(Config.get("parameters",o))
elif o.upper() == "CHECKPOINT_FREQ":
checkpoint_freq = int(Config.get("parameters",o))
'''
parser = ParseMSDN()
url="https://msdn.microsoft.com/en-us/library/windows/desktop/bg126469(v=vs.85).aspx"
#url="https://msdn.microsoft.com/en-us/library/windows/desktop/dd239108(v=vs.85).aspx"
parser.feed( urllib.urlopen(url).read())
parser.close()
#for item in parser.links:
# if re.match(".*en-us/library/windows/desktop.*",item):
# print item
url="https://msdn.microsoft.com/en-us/library/windows/desktop/bg126469(v=vs.85).aspx"
'''
url="https://msdn.microsoft.com/en-us/library/windows/desktop/bg126469(v=vs.85).aspx"
url="https://msdn.microsoft.com/en-us/library/windows/desktop/dd239108(v=vs.85).aspx"
url = "https://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v=vs.85).aspx"
url = "https://msdn.microsoft.com/en-us/library/windows/desktop/ms724397(v=vs.85).aspx"
checkpoint = False
if len(sys.argv) > 1 :
checkpoint = True
c = crawler(url,".*msdn.*en-us/library/windows/desktop.*",MAX_THREADS,checkpoint,checkpoint_freq)
writer = writer_wrapper("names.txt")
c.start(writer)
if __name__ == "__main__":
main()