Last active
October 31, 2021 12:23
-
-
Save curipha/03d9c1a1009de3e4d063759a9b0de9c9 to your computer and use it in GitHub Desktop.
Mass downloader for bing wallpaper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import hashlib | |
import json | |
import os | |
from urllib import request,parse | |
TARGET_DIR = './bing/' | |
BING_URI_FORMAT = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx={}&n=8&mkt={}' # n > 8 is ignored | |
LOCALE = { | |
'de-de', | |
'en-ca', | |
'en-gb', | |
'en-hk', # for all other region/languages | |
'en-in', | |
'en-us', | |
'fr-ca', | |
'fr-fr', | |
'ja-jp', | |
'zh-cn', | |
} | |
if not os.path.isdir(TARGET_DIR): | |
print('Download directory does not exist!') | |
exit(1) | |
for l in LOCALE: | |
print('Checking for locale:', l) | |
for i in range(0, 14, 8): # Currently only 14 images can be fetched (maybe for 2 weeks) | |
wallpapers = set(map(lambda f: os.path.splitext(f)[0], os.listdir(TARGET_DIR))) | |
req = request.urlopen(BING_URI_FORMAT.format(i, l)) | |
bing = json.loads(req.read()) | |
for j in bing['images']: | |
image = parse.urljoin('https://www.bing.com/', j['url']) | |
print(image, end=' ... ') | |
sha256 = hashlib.sha256(image.encode()).hexdigest() | |
if sha256 in wallpapers: | |
print('SKIP') | |
continue | |
req = request.urlopen(image) | |
with open(os.path.join(TARGET_DIR, '{0}.jpg'.format(sha256)), 'wb') as fp: | |
fp.write(req.read()) | |
print('OK') | |
# Remove duplicated images | |
for f in os.scandir(TARGET_DIR): | |
if not f.is_file(): | |
continue | |
if not os.path.splitext(f)[1] == '.jpg': | |
continue | |
with open(f, 'rb') as fp: | |
sha256 = hashlib.sha256(fp.read()).hexdigest() | |
print(os.path.splitext(os.path.basename(f))[0], '->', sha256) | |
os.replace(f, os.path.join(TARGET_DIR, '{0}.jpg'.format(sha256))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment