-
Notifications
You must be signed in to change notification settings - Fork 2
/
imgurl.py
executable file
·114 lines (81 loc) · 3.35 KB
/
imgurl.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
#!/usr/bin/python3
###############################################################
# Imports
###############################################################
import random
import subprocess
import urllib.request
import os
import sys
###############################################################
# Variables
###############################################################
# Base URL used for downloads
imgUrl = 'http://i.imgur.com/'
# output dir for images
dlPath = './output/'
# sha256sum of placeholder image from imgur.com
shaSum = '9b5936f4006146e4e1e9025b474c02863c0b5614132ad40db4b925a10e8bfbb9'
# Number of tries used to fetch an image
tries = 1
# Get number of images to download from user input (make this a static value to force download specific number of images when run)
numOfPics = int(input('How many pictures to download? '))
###############################################################
# Functions
###############################################################
def randomnes(): # Function to generate random url to images on imgur.com
ext = '.jpg'
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
r1 = random.choice(chars)
r2 = random.choice(chars)
r3 = random.choice(chars)
r4 = random.choice(chars)
r5 = random.choice(chars)
rT = r1 + r2 + r3 + r4 + r5
rTE = rT + ext
rTFull = imgUrl + rTE
return (rTFull, rTE)
def downloadImages(rTFull, dlPath, filename): # Download random generatet image url to ./output folder
local_file_name = dlPath + filename
with open(local_file_name, 'wb') as f:
r = urllib.request.urlopen(rTFull).read()
f.write(r)
f.close()
return local_file_name
def check_sha256sum(f, shaSum, numOfPics, tries): # Check sha256sum of downloaded image, check if dead image placeholder downloadet, if so download a replacement image.
file_name = f
output = subprocess.check_output(['sha256sum', file_name])
newShaSum = output[:64] # grap 64 chars of the shasum
newShaSum = newShaSum.decode('utf-8') # converting it to a string to compare to shaSum
# check if new file is a placeholder
if newShaSum == shaSum:
print ('Try number:', tries, '- dead image placeholder downloaded...')
tries += 1
# Delete image
os.remove(file_name)
shortFileName = file_name[9:]
print ('deleting file', shortFileName, 'and downloading new image...\n')
# Fetch new image
rTFull = randomnes()
local_file_name = downloadImages(rTFull[0], dlPath, rTFull[1])
check_sha256sum(local_file_name, shaSum, numOfPics, tries)
else:
print ('Try number:', tries, '- done!\n\n')
return
def check_output_dir(f):
outputDir = os.path.dirname(f)
print ('Checking if output folder exist...')
if not os.path.exists(outputDir):
print ('Output dir not found, making one...\n')
os.makedirs(outputDir)
else:
print ('output folder exists...\n')
###############################################################
# Main
###############################################################
check_output_dir(dlPath)
print ('Need to download', numOfPics, 'images...\n')
for i in range(numOfPics):
rTFull = randomnes()
local_file_name = downloadImages(rTFull[0], dlPath, rTFull[1])
check_sha256sum(local_file_name, shaSum, numOfPics, tries)