Last active 1772610543

Revision 3cfa518ee9b161bc8b100faaf0cd42fcb3d4797b

download-bing-py3.py Raw
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Create By Doracoin
4# Github: https://github.com/doracoin
5
6import json, os, sys
7from urllib.request import Request, urlopen, URLError, HTTPError
8
9# 要查询的天数(尽量写大点, 反正最多只能查询到当天和过去7天的)
10days = 30
11# 超时时间
12time_out = 10
13# 是否下载全部可能支持的分辨率(大部分图片都有三个分辨率, 1920x1200, 1920x1080, 1366x768, 其中只有1920x1200有水印)
14# 2023-11-28 发现图片分辨率新增了 UHD 版本,根据文件大小和分辨率来看可能是原图,具体什么时候新增的无法确定
15all_px = False
16# 默认保存路径
17down_dir = "." + os.path.sep + "bing_wallpaper"
18# 接口地址
19bingUrl = "".join(['http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=', str(days), '&mkt=zh-cn'])
20
21for i in range(0, len(sys.argv)):
22 if (i == len(sys.argv) - 1):
23 key = sys.argv[i]
24 value = ""
25 else:
26 key = sys.argv[i]
27 value = sys.argv[i + 1]
28 # print ("key: %s, value: %s" % (key, value))
29 if ("-d" == key):
30 days = value
31 bingUrl = "".join(['http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=', str(days), '&mkt=zh-cn'])
32 if ("-t" == key):
33 time_out = int(value)
34 if ("-a" == key):
35 if ("yes" == str(value).lower() or "y" == str(value).lower()):
36 all_px = True
37 else:
38 all_px = False
39 if ("-p" == key):
40 down_dir = value
41 if (i < 2 and ("-h" == key or "-help" == key or "--help" == key)):
42 print (" -d (number) 指定查询的天数, 最多只能查询到当天和过去7天的")
43 print (" -t (number) 请求超时时间, 单位:秒")
44 print (" -a (y/n, Y/N) 是否尝试下载所有分辨率的图片, 默认 yes")
45 print (" -p (string) 指定图片保存路径, 注意你的系统的分隔符 '\\' or '/'")
46 print (" -h 显示帮助")
47 exit(0)
48
49
50def download_img(url):
51 try:
52 target_file_path = down_dir + os.path.sep + check_base_url(os.path.basename(url))
53 if (os.path.exists(target_file_path) == False):
54 img_data = urlopen(Request(url)).read()
55 fp = open(target_file_path, 'wb')
56 fp.write(img_data)
57 fp.close()
58 print (url, "图片下载成功")
59 else:
60 print (url, "该图片已存在,跳过")
61 except URLError as e:
62 if isinstance(e, HTTPError):
63 print (url, "该图片无法下载:", e.code, "" if e.reason == None else e.reason)
64 else:
65 print (url, "该图片无法下载", "" if e.reason == None else e.reason)
66
67
68def get_bing_wallpaper():
69 if (os.path.exists(down_dir) == False):
70 os.makedirs(down_dir)
71 req = Request(bingUrl)
72 try:
73 res_data = urlopen(req, timeout=time_out).read()
74 print ("请求壁纸信息成功,正在解析\n")
75 except URLError as e:
76 print ("查询接口失败,有可能是网络问题,或是Bing官方关闭了这个接口,请稍后重试", "" if e.reason == None else e.reason)
77 exit()
78 s = json.loads(res_data)
79 img_counts = len(s["images"])
80 for i in range(0, img_counts):
81 item = s["images"][i]["url"] # 默认1920x1080分辨率
82 item_base = s["images"][i]["urlbase"] # 图片baseurl,可以拼接分辨率
83 # UHD
84 download_img("http://www.bing.com" + item_base + "_UHD.jpg")
85 # 1920x1080
86 download_img("http://www.bing.com" + item_base + "_1920x1080.jpg")
87 if bool(all_px):
88 # 1366x768
89 download_img("http://www.bing.com" + item_base + "_1366x768.jpg")
90 # 1920x1200 (有水印)
91 download_img("http://www.bing.com" + item_base + "_1920x1200.jpg")
92 print ("\n")
93
94
95def check_base_url(base_url):
96 base_url = base_url.replace('th?id=OHR.', '')
97 return base_url
98
99
100if __name__ == '__main__':
101 print ("\nWelcome to use < Bing Wallpaper Downloader 1.0 >\n")
102 print ("文件将会被下载到: ", down_dir)
103 print ("是否尝试多分辨率: ", "" if all_px == True else "")
104 print ("尝试查询天数: ", days, "\n")
105
106 # 开始下载
107 get_bing_wallpaper()
108
start-download.sh Raw
1#!/bin/bash
2
3# You can make this script run by crontab
4# 1 8 * * * /path/to/start-download.sh
5
6cd `dirname $0`
7
8python download-bing.py
9
10cp --preserve=timestamps ./bing_wallpaper/*_1920* ./1080P