-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieJar.py
358 lines (302 loc) · 9.87 KB
/
CookieJar.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from lxml import html
import requests
from urllib.parse import urlparse
from urllib.parse import urljoin
import urllib
import sqlite3
import sys
import time
import re
import string
import os.path
_user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0.4'
class LinkExtractor(object):
htmltext = ''
url = ''
links = []
images = []
cookiestr=''
filter=''
def __init__(self, url, cookiestr='', filter=''):
self.links = []
self.url = url
self.cookiestr = cookiestr
self.filter = filter
def load_links(self):
self.links = []
if(len(self.cookiestr)>0):
headers = {'Cookie': self.cookiestr, 'User-Agent': _user_agent}
page = requests.get(self.url, headers = headers)
else:
headers = {'User-Agent': _user_agent}
page = requests.get(self.url, headers = headers)
self.htmltext = page.text
tree = html.fromstring(self.htmltext)
if(len(self.filter)==0):
self.links = tree.xpath('//a/@href')
else:
filterparts = self.filter.split('|')
for fl in filterparts:
self.links.extend(tree.xpath('//a[contains(@href,\''+fl+'\')]/@href'))
for i, s in enumerate(self.links):
self.links[i] = urljoin(self.url, self.links[i])
def load_images(self):
self.images = []
if(len(self.cookiestr)>0):
headers = {'Cookie': self.cookiestr, 'User-Agent': _user_agent}
page = requests.get(self.url, headers = headers)
else:
headers = {'User-Agent': _user_agent}
page = requests.get(self.url, headers = headers)
self.htmltext = page.text
tree = html.fromstring(self.htmltext)
if(len(self.filter)==0):
self.links = tree.xpath('//img/@src')
else:
filterparts = self.filter.split('|')
for fl in filterparts:
self.links.extend(tree.xpath('//a[contains(@href,\''+fl+'\')]/@href'))
for i, s in enumerate(self.images):
self.images[i] = urljoin(self.url, self.images[i])
def save_links(self, filename):
f = open(filename, 'w')
for link in self.links:
f.write(link + '\n')
f.close()
class CookieUtils(object):
url = ''
hostchecks = []
cookie_header = ''
cookieheader = ''
cookiefile = ''
def __init__(self, url, cookiefile):
self.url = url
self.hostchecks = []
self.cookiefile = cookiefile
if(self.cookiefile):
self.extract_cookie_checks()
self.load_cookies()
def extract_cookie_checks(self):
o = urlparse(self.url)
hostname = o.hostname;
hostparts = hostname.split('.')
tmp=''
self.hostchecks.append(hostname)
self.hostchecks.append('.'+hostname)
if(len(hostparts)>1):
tmp = hostparts[len(hostparts)-2]+'.'+hostparts[len(hostparts)-1]
if tmp not in self.hostchecks:
self.hostchecks.append(tmp)
if '.'+tmp not in self.hostchecks:
self.hostchecks.append('.'+tmp)
if(len(hostparts)>2):
tmp = hostparts[len(hostparts)-3]+'.'+hostparts[len(hostparts)-2]+'.'+hostparts[len(hostparts)-1]
if tmp not in self.hostchecks:
self.hostchecks.append(tmp)
if '.'+tmp not in self.hostchecks:
self.hostchecks.append('.'+tmp)
if(len(hostparts)>3):
tmp = hostparts[len(hostparts)-4]+'.'+hostparts[len(hostparts)-3]+'.'+hostparts[len(hostparts)-2]+'.'+hostparts[len(hostparts)-1]
if tmp not in self.hostchecks:
self.hostchecks.append(tmp)
if '.'+tmp not in self.hostchecks:
self.hostchecks.append('.'+tmp)
if(len(hostparts)>4):
tmp = hostparts[len(hostparts)-5]+'.'+hostparts[len(hostparts)-4]+'.'+hostparts[len(hostparts)-3]+'.'+hostparts[len(hostparts)-2]+'.'+hostparts[len(hostparts)-1]
if tmp not in self.hostchecks:
self.hostchecks.append(tmp)
if '.'+tmp not in self.hostchecks:
self.hostchecks.append('.'+tmp)
def load_cookies(self):
o = urlparse(self.url)
hostcount = len(self.hostchecks)
isfirst = True
if(self.cookiefile.endswith('cookies.sqlite')):
query = 'SELECT name, value FROM moz_cookies WHERE ('
for host in self.hostchecks:
if isfirst == True:
query = query + 'host=? '
isfirst = False
else:
query = query + 'OR host=? '
if o.scheme=='http':
query = query + ') AND (path=? OR path=\'/\') AND isSecure=0;'
elif o.scheme=='https':
query = query + ') AND (path=? OR path=\'/\');'
sqlParams = self.hostchecks
sqlParams.append(o.path)
conn = sqlite3.connect(self.cookiefile)
c = conn.cursor()
c.execute(query, sqlParams)
self.cookieheader = ''
for row in c:
self.cookieheader = self.cookieheader + row[0] + '=' + row[1] + '; '
conn.close()
elif(self.cookiefile.endswith('Cookies')):
query = 'SELECT name, value FROM cookies WHERE ('
for host in self.hostchecks:
if isfirst == True:
query = query + 'host_key=? '
isfirst = False
else:
query = query + 'OR host_key=? '
if o.scheme=='http':
query = query + ') AND (path=? OR path=\'/\') AND secure=0;'
elif o.scheme=='https':
query = query + ') AND (path=? OR path=\'/\');'
sqlParams = self.hostchecks
sqlParams.append(o.path)
conn = sqlite3.connect(self.cookiefile)
c = conn.cursor()
c.execute(query, sqlParams)
self.cookieheader = ''
for row in c:
self.cookieheader = self.cookieheader + row[0] + '=' + row[1] + '; '
conn.close()
self.cookieheader = self.cookieheader.strip()
class Downloader(object):
def __init__(self):#, url, filename=''):
i=0
def download(self, url, filename=''):
if(len(filename)>0):
local_filename = filename
else:
local_filename = url.split('/')[-1]
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=32768):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
print('#',end="",flush=True)
f.close()
return local_filename
def downloadFile(self, url, directory, cookieheader=''):
r = requests.head(url, allow_redirects=True)
headers = {}
if(len(cookieheader)>0):
headers = {'Cookie': cookieheader, 'User-Agent': _user_agent}
else:
headers = {'User-Agent': _user_agent}
dl = 0
start = time.clock()
r = requests.get(url, stream=True, headers = headers)
contentdisposotion = r.headers.get('content-disposition')
if contentdisposotion is None:
filename = url.split('/')[-1].split('#')[0].split('?')[0]
else:
matches=re.findall(r'\"(.+?)\"',contentdisposotion)
if(len(matches)>0) and (contentdisposotion.find('attachment') != -1):
filename = matches[0]
else:
filename = url.split('/')[-1].split('#')[0].split('?')[0]
total_length = r.headers.get('content-length')
filesizestr = ''
if(total_length is not None):
filesizestr = ' / Size: ' + total_length
localFilename = filename
if(os.path.isfile(directory + '/' + localFilename)):
return -1
print('File: ' + localFilename + filesizestr)
with open(directory + '/' + localFilename, 'wb') as f:
if total_length is None: # no content length header
f.write(r.content)
else:
filelen = int(total_length)
for chunk in r.iter_content(32768):
dl += len(chunk)
f.write(chunk)
done = int(50 * dl / filelen)
sys.stdout.write("\r[%s%s] %s kb/s" % ('=' * done, ' ' * (50-done), (dl /1024) //(time.clock() - start)))
print('')
return (time.clock() - start)
class TSectionMatch(object):
StartNum=0
EndNum=0
Increment=1
UrlCount=0
IsFilled=False
SectionText=''
MinIntegerWidth=0
IsLeadingZeroes=False
def __init__(self):
self.EndNum = 0
class SectionExpander(object):
strs=[]
url=''
def __init__(self, url):
self.url=url
def RegexpGetMatchCount(self, s, pattern):
return len(re.findall(pattern, s))
def GetSectionCount(self, s):
return self.RegexpGetMatchCount(s, '\{([0-9]*)\-([0-9]*)\ *\,([0-9]*)\ *\}')
def StringContainsSection(self, s):
return len(re.findall('\{([0-9]*)\-([0-9]*)\ *\,([0-9]*)\ *\}', s))>0
def GetSectionFromString(self, s, i):
matches = re.findall('\{([0-9]*)\-([0-9]*)\ *\,([0-9]*)\ *\}', s)
matcheslit = re.findall('\{[0-9]*\-[0-9]*\ *\,[0-9]*\ *\}', s)
if len(matches)>i:
start = int(matches[i][0])
end = int(matches[i][1])
inc = int(matches[i][2])
m = TSectionMatch()
m.StartNum = start
m.EndNum = end
m.Increment = inc
m.MinIntegerWidth = len(matches[i][0])
m.IsLeadingZeroes = (matches[i][0][0] == '0')
m.SectionText = matcheslit[0]
m.UrlCount = int(int(m.EndNum - m.StartNum) / inc) + 1
m.IsFilled=True;
return m
else:
return None
def GetTotalExpansionSize(self, url):
c=self.GetSectionCount(url)
total=1
for index in range(c):
sect=self.GetSectionFromString(url, index)
if sect is not None:
total=total*sect.UrlCount
else:
return total
return total
def ExpandFirstSection(self, formatted_urltext, urlHolder, sect):
i=0
if sect.IsFilled:
for index in range(sect.UrlCount):
urlHolder.append(formatted_urltext.replace('%d', str(sect.StartNum + (i*sect.Increment)) ))
return urlHolder
def ReplaceStrings(self, urls, OldPattern, NewPattern):
for index in range(len(urls)):
urls[index]=urls[index].replace(OldPattern, NewPattern,1)
def PadInt(self, i, minwidth):
s=str(i);
if len(s) >= minwidth:
return s
else:
n=minwidth-len(s)
for i in range(n):
s='0'+s
return s
def ExpandSections(self, urls, sect):
urlHolder = []
if sect.IsFilled:
if(sect.IsLeadingZeroes):
self.ReplaceStrings(urls, sect.SectionText, '%s');
else:
self.ReplaceStrings(urls, sect.SectionText, '%d');
for j in range(len(urls)):
for i in range(sect.UrlCount):
if(sect.IsLeadingZeroes==True):
urlHolder.append(urls[j].replace('%s', self.PadInt( sect.StartNum + (i*sect.Increment), sect.MinIntegerWidth) ))
else:
urlHolder.append(urls[j].replace('%d', str(sect.StartNum + (i*sect.Increment) ) ))
return urlHolder
def TotallyExpandString(self):
self.strs = []
self.strs.append(self.url)
#sect = GetSectionFromString(url, 0)
while(self.GetSectionFromString(self.strs[0], 0) is not None):
sect = self.GetSectionFromString(self.strs[0], 0)
self.strs = self.ExpandSections(self.strs, sect)