-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicloader.py
More file actions
301 lines (236 loc) · 8.6 KB
/
picloader.py
File metadata and controls
301 lines (236 loc) · 8.6 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
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
from io import BytesIO
from os import access,path,mkdir,sep
import sys
import optparse
import urllib
import requests
import re
from PIL import Image
from tqdm import tqdm
from bs4 import BeautifulSoup as bs
from requests_html import HTMLSession
def main(argv):
try:
target_urls = get_targets(argv[1])
except IndexError as e:
print(f"{e}: no target URL entered - exiting...")
return
parser = optparse.OptionParser()
filetypes = parser.add_option_group("filetypes")
sizelimits = parser.add_option_group("sizelimits")
init_optparser(parser,filetypes,sizelimits)
args, remainder = parser.parse_args()
compat_types = [".jpg",".png",".gif"]
if not args.chosen_types:
chosen_types = compat_types
else:
chosen_types = args.chosen_types
output_dir = args.o
count = 0
for url in target_urls:
img_urls = []
print(f"searching in {url}")
if args.G:
gallery_url_list = get_gallery_img_urls(url,args.n)
img_urls.extend(gallery_url_list)
else:
img_urls.extend(get_img_urls(url,args.n))
filtered_img_urls = filter_compat_urls(img_urls,chosen_types)
if output_dir == None:
i = 1
while (path.isdir( sep.join(str(count+i)) + sep)):
i += 1
mkdir(f"{count+i}")
savepath = sep.join(str(count+i)) + sep
else:
i = 1
while (path.isdir(output_dir + sep.join(str(count+i)) + sep)):
i += 1
mkdir(f"{count+i}")
savepath = output_dir + sep.join(str(count+i)) + sep
print(f"saving images from {url} to ..{sep+savepath}")
for url in tqdm(filtered_img_urls,"saving..."):
try:
img = get_img(url)
if args.min_s and args.max_s:
if filter_min_size(img,args.min_s) and filter_max_size(img,args.max_s):
save_img(img, url.rsplit('/', 1)[1],savepath)
elif args.min_s:
if filter_min_size(img,args.min_s):
save_img(img, url.rsplit('/', 1)[1],savepath)
elif (args.max_s != None):
if filter_max_size(img,args.max_s):
save_img(img, url.rsplit('/', 1)[1],savepath)
else:
save_img(img, url.rsplit('/', 1)[1],savepath)
except Exception as e:
print(e)
count += 1
print(f"finished downloading images!")
# Initializes all possible command-line options
def init_optparser(parser,filetypegroup,sizelimitgroup):
# Output Directory
parser.add_option('-o',
help="Set Output Directory for downloaded Images."
"If not set, Output Directory will be current Directory",
action="store"
)
# Set a File Name or part of a name to search for
parser.add_option('-n',
"--name",help="Search for images with a specific name, * for "
"wildcard (e.g. \"image*\" for files like image123.jpg)",
action="store",
dest="n"
)
# Search for the source images of a image gallery
parser.add_option('-G',
"--Gallery",help="tries to download the source images in a gallery of thumbnails",
action="store_true",
dest="G"
)
# Filter for Filetypes
filetypegroup.add_option('-g',"--gif",
help="Filter for .gif Images,"
"can be combined with other Imagetype Filters",
action="append_const",
const=".gif",
dest="chosen_types"
)
filetypegroup.add_option('-j',"--jpeg",
help="Filter for .jpeg Images,"
"can be combined with other Imagetype Filters",
action="append_const",
const=".jpg",
dest="chosen_types"
)
filetypegroup.add_option('-p',"--png",
help="Filter for .png Images,"
"can be combined with other Imagetype Filters",
action="append_const",
const=".png",
dest="chosen_types"
)
# Set min. and max. Image Size
sizelimitgroup.add_option("--min-size",
metavar="WIDTH HEIGHT",
help="All images sized above the chosen WIDTH and HEIGHT"
"in Pixels, can be combined with --max-size",
type=int,
nargs=2,
dest="min_s",
default=None
)
sizelimitgroup.add_option("--max-size",
metavar="WIDTH HEIGHT",
help="All images sized below the chosen WIDTH and HEIGHT"
"in Pixels, can be combined with --min-size",
type=int,
nargs=2,
dest="max_s",
default=None
)
# Extract all absolute image-source-urls from the html code of given webpage url,
# returns list of image-source-urls
def get_gallery_img_urls(url : str, searchword : str = None):
gallery_links = get_gallery_source_urls(url)
gallery_img_links = []
print(f"searching {len(gallery_links)} gallery source links")
for glink in tqdm(gallery_links, "extracting source image links from gallery"):
img_links = get_img_urls(glink,searchword)
gallery_img_links.extend(img_links)
print(f"found {len(gallery_img_links)} image links from gallery")
return gallery_img_links
def get_gallery_source_urls(url : str):
page_html = get_html(url)
elements = page_html.find('a')
source_links = []
for e in elements:
if re.findall(r'<.*[\"\'](?:gallery.*|thumb.*)[\"\']\s?[>]',e.html):
link = str(e.absolute_links)
link = link[2:-2]
source_links.append(link)
#print(f"Gallery Source Links: {source_links}")
return source_links
def get_img_urls(url : str, pattern : str = None ):
page_html = get_html(url)
links = []
if(pattern == None):
pattern = "*"
for link in re.findall(r'(http\:\/\/|https\:\/\/)?([a-zA-Z0-9\-\.\_]+\.[a-zA-Z]{2,3})(\/\S*?)(\.jpg|\.jpeg|\.gif|\.png){1}',page_html.html):
link = ''.join(map(str, link))
if re.match(re.escape(pattern),link.rsplit('/', 1)[1]) != None:
link = urllib.parse.urljoin(url, link)
links.append(link)
links = remove_dupl_urls(links)
return links
def get_img(img_url):
resp = requests.get(img_url,stream=True)
return Image.open(BytesIO(resp.content))
# Helper functions
def remove_dupl_urls(url_list : list):
return list(dict.fromkeys(url_list))
def get_html(url : str):
session = HTMLSession()
resp = session.get(url)
resp.html.render()
#print(resp.html.html)
return resp.html
def read_targets_file(path : str):
url_list = []
try:
file = open(path,"r")
except FileNotFoundError as e:
print(f"{e}")
return None
print(f"opened {path}")
for line in file.read().splitlines():
if re.match(r'(http\:\/\/|https\:\/\/|www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?',line) != None:
url_list.append(line)
file.close()
print(f"found {len(url_list)} target URL(s) in file")
return url_list
def get_targets(input : str):
file_links = []
if re.match(r'(http\:\/\/|https\:\/\/|www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}\/(\/\S*)?',input) != None:
file_links.append(input)
return file_links
else:
file_links = read_targets_file(input)
return file_links
# Saves image to given path
# (if savepath=None: Save in current working directory)
def save_img(img,fname : str,savepath : str = None):
if savepath == None:
img.save(fname)
#print("Saving: {}".format(fname))
return
fpath = savepath+fname
img.save(fpath)
#print("Saving {} to: {}".format(fname,fpath))
return
# Functions to filter a list of given Image-URLs for compatible formats
def check_compat(url : str,compat_types : list):
for type in compat_types:
if url.find(type) != -1:
return True
return False
def filter_compat_urls(url_list : list,compat_types : list):
filtered_list = []
for url in url_list:
if check_compat(url,compat_types):
filtered_list.append(url)
print(f"found/filtered {len(filtered_list)} compatible image URL(s)")
return filtered_list
# Filter images by given minimum and/or maximum size
def filter_min_size(img,size_limit):
width, height = img.size
if width > size_limit[0] and height > size_limit[1]:
return True
return False
def filter_max_size(img,size_limit):
width, height = img.size
if width < size_limit[0] and height < size_limit[1]:
return True
return False
if __name__ == "__main__":
main(sys.argv)