-
Notifications
You must be signed in to change notification settings - Fork 0
/
downloader.py
executable file
·460 lines (408 loc) · 20.3 KB
/
downloader.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import urllib.parse,urllib.request,urllib3.exceptions
import shutil,os,configparser,logging,sys,requests
import downloaderConfig
from bs4 import BeautifulSoup
from datetime import datetime
from yattag import Doc, indent
from PIL import Image
urlsCache = set()
logger = logging.getLogger('debug')
accesslogger = logging.getLogger('access')
programTime = ''
outputFolder = ''
def downloadRequests3(url):
if (url in urlsCache):
logger.info("Cache Hit for: "+url)
return None
else:
urlsCache.add(url)
logger.info("urlsCache updated, current size: "+str(len(urlsCache)))
try:
pages() # log a counter of a page being requested
r = requests.get(url.strip())
printAccessLog(str(r.status_code), r.headers, url)
if r.status_code == requests.codes.ok:
return r.text
else:
logger.error("error response code: "+str(r.status_code)+" for URL: "+url)
return None
except requests.exceptions.ConnectionError as e1:
logger.error("A ConnectionError occurred while requesting "+url)
logError(e1)
except requests.exceptions.TooManyRedirects as e2:
logger.error("A TooManyRedirects occurred while requesting "+url)
logError(e2)
def downloadRequestsImage3(imgUrl, refererUrl, outputConfig):
""" Downloads a single image using the Requests package
"""
if (imgUrl in urlsCache):
logger.info("Cache Hit for: "+imgUrl)
return None
else:
urlsCache.add(imgUrl)
logger.info("urlsCache updated, current size: "+str(len(urlsCache)))
if imgUrl != None:
file_name = buildLocalFilename(imgUrl)
if file_name != "":
# retries = urllib3.util.retry.Retry(connect=5, read=2, redirect=5) # todo: fix hardcoding
# req = urllib3.PoolManager(retries=retries)
downloads() # log a counter of a download being made
headers = {'user-agent': downloaderConfig.getUserAgent(), 'referer': refererUrl}
try:
resp = requests.get(imgUrl.strip(), headers=headers)
printAccessLog(str(resp.status_code), resp.headers, imgUrl)
if resp.status_code != requests.codes.ok:
logger.error("error response code: "+str(resp.status_code)+" for image URL: "+imgUrl)
return None
if 'Content-Length' in resp.headers and resp.headers['Content-Length'] != None:
if (int(resp.headers['Content-Length']) < int(outputConfig['minimumContentLength'])):
logger.info("Image Content-Length is less than the minimum ("+outputConfig['minimumContentLength']+")")
return None
else:
urllib.request.urlretrieve(imgUrl, buildTmpFileOutputLocation(outputConfig['outputTmpFolderPrefix'], \
getOutputFolder(outputConfig), file_name))
logger.debug("image saved("+resp.headers['Content-Type']+"), filename: "+file_name)
else:
logger.error("Content-Length == None for URL: "+imgUrl)
except requests.exceptions.ConnectionError as e1:
logger.error("A ConnectionError occurred while requesting "+imgUrl)
logError(e1)
except requests.exceptions.TooManyRedirects as e2:
logger.error("A TooManyRedirects occurred while requesting "+imgUrl)
logError(e2)
except urllib.error.HTTPError as e3:
logger.error("A urllib.error.HTTPError occurred while requesting "+imgUrl)
logError(e3)
def downloadRequestsVideo3(videoUrl, videoType, refererUrl, outputConfig):
""" Downloads a single video using the Requests package
"""
if (videoUrl in urlsCache):
logger.info("Cache Hit for: "+videoUrl)
return None
else:
urlsCache.add(videoUrl)
logger.info("urlsCache updated, current size: "+str(len(urlsCache)))
if videoUrl != None:
file_name = buildLocalFilename(videoUrl)
if file_name != "":
file_name = file_name + '.' + videoType
# retries = urllib3.util.retry.Retry(connect=5, read=2, redirect=5) # todo: fix hardcoding
# req = urllib3.PoolManager(retries=retries)
downloads() # log a counter of a download being made
headers = {'user-agent': downloaderConfig.getUserAgent(), 'referer': refererUrl}
try:
resp = requests.get(videoUrl.strip(), headers=headers)
printAccessLog(str(resp.status_code), resp.headers, videoUrl)
if resp.status_code != requests.codes.ok:
logger.error("error response code: "+str(resp.status_code)+" for video URL: "+videoUrl)
return None
if 'Content-Length' in resp.headers and resp.headers['Content-Length'] != None:
if (int(resp.headers['Content-Length']) < int(outputConfig['minimumContentLength'])):
logger.info("Image Content-Length is less than the minimum ("+outputConfig['minimumContentLength']+")")
return None
else:
urllib.request.urlretrieve(videoUrl, buildTmpFileOutputLocation(outputConfig['outputTmpFolderPrefix'], \
getOutputFolder(outputConfig), file_name))
logger.debug("video saved("+resp.headers['Content-Type']+"), filename: "+file_name)
else:
logger.error("Content-Length == None for URL: "+videoUrl)
except requests.exceptions.ConnectionError as e1:
logger.error("A ConnectionError occurred while requesting "+videoUrl)
logError(e1)
except requests.exceptions.TooManyRedirects as e2:
logger.error("A TooManyRedirects occurred while requesting "+videoUrl)
logError(e2)
def get_images(soup, baseUrl, outputConfig, exceptionsConfig):
images = [img for img in soup.findAll('img')]
logger.info(str(len(images)) + " images found.")
logger.info('Downloading images to \''+getOutputFolder(outputConfig)+'\'')
#compile our unicode list of image links
image_links = [each.get('src') for each in images]
for each in image_links:
if each != None:
if not isURLBlacklisted(exceptionsConfig, each):
file_name = buildLocalFilename(each)
if file_name != "":
# todo: Only do this if it doesn't already contain http
fullyQualifiedName = buildFullyQualifiedURL(each,baseUrl)
result = downloadRequestsImage3(fullyQualifiedName, baseUrl, outputConfig)
if (result == None):
logger.debug("Failing image request. Printing imageUrl ("+each+") and baseUrl ("+baseUrl+")")
# Check whether the file is the minimum size and copies it if it meets the minimum size
tmpFileLocation = buildTmpFileOutputLocation(outputConfig['outputTmpFolderPrefix'], getOutputFolder(outputConfig), "")
copyCounter = 0
for eachImage in os.listdir(tmpFileLocation):
imgFile = tmpFileLocation+eachImage
if (os.path.getsize(imgFile) > (int(outputConfig['minimumFileSize'])*1000)):
logger.info("File " + imgFile + " is greater than " + str(outputConfig['minimumFileSize']) + "k")
# Copy the file to the actual location
saved() # log a counter of a object being moved
os.makedirs(getOutputFolder(outputConfig), exist_ok=True)
shutil.move(imgFile, getOutputFolder(outputConfig)+eachImage)
copyCounter += 1
# Delete the file
shutil.rmtree(tmpFileLocation)
logger.info("Temporary files and folder deleted.")
logger.info(str(copyCounter)+" images saved.")
return image_links
def get_videos(soup, baseUrl, outputConfig, exceptionsConfig):
localheaders = {'User-Agent': downloaderConfig.getUserAgent()}
with requests.Session() as session:
session.headers = localheaders
try:
localresponse = session.get(baseUrl)
localsoup = BeautifulSoup(localresponse.content, "html.parser")
# follow the iframe url
iframes = localsoup.findAll('iframe', src=True)
for soup_iframe in iframes:
src = soup_iframe['src']
if src:
iframeResponse = session.get(soup_iframe['src'], headers={'Referer': baseUrl})
iframeSoup = BeautifulSoup(iframeResponse.content, "html.parser")
iframeSources = [source for source in iframeSoup.findAll('source')]
for iframeSourceLink in iframeSources:
pos = iframeSourceLink['type'].rfind('/')
type = iframeSourceLink['type'][:pos]
extension = iframeSourceLink['type'][pos+1:]
if type == 'video':
result = downloadRequestsVideo3(iframeSourceLink['src'], extension, baseUrl, outputConfig)
except requests.exceptions.ConnectionError as e1:
logger.error("A ConnectionError occurred.")
logError(e1)
except requests.exceptions.TooManyRedirects as e2:
logger.error("A TooManyRedirects occurred.")
logError(e2)
except urllib.error.HTTPError as e3:
logger.error("A urllib.error.HTTPError occurred.")
logError(e3)
except requests.exceptions.MissingSchema as e4:
logger.error("A requests.exceptions.MissingSchema occurred.")
logError(e4)
# Check whether the file is the minimum size and copies it if it meets the minimum size
tmpFileLocation = buildTmpFileOutputLocation(outputConfig['outputTmpFolderPrefix'], getOutputFolder(outputConfig), "")
copyCounter = 0
for eachVideo in os.listdir(tmpFileLocation):
videoFile = tmpFileLocation+eachVideo
if (os.path.getsize(videoFile) > (int(outputConfig['minimumFileSize'])*1000)):
logger.info("File " + videoFile + " is greater than " + str(outputConfig['minimumFileSize']) + "k")
# Copy the file to the actual location
saved() # log a counter of a object being moved
os.makedirs(getOutputFolder(outputConfig), exist_ok=True)
shutil.move(videoFile, getOutputFolder(outputConfig)+eachVideo)
copyCounter += 1
# Delete the file
shutil.rmtree(tmpFileLocation)
logger.info("Temporary files and folder deleted.")
logger.info(str(copyCounter)+" videos saved.")
def downloadDepth(soup, downloadUrl, urlConfig, outputConfig, extensionsConfig, exceptionsConfig, currentDepth, imagesOnly):
print("CurrentDepth: "+str(currentDepth))
if not isURLBlacklisted(exceptionsConfig, downloadUrl):
imageResults = get_images(soup, downloadUrl, outputConfig, exceptionsConfig)
linkResults = get_links(soup, downloadUrl, outputConfig)
for url in linkResults:
if not isURLBlacklisted(exceptionsConfig, url):
if checkURLContainsExtension(extensionsConfig, url):
downloadRequestsImage3(url, downloadUrl, outputConfig)
else:
urlResult = downloadRequests3(url)
if urlResult == None:
logger.error("URL ("+url+") returns an error, continuing")
else:
urlSoup = BeautifulSoup(urlResult, "html.parser")
urlImageResults = get_images(urlSoup, url, outputConfig, exceptionsConfig)
if(imagesOnly in ['false', 'FALSE'] and currentDepth < int(urlConfig['maxDepth'])):
downloadDepth(urlSoup, url, urlConfig, outputConfig, extensionsConfig, exceptionsConfig, currentDepth+1, 'false')
def get_links(soup, baseUrl, outputConfig):
urls = []
links = [href for href in soup.findAll('a')]
logger.info(str(len(links)) + " links found.")
#compile our unicode list of image links
allLinks = [each.get('href') for each in links]
for each in allLinks:
if each != None:
urls.append(buildFullyQualifiedURL(each, baseUrl))
return urls
def buildFullyQualifiedURL(srcName, baseUrl):
if "http" not in srcName:
return urllib.parse.urljoin(baseUrl,srcName)
return srcName
def buildLocalFilename(url):
fileName=url.split('/')[-1]
if "?" in fileName:
fileName = fileName[0:fileName.find("?")]
if "&" in fileName:
fileName = fileName[0:fileName.find("&")]
if fileName == "":
logger.error("local filename shortened from \""+url+"\" to \""+fileName+"\"")
return fileName
if url != fileName:
logger.debug("local filename shortened from \""+url+"\" to \""+fileName+"\"")
return fileName
def buildFileOutputLocation(directory, fileName):
""" Takes the given directory, creates it and returns the new filename location
"""
os.makedirs(directory, exist_ok=True)
return directory + fileName
def buildTmpFileOutputLocation(prefix, directory, fileName):
""" Takes the given directory and prepends the tmp directory prefix to it, and then creates it
if it doesn't already exist
"""
# Check whether the location contains /
slashLocation = directory.find("/")
if (slashLocation == -1):
tmpDir = prefix+directory
else:
tmpDir = directory[0:slashLocation+1]+prefix+directory[slashLocation+1:len(directory)]
os.makedirs(tmpDir, exist_ok=True)
return tmpDir + fileName
def readConfigSection(configFile, section):
""" Reads in the given config file and returns the requested section
"""
config = configparser.ConfigParser()
config.read(configFile)
return config[section]
def checkURLContainsExtension(extensionConfig, url):
""" Checks whether any of the extensions from the config file exist in the given url
"""
for type in extensionConfig:
# Return true if it ends with one of the extensions, and debug log it
if url.endswith(extensionConfig[type]):
logger.info("url ("+url+") ends with "+extensionConfig[type])
return True
# Return true if it exists with a '?' after it, and debug log it
updExt = extensionConfig[type] + "?"
if updExt in url:
logger.info("url ("+url+") contains "+updExt)
return True
return False
def isURLBlacklisted(exceptionsConfig, url):
""" Checks whether the given URL contains any of the blacklisted exceptions
"""
#print("Processing isURLBlacklisted for url: "+url)
for blacklistUrl in exceptionsConfig:
# Return true if the url contains the blacklisted snippet
if exceptionsConfig[blacklistUrl] in url:
logger.info("Given URL ("+url+") contains "+exceptionsConfig[blacklistUrl])
return True
return False
def outputHTML(downloadFolder):
#downloadFolder = getOutputFolder(outputConfig)
videos = ('.mp4','.webm')
#downloadFolder = './images_20160214_2129/'
doc, tag, text = Doc().tagtext()
with tag('html'):
with tag('head'):
with tag('title'):
text('Any old title')
with tag('style'):
text('body { background-color: black; }')
text('h1 { color: maroon; margin-left: 40px; } ')
text('img.displayed { display: block; margin-left: auto; margin-right: auto }')
text('video.displayed { display: block; margin-left: auto; margin-right: auto }')
with tag('body'):
for eachItem in os.listdir(downloadFolder):
if (eachItem.endswith(videos)):
doc.asis('<video width="540" class="displayed" controls><source src="'+eachItem+'" ></video>')
#with tag('video', width='540', klass='displayed', src=eachItem):
#with tag('video', width='540', klass='displayed'):
# doc.stag('source', src=eachItem)
doc.stag('br')
elif (not eachItem.startswith('.')):
image = os.path.join(downloadFolder, eachItem)
try:
img = Image.open(image)
width,height = img.size
outputWidth, outputHeight = calculateImgOutputSizes(width,height)
with tag('a', href=eachItem):
doc.stag('img', style="width:"+str(outputWidth)+"px; height:"+str(outputHeight)+"px;)", klass='displayed', src=eachItem)
doc.stag('br')
except:
logger.error("An error occurred while processing "+image+". Skipping.")
with open(downloadFolder+'index.htm', 'a') as out: out.write(indent(doc.getvalue(), indent_text = True))
print('HTML saved to '+downloadFolder+'index.htm')
logger.info('HTML saved to '+downloadFolder+'index.htm')
def regenerateAllHTML():
# Generate the correct download folder
downloadFolder = '.'
# list all folders that match the name format in downloader.ini
for eachItem in os.listdir(downloadFolder):
# print(os.path.join(downloadFolder, eachItem))
eachItem = os.path.join(downloadFolder, eachItem)
if (os.path.isdir(eachItem)):
# for each folder, check if it has an index.htm
fileToProcess = os.path.join(eachItem, "index.htm")
if (checkIfFolderMatchesOutputConfigPattern(eachItem) in ['true', 'TRUE']):
# print("index.htm exists in "+eachItem+", deleting")
#checkIfFolderMatchesOutputConfigPattern(eachItem)
if (os.path.isfile(fileToProcess)):
os.remove(fileToProcess)
outputHTML(eachItem+"/")
def checkIfFolderMatchesOutputConfigPattern(folder):
configOutFolderFormat = downloaderConfig.getOutputFolder()
# remove trailing slash
configOutFolderFormat = configOutFolderFormat[0:len(configOutFolderFormat)-1]
# check if the passed folder starts with this
if (folder.startswith(configOutFolderFormat)):
return 'true'
else:
return 'false'
def calculateImgOutputSizes(width,height):
""" Calculates the image output width and height based on a configurage max width
"""
maxOutputWidth = 540 # TODO: read from config
if (width > maxOutputWidth):
divider = width / maxOutputWidth
outputHeight = height / divider
return (maxOutputWidth, outputHeight)
else:
# If it's already smaller, we don't need to modify it
return (width,height)
def printAccessLog(statusCode, headers, url):
contentLength = '-'
contentType = '-'
if ('Content-Length' in headers):
contentLength = headers['Content-Length']
if ('Content-Type' in headers):
contentType = headers['Content-Type']
accesslogger.info(statusCode+" "+contentLength+" "+contentType+" "+url)
def getProgramTime():
""" Checks whether a unique system time has been generated and generates and returns it
"""
global programTime
if programTime == '':
programTime = datetime.strftime(datetime.now(), '%Y%m%d_%H%M')
return programTime
def getOutputFolder(outputConfig):
""" Checks whether the output folder has been set and generates (if required) and returns it
"""
global outputFolder
if outputFolder == '':
if outputConfig['includeDateInOutputName'] in ['true', 'TRUE']:
# Add the date to the name
outputFolderName = outputConfig['outputFolder']
pos = outputFolderName.rfind("/")
outputFolder = outputFolderName[:pos] + "_" + getProgramTime() + outputFolderName[pos:]
else:
outputFolder = outputConfig['outputFolder']
return outputFolder
def getCacheSize():
return len(urlsCache)
def logError(error):
if error != None and error.args != None and len(error.args) > 0:
logger.error("Error {0}".format(str(error.args[0])))
# Counters for stats
def pages():
pages.counter += 1
def downloads():
downloads.counter += 1
def saved():
saved.counter += 1
def getPages():
return pages.counter
def getDownloads():
return downloads.counter
def getSaved():
return saved.counter
pages.counter = 0
downloads.counter = 0
saved.counter = 0