-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbing_img.py
50 lines (38 loc) · 1.44 KB
/
bing_img.py
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
import requests, json
from urllib.parse import urlparse, parse_qs
from tenacity import retry, stop_after_attempt, wait_fixed
import time
@retry(wait=wait_fixed(60), stop=stop_after_attempt(5))
def get_bing_img_info():
info = json.loads(requests.get('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-cn').content)['images'][0]
print(info)
return info
@retry(wait=wait_fixed(60), stop=stop_after_attempt(5))
def post_tg_channel(data):
with open('tg_config.json', 'r') as f:
config = json.load(f)
url = f"https://api.telegram.org/bot{config['token']}/sendPhoto"
payload = {
"chat_id": config['chat_id'],
"photo": f"https://cn.bing.com{data['url']}",
"parse_mode": 'markdown',
"caption": ''
}
caption = ''
date = data['startdate']
caption += f"图片信息: {data['copyright']}\n"
caption += f"发布时间: {date[:4]}年{date[4:6]}月{date[-2:]}日\n"
caption += f"内容详情: [点击访问]({data['copyrightlink']})\n"
caption += f"原图下载: [点击下载](https://cn.bing.com{data['url']})"
payload['caption'] = caption
resp = requests.post(url, data=payload)
def dump_all():
with open('tg_pub.txt', 'r') as f:
lines = f.readlines()
for line in lines:
data = eval(line)
post_tg_channel(data)
time.sleep(5)
if __name__ == '__main__':
post_tg_channel(get_bing_img_info())
# dump_all()