20190308更新
bing已经停止维护每日故事的接口
装了新的系统就总想折腾一下好看的桌面,一直都很喜欢bing网站上每日更新的图片,所以就像把这个爬下来做每日壁纸,windows和部分linux发行版都有已经开发的软件可以实现,在网上也有很多API可以直接调用。我找到了一个加水印版本的代码改动了以下就能很好的运用,还可以抓取每日的壁纸背后的故事,虽然这个加水印确实有些难看,我也不知道也什么用。
1.安装ImageMagick
1
2
3
4
|
wget -4 https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-7.0.8-16.x86_64.rpm
wget -4 https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-libs-7.0.8-16.x86_64.rpm
sudo yum localinstall ImageMagick-libs-7.0.8-16.x86_64.rpm
sudo yum localinstall ImageMagick-7.0.8-16.x86_64.rpm
|
2.抓取网点分析(JSON格式)
这两个网址都是每日更新的,可以从这里下手来抓取每日的图片地质
图片网址https://cn.bing.com/HPImageArchive.aspx?format=js&idx=1&n=1
{"images":
[{"startdate":"20181211",
"fullstartdate":"201812111600",
"enddate":"20181212",
"url":"/az/hprichbg/rb/PoinsettiaBuds_ZH-CN13611855261_1920x1080.jpg",
"urlbase":"/az/hprichbg/rb/PoinsettiaBuds_ZH-CN13611855261",
"copyright":"一品红的花蕾 (© Wild Wonders of Europe/Radisics/Minden Pictures)",
"copyrightlink":"/search?q=%e4%b8%80%e5%93%81%e7%ba%a2&form=hpcapt&mkt=zh-cn",
"title":"",
"quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20181211_PoinsettiaBuds%22&FORM=HPQUIZ",
"wp":true,
"hsh":"30a8c2c59bb62edcb47f5a87f8a3e9f1",
"drk":1,
"top":1,
"bot":1,
"hs":[]}
],
"tooltips":{"loading":"正在加载...","previous":"上一个图像","next":"下一个图像","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。","play":"播放视频","pause":"暂停视频"}
}
每日故事网址https://www.bing.com/cnhp/coverstory?format=js
{"date":"December 13",
"title":"世界尽头的洒脱都市",
"attribute":"冰岛,雷克雅未克",
"para1":"满城的灯火辉煌让人更加能感受到圣诞的浓厚气息,壁纸中的便是美丽的雷克雅未克。虽然是冰岛的首都,但其实这里还是保持着一种独特的缓慢感和原始感,甚至几乎没有几座高楼大厦,人们永远都在悠闲地慢生活。即使在工作日的白天,街角的咖啡馆里还是座无虚席,在这样一个慢节奏的都市,与我们一起来一场不一样的旅行吧!",
"para2":"",
"provider":"©Arctic-Images/Corbis Documentary/Getty Images",
"imageUrl":"http://hpimges.blob.core.chinacloudapi.cn/coverstory/watermark_reykjavikyulelads_zh-cn12225355290_1920x1080.jpg",
"primaryImageUrl":"http://hpimges.blob.core.chinacloudapi.cn/coverstory/watermark_reykjavikyulelads_zh-cn12225355290_1920x1080.jpg",
"Country":"冰岛",
"City":"雷克雅未克",
"Longitude":"-21.926600 ",
"Latitude":"64.141700",
"Continent":"欧洲",
"CityInEnglish":"Reykjavik","CountryCode":""
}
3.python 抓取 代码
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
|
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
#!/usr/bin/python3
# ubuntu设置动态bing壁纸
import requests
import os, re
save_dir = "/home/sherry/wallpaper/"
requests.adapters.DEFAULT_RETRIES = 5
#获取今天的图片的url和copyright信息
def get_bing_info():
base_url = "http://cn.bing.com/"
query_url = "HPImageArchive.aspx?format=js&idx=0&n=1"
r = requests.get(base_url + query_url)
#s = requests.session()
#s.keep_alive = False
#s.proxies = {"https": "47.100.104.247:8080", "http": "36.248.10.47:8080", }
#r.get(base_url + query_url)
image_url = base_url + r.json()["images"][0]["url"]
copyright = r.json()["images"][0]["copyright"]
copyright = re.sub(r'\(.*\)', '', copyright);
print(copyright)
return (image_url, copyright)
#下载图片
def down_pic(pic_url, save_dir):
# image_name = re.sub(r'.*/', '', pic_url)
save_path = save_dir + "wallpaper.jpg"
print(save_path)
r = requests.get(pic_url)
file = open(save_path, "wb")
file.write(r.content)
file.close()
return save_path
#添加水印
def add_watermark(path, text):
new_path = path + ".watermark"
cmd = "convert {0} -font /usr/share/fonts/wqy-microhei/wqy-microhei.ttc -pointsize 30 -draw \"gravity south fill black text 0,72 '{1}' fill white text 1,71 '{1}' \" {2}".format(path, text, new_path)
#print(cmd)
os.system(cmd)
os.system("mv {} {}".format(new_path, path))
#通过python的PIL库添加水印
def main():
url, copyright = get_bing_info()
path = down_pic(url, save_dir)
add_watermark(path, copyright)
# add_watermark_by_PIL(path, copyright)
main()
|
4.shell抓取代码
python的代码总有些时候运行不了,而shell的就比较方便了,经改良后设置了图片名字按日期,如果当天已经运行过一次程序就省去整个过程。
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
|
path=/home/sherry/wallpaper/$(date +%Y%m%d).jpg
if [ ! -f "$path" ]; then
mv /home/sherry/wallpaper/*.jpg /home/sherry/wallpaper/old/
fi
## 路径
urlpath=$( \
curl -4 "https://www.bing.com/HPImageArchive.aspx?format=rss&idx=0&n=1&mkt=en-US" \
| xmllint --xpath "/rss/channel/item/link/text()" - \
| sed 's/1366x768/1920x1080/g' \
)
#urlpath=https://cn.bing.com/$(curl -4 -s "http://bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"|grep -Po 'url[" :]+\K[^"]+')
## 文字信息
text=$( \
curl -4 "https://www.bing.com/HPImageArchive.aspx?format=rss&idx=0&n=1&mkt=en-US" \
| xmllint --xpath "string(/rss/channel/item/title)" - \
| sed 's/(.*)//g' \
)
## 每日故事
story=$(curl -4 -s "https://cn.bing.com/cnhp/coverstory?d="|grep -Po 'para1[" :]+\K[^"]+')
echo $story
#path='/home/sherry/wallpaper/'$DATE
new_path=$path'.watermark'
wget -4 -O $path "https://www.bing.com$urlpath"
convert $path -font /usr/share/fonts/wqy-microhei/wqy-microhei.ttc -pointsize 30 -draw "gravity south fill black text 0,72 '$text' fill white text 1,71 '$text' " $new_path
mv $new_path $path
## 分行添加故事水印(此部分已经无效)
length=${#story}
let "sum=$length/20"
for (( c=0; c<=$sum; c++ ));
do
let "start=c*20"
let "height=($sum+1-$c)*40"
mid=`echo ${story:$start:20}|sed 's/^。//g;s/。$//g;s/ //g'`
#echo $mid
mogrify -font /usr/share/fonts/wqy-microhei/wqy-microhei.ttc -pointsize 30 -fill white -weight bolder -gravity southeast -annotate +20+$height $mid $path
done
fi
|
最后效果如下
5.其他方法
如果不需要水印的话完全可以使用现成的api,放在crontab里每日定时下载,然后feh或者gnome用命令行设置为壁纸,其它的再把旧壁纸改名,新壁纸替换就行。
参考
- 通过此api,你可以获取每日bing主页显示的壁纸 https://api.mayuko.cn/!2
- 每天更新bing首页图片为桌面壁纸 https://blog.csdn.net/u013401853/article/details/73867210
- https://github.com/leon0625/practice/tree/master/bing_wallpaper
- 5种方法获取Bing每日图片 附送高清API接口及网站背景调用 https://www.dujin.org/3618.html