bingwall 获取 Bing 每日壁纸

Co-authored-by: StarStar-Lab <35412541+StarStar-Lab@users.noreply.github.com>
This commit is contained in:
xtaodada 2022-07-21 21:18:20 +08:00
parent c146254bb6
commit 008e92e8b9
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 68 additions and 0 deletions

58
bingwall/main.py Normal file
View File

@ -0,0 +1,58 @@
import secrets
from os import sep
from pagermaid.listener import listener
from pagermaid.single_utils import Message, safe_remove
from pagermaid.utils import client
async def get_wallpaper_url(num):
json_url = f"https://www.bing.com/HPImageArchive.aspx?format=js&mkt=zh-CN&n=1&idx={str(num)}"
req = await client.get(json_url)
url = ""
copy_right = ""
if req.status_code == 200:
data = req.json()
url = data['images'][0]['url']
copy_right = data['images'][0]['copyright']
return url, copy_right
@listener(command="bingwall",
description="获取Bing每日壁纸带参数发送原图")
async def bingwall(message: Message):
status = False
filename = f"data{sep}wallpaper.jpg"
for _ in range(3):
num = secrets.choice(range(7))
url, copy_right = await get_wallpaper_url(num)
image_url = f"https://www.bing.com{url}"
try:
if image_url != " ":
img = await client.get(image_url)
else:
continue
if img.status_code == 200:
with open(filename, "wb") as f:
f.write(img.content)
if message.arguments:
await message.reply_document(
filename,
caption=f"#bing wallpaper\n"
f"{str(copy_right)}",
quote=False)
else:
await message.reply_photo(
filename,
caption=f"#bing wallpaper\n"
f"{str(copy_right)}",
quote=False)
status = True
break # 成功了就赶紧结束啦!
except Exception:
continue
safe_remove(filename)
if not status:
return await message.edit("出错了呜呜呜 ~ 试了好多好多次都无法访问到服务器 。")
await message.safe_delete()

View File

@ -399,6 +399,16 @@
"supported": true,
"des-short": "随机一条网易云音乐评论。",
"des": "随机一条网易云音乐评论。\n指令,netease_comment"
},
{
"name": "bingwall",
"version": "1.0",
"section": "chat",
"maintainer": "xtaodada、ahhhiiii",
"size": "2.0 kb",
"supported": true,
"des-short": "获取 Bing 每日壁纸。",
"des": "获取 Bing 每日壁纸。\n指令,bingwall"
}
]
}