Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update for bing v7.0 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions bing_image_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def save_image(filename, image):

headers = {
# Request headers
'Content-Type': 'multipart/form-data',
# 'Content-Type': 'multipart/form-data',
'Ocp-Apim-Subscription-Key': bing_api_key, # API key
}

Expand All @@ -101,7 +101,7 @@ def save_image(filename, image):

try:
conn = http.client.HTTPSConnection('api.cognitive.microsoft.com')
conn.request("POST", "/bing/v5.0/images/search?%s" % params, "{body}", headers)
conn.request("POST", "/bing/v7.0/images/search?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()

Expand All @@ -118,13 +118,15 @@ def save_image(filename, image):
decode_res = data.decode('utf-8')
data = json.loads(decode_res)

pattern = r"&r=(http.+)&p=" # extract an URL of image
# pattern = r"&r=(http.+)&p=" # extract an URL of image

for values in data['value']:
unquoted_url = urllib.parse.unquote(values['contentUrl'])
img_url = re.search(pattern, unquoted_url)
if img_url:
url_list.append(img_url.group(1))
# img_url = re.search(pattern, unquoted_url)
# if img_url:
# url_list.append(img_url.group(1))
if unquoted_url:
url_list.append(unquoted_url)

for url in url_list:
try:
Expand Down