From 008e92e8b99376fdfa0b1cded04185c2dfdafef3 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Thu, 21 Jul 2022 21:18:20 +0800 Subject: [PATCH] =?UTF-8?q?bingwall=20=E8=8E=B7=E5=8F=96=20Bing=20?= =?UTF-8?q?=E6=AF=8F=E6=97=A5=E5=A3=81=E7=BA=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: StarStar-Lab <35412541+StarStar-Lab@users.noreply.github.com> --- bingwall/main.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ list.json | 10 +++++++++ 2 files changed, 68 insertions(+) create mode 100644 bingwall/main.py diff --git a/bingwall/main.py b/bingwall/main.py new file mode 100644 index 0000000..1fbf2ed --- /dev/null +++ b/bingwall/main.py @@ -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() diff --git a/list.json b/list.json index 2ac4038..8ec528e 100644 --- a/list.json +++ b/list.json @@ -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" } ] }