-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagescraper.py
executable file
·94 lines (71 loc) · 2.92 KB
/
imagescraper.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
"""
Created on Thu Oct 22 13:31:29 2020
@author: carminoplata
"""
import logging
import logging.config
import os
import sys
import threading
from bingsearch import BingSearch
from pathlib import Path
def parsearguments(args):
target = ''
output_folder = ''
num_images = 0
i = 1
loglevel = 'INFO'
if args[0] != '-i' :
raise ValueError
while i < len(args) and args[i]!='-n':
target += args[i] + ' '
i+= 1
if args[i] != '-n' or i >= len(args):
raise ValueError
num_images = int(args[i+1])
if args[i+2] != '-o':
raise ValueError
output_folder = args[i+3]
i+=3
if i < (len(args)-1) :
loglevel = args[i+1]
return target, num_images, output_folder, loglevel
if __name__ == '__main__':
logging.config.fileConfig(os.path.join(os.getcwd(), 'logging.conf'))
logger = logging.getLogger('ImageScraper')
logger.info("Welcome to ImageScraper!")
if(len(sys.argv) < 2):
logger.error("Run: imagescraper.py -i <query_to_search> -n <num_of_images> "
+" -o <output_folder>")
exit(-1)
try:
target, num_images, output_folder, loglevel = parsearguments(sys.argv[1:])
if(not target or not num_images or not output_folder):
logger.error("Impossible parse the arguments")
logging.info("Searching {} using Bing Search...".format(target))
lock = threading.Lock()
cv = threading.Condition(lock)
if not os.path.isdir(output_folder):
os.mkdir("{}/{}".format(os.getcwd(), output_folder))
beerSearcher = BingSearch(target.capitalize()+"Searcher", target,
"+filterui:imagesize-medium", num_images,
output_folder, cv, loglevel)
peopleSearcher = BingSearch("PeopleSearcher", "people",
"+filterui:imagesize-medium", num_images,
output_folder, cv, loglevel)
animalSearcher = BingSearch("AnimalSearcher","animals",
"+filterui:imagesize-medium", num_images,
output_folder, cv, loglevel)
objectSearcher = BingSearch("ObjectSearcher", "objects",
"+filterui:imagesize-medium", num_images,
output_folder, cv, loglevel)
tasks = [beerSearcher, peopleSearcher, animalSearcher, objectSearcher]
for t in tasks:
t.start()
for t in tasks:
t.join()
except IndexError as e:
logger.error(e.with_traceback())
except ValueError:
logger.error("Unknown command!\n Please run:\n imagescraper.py -i "
+ "<query_to_search> -n <num_of_images> -o <output_folder> ")