Created
June 1, 2022 12:16
-
-
Save y0ug/00b329338d8f2f2db10c3edb466faf22 to your computer and use it in GitHub Desktop.
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
import sys | |
import os | |
import shutil | |
import datetime | |
import json | |
import base64 | |
import argparse | |
import logging | |
import requests | |
import random | |
import subprocess | |
resolutions = ['3840x2160', '2560x1440', '1920x1080', '2560x1600', '1680x1050'] | |
download_path = os.path.join(os.path.expanduser('~'), 'Pictures', 'wallpapers') | |
class WpDownloader(): | |
def _download(self, url, file_path): | |
if os.path.exists(file_path): | |
log.info(f'{self.__class__.__name__}, already downloaded, {file_path} ({url}') | |
return False | |
r = requests.get(url, stream=True) | |
if r.headers.get('content-type', None) != 'image/jpeg': | |
return False | |
log.info(f'{self.__class__.__name__}, downloaded, {file_path} ({url}') | |
with open(file_path, 'wb') as fp: | |
shutil.copyfileobj(r.raw, fp) | |
return True | |
class BingWallpaper(WpDownloader): | |
def __init__(self, locale='en-US'): | |
self.base_url = 'https://www.bing.com' | |
self.locale = locale | |
def get(self, n=1, idx=0): | |
url = f'{self.base_url}/HPImageArchive.aspx' | |
params = { 'format': 'js', 'idx': idx, | |
'n': n, 'mkt': self.locale} | |
r = requests.get(url, params=params) | |
return r.json() | |
def download(self, info, target_path): | |
wp = [] | |
for img in info['images']: | |
for res in resolutions: | |
url = f'{self.base_url}{img["urlbase"]}_{res}.jpg' | |
fn = f'wp_bing_{img["startdate"]}_{res}.jpg' | |
file_path = os.path.join(target_path, fn) | |
if not self._download(url, file_path): continue | |
wp.append(file_path) | |
return wp | |
class SpoteligthWallpaper(WpDownloader): | |
def __init__(self, locale='en-US'): | |
self.locale = locale | |
self.country= 'us' | |
def get(self, res): | |
[ disphorzres, dispvertres ] = [int(x) for x in res.split('x')] | |
url = 'https://arc.msn.com/v3/Delivery/Placement' | |
params = {'pid': 209567, 'fmt': 'json', | |
'ua': 'WindowsShellClient', | |
'cdm': 1, | |
'disphorzres': disphorzres, 'dispvertres': dispvertres, | |
'pl': self.locale, | |
'lc': self.locale, | |
'ctry': self.country, | |
'time': datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ') | |
} | |
r = requests.get(url, params=params) | |
return r.json() | |
def download(self, info, target_path): | |
wp = [] | |
for el in info['batchrsp']['items']: | |
item = json.loads(el['item']) | |
for k,v in item['ad'].items(): | |
img_info = item['ad'][k] | |
if not k.startswith('image_fullscreen'): continue | |
if 'landscape' not in k: continue | |
if img_info['t'] != 'img': continue | |
if int(img_info['fileSize']) < 1000: continue | |
url = img_info['u'] | |
sha256 = base64.b64decode(img_info['sha256']).hex() | |
reso = f'{img_info["w"]}x{img_info["h"]}' | |
file_path = os.path.join(target_path, f'wp_spotlight_{sha256}_{reso}.jpg') | |
if not self._download(url, file_path): continue | |
wp.append(file_path) | |
return wp | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(asctime)s; %(name)s; %(levelname)s; %(message)s', | |
handlers=[ | |
#logging.FileHandler(f'{sys.argv[0][:-3]}.log'), | |
logging.StreamHandler() | |
]) | |
log = logging.getLogger() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Wallpaper download tool') | |
parser.add_argument("-v", "--verbose", dest="verbose_count", | |
action="count", default=0) | |
args = parser.parse_args() | |
log.setLevel(max(2 - args.verbose_count, 1) * 10) | |
os.makedirs(download_path, exist_ok=True) | |
wp = SpoteligthWallpaper() | |
for res in resolutions: | |
info = wp.get(res) | |
files = wp.download(wp.get(res), download_path) | |
wp = BingWallpaper() | |
wp.download(wp.get(8), download_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment