@@ -43,75 +43,100 @@ def add_filetype(file_path: str):
43
43
44
44
45
45
def process_image_size (val : str ):
46
+ key = 'isz:'
46
47
if (val == 'large' ):
47
- return "isz:l"
48
+ return key + 'l'
48
49
elif (val == 'medium' ):
49
- return "isz:m"
50
+ return key + 'm'
50
51
elif (val == 'icon' ):
51
- return "isz:i"
52
+ return key + 'i'
53
+ elif (val in ['400x300' , '640x480' , '800x600' , '1024x768' ]):
54
+ key += 'lt%2Cislt:'
55
+ if (val == '400x300' ):
56
+ return key + "qsvga"
57
+ elif (val == '640x480' ):
58
+ return key + "vga"
59
+ elif (val == '800x600' ):
60
+ return key + "svga"
61
+ elif (val == '1024x768' ):
62
+ return key + "xga"
63
+ elif (val in ['2mp' ,'4mp' ,'6mp' ,'8mp' ,'10mp' ,'12mp' ,'15mp' ,'20mp' ,'40mp' ,'70mp' ]):
64
+ return key + 'lt%2Cislt:' + val
52
65
else :
53
66
return ""
54
67
68
+ def process_image_aspectratio (val : str ):
69
+ key = 'iar:'
70
+ if (val == 'tall' ):
71
+ return key + 't'
72
+ elif (val == 'square' ):
73
+ return key + 's'
74
+ elif (val == 'wide' ):
75
+ return key + 'w'
76
+ elif (val == 'panoramic' ):
77
+ return key + 'xw'
78
+
55
79
def process_image_color (val : str ):
56
- if (val == "grayscale" ):
80
+ if (val == "color" ):
81
+ return "ic:color"
82
+ elif (val == "grayscale" ):
57
83
return "ic:gray"
58
84
elif (val == "transparent" ):
59
85
return "ic:trans"
60
- elif (val == "red" ):
61
- return "ic:specific%2Cisc:red"
62
- elif (val == "orange" ):
63
- return "ic:specific%2Cisc:orange"
64
- elif (val == "yellow" ):
65
- return "ic:specific%2Cisc:yellow"
66
- elif (val == "green" ):
67
- return "ic:specific%2Cisc:green"
68
- elif (val == "teal" ):
69
- return "ic:specific%2Cisc:teal"
70
- elif (val == "blue" ):
71
- return "ic:specific%2Cisc:blue"
72
- elif (val == "purple" ):
73
- return "ic:specific%2Cisc:purple"
74
- elif (val == "pink" ):
75
- return "ic:specific%2Cisc:pink"
76
- elif (val == "white" ):
77
- return "ic:specific%2Cisc:white"
78
- elif (val == "gray" ):
79
- return "ic:specific%2Cisc:gray"
80
- elif (val == "black" ):
81
- return "ic:specific%2Cisc:black"
82
- elif (val == "brown" ):
83
- return "ic:specific%2Cisc:brown"
86
+ elif (val in ['red' ,'orange' ,'yellow' ,'green' ,'teal' ,'blue' ,'purple' ,'pink' ,'white' ,'gray' ,'black' ,'brown' ]):
87
+ return "ic:specific%2Cisc:" + val
84
88
else :
85
89
return ""
86
90
87
91
def process_image_type (val : str ):
88
- if (val == "clipart" ):
89
- return "itp:clipart"
90
- elif (val == "lineart" ):
91
- return "itp:lineart"
92
- elif (val == "animated" ):
93
- return "itp:animated"
92
+ if (val in ['face' , 'photo' , 'clipart' , 'lineart' , 'animated' ]):
93
+ return 'itp:' + val
94
94
else :
95
95
return ""
96
96
97
+ def process_image_region (val : str ):
98
+ if (val == '' ):
99
+ return ''
100
+ else :
101
+ return 'ctr:country' + val .upper ()
102
+
103
+ def process_image_filetype (val : str ):
104
+ if (val in ['jpg' , 'gif' , 'png' , 'bmp' , 'svg' , 'webp' , 'ico' , 'raw' ]):
105
+ return 'ift:' + val
106
+
107
+ def process_image_usage (val : str ):
108
+ key = 'sur:'
109
+ if (val == 'cc' ):
110
+ return key + 'cl'
111
+ elif (val == 'other' ):
112
+ return key + 'ol'
113
+ else :
114
+ return ''
115
+
97
116
def process_safesearch (val : str ):
98
- if (val == "on" ):
99
- return "on"
100
- elif (val == "off" ):
101
- return "off"
117
+ if (val in ["on" , "off" ]):
118
+ return val
102
119
else :
103
120
return ""
104
121
105
122
106
- def setup_url (searchurl : str , imgsize : str , imgcolor : str , imgtype : str , safesearch : str ):
123
+ def setup_url (searchurl : str , imgsize : str , imgaspectratio : str , imgcolor : str , imgtype : str , imgregion : str , imgfiletype : str , imgusage : str , safesearch : str ):
107
124
features = [searchurl ]
108
125
subfeatures = [[],[]]
109
126
if (imgsize != None ):
110
127
subfeatures [0 ] += [process_image_size (imgsize )]
128
+ if (imgaspectratio != None ):
129
+ subfeatures [0 ] += [process_image_aspectratio (imgaspectratio )]
111
130
if (imgcolor != None ):
112
131
subfeatures [0 ] += [process_image_color (imgcolor )]
113
132
if (imgtype != None ):
114
133
subfeatures [0 ] += [process_image_type (imgtype )]
134
+ if (imgregion != None ):
135
+ subfeatures [0 ] += [process_image_region (imgregion )]
136
+ if (imgfiletype != None ):
137
+ subfeatures [0 ] += [process_image_filetype (imgfiletype )]
138
+ if (imgusage != None ):
139
+ subfeatures [0 ] += [process_image_usage (imgusage )]
115
140
if (safesearch != None ):
116
141
subfeatures [1 ] += [process_safesearch (safesearch )]
117
142
@@ -122,7 +147,7 @@ def setup_url(searchurl: str, imgsize: str, imgcolor: str, imgtype: str, safesea
122
147
features += ["tbs=" + delim2 .join (subfeatures [0 ])]
123
148
if (subfeatures [1 ] != []):
124
149
features += ["safe=" + delim2 .join (subfeatures [1 ])]
125
-
150
+ print ( delim1 . join ( features ))
126
151
return delim1 .join (features )
127
152
128
153
@@ -244,7 +269,7 @@ def get_manifest(search_key: str, image_cnt: int):
244
269
245
270
################################# main api ####################################
246
271
247
- def scrape_images (search_key , image_cnt , directory , threads , size , color , imgtype , safesearch ):
272
+ def scrape_images (search_key , image_cnt , directory , threads , size , aspectratio , color , imgtype , region , filetype , usage , safesearch ):
248
273
"""
249
274
Request manifest, generate paths, save files, get filetype.
250
275
This is the only function that should be called externally.
@@ -260,7 +285,7 @@ def scrape_images(search_key, image_cnt, directory, threads, size, color, imgtyp
260
285
if not os .path .exists (directory ):
261
286
os .makedirs (directory )
262
287
global search_url
263
- search_url = setup_url (search_url , size , color , imgtype , safesearch )
288
+ search_url = setup_url (search_url , size , aspectratio , color , imgtype , region , filetype , usage , safesearch )
264
289
id_url_manifest = get_manifest (search_key , image_cnt )
265
290
with ThreadPoolExecutor (max_workers = threads ) as pool :
266
291
with tqdm (total = len (id_url_manifest )) as progress :
0 commit comments