File size: 2,591 Bytes
6610027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import os

import cv2
import requests
from requests import exceptions

ap = argparse.ArgumentParser()
ap.add_argument(
    "-q",
    "--query",
    required=True,
    help="search query to search Bing Image API for",
)
ap.add_argument(
    "-o",
    "--output",
    required=True,
    help="path to output directory of images",
)
args = vars(ap.parse_args())
API_KEY = "d8982f9e69a4437fa6e10715d1ed691d"
MAX_RESULTS = 500
GROUP_SIZE = 50
URL = "https://api.cognitive.microsoft.com/bing/v7.0/images/search"
EXCEPTIONS = {
    IOError,
    FileNotFoundError,
    exceptions.RequestException,
    exceptions.HTTPError,
    exceptions.ConnectionError,
    exceptions.Timeout,
}
term = args["query"]
headers = {"Ocp-Apim-Subscription-Key": API_KEY}
params = {"q": term, "offset": 0, "count": GROUP_SIZE}
print(f"[INFO] searching Bing API for '{term}'")
search = requests.get(URL, headers=headers, params=params)
search.raise_for_status()
results = search.json()
estNumResults = min(results["totalEstimatedMatches"], MAX_RESULTS)
print(
    "[INFO] {} total results for '{}'".format(
        estNumResults,
        term,
    ),
)
total = 0
for offset in range(0, estNumResults, GROUP_SIZE):
    print(
        "[INFO] making request for group {}-{} of {}...".format(
            offset,
            offset + GROUP_SIZE,
            estNumResults,
        ),
    )
    params["offset"] = offset
    search = requests.get(URL, headers=headers, params=params)
    search.raise_for_status()
    results = search.json()
    print(
        "[INFO] saving images for group {}-{} of {}...".format(
            offset,
            offset + GROUP_SIZE,
            estNumResults,
        ),
    )
    for v in results["value"]:
        try:
            print("[INFO] fetching: {}".format(v["contentUrl"]))
            r = requests.get(v["contentUrl"], timeout=30)
            ext = v["contentUrl"][v["contentUrl"].rfind(".") :]
            p = os.path.sep.join(
                [
                    args["output"],
                    "{}{}".format(
                        str(total).zfill(8),
                        ext,
                    ),
                ],
            )
            f = open(p, "wb")
            f.write(r.content)
            f.close()
        except Exception as e:
            if type(e) in EXCEPTIONS:
                print("[INFO] skipping: {}".format(v["contentUrl"]))
                continue
        image = cv2.imread(p)
        if image is None:
            print(f"[INFO] deleting: {p}")
            os.remove(p)
            continue
        total += 1