PagerMaid_Plugins_Pyro/bingwall/main.py

60 lines
2.1 KiB
Python
Raw Normal View History

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()
2023-07-01 12:18:58 +00:00
url = data["images"][0]["url"]
copy_right = data["images"][0]["copyright"]
return url, copy_right
2023-07-01 12:18:58 +00:00
@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,
2023-07-01 12:18:58 +00:00
caption=f"#bing wallpaper\n" f"{str(copy_right)}",
2022-11-21 11:38:51 +00:00
quote=False,
2024-02-04 07:56:06 +00:00
message_thread_id=message.message_thread_id,
2022-11-21 11:38:51 +00:00
)
else:
await message.reply_photo(
filename,
2023-07-01 12:18:58 +00:00
caption=f"#bing wallpaper\n" f"{str(copy_right)}",
2022-11-21 11:38:51 +00:00
quote=False,
2024-02-04 07:56:06 +00:00
message_thread_id=message.message_thread_id,
2022-11-21 11:38:51 +00:00
)
status = True
break # 成功了就赶紧结束啦!
except Exception:
continue
safe_remove(filename)
if not status:
return await message.edit("出错了呜呜呜 ~ 试了好多好多次都无法访问到服务器 。")
await message.safe_delete()