Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os
import json
import datetime
import miraicle
import requests
os.chdir(os.path.split(os.path.realpath(__file__))[0])
def get_config(path:str="config.json") -> dict:
if os.path.exists(path)==True:
with open(file=path,mode="r",encoding="utf-8") as fp:
return json.load(fp)
else:
default_config={
"qq":10000,
"port":8080,
"verify_key":"KEY",
"adapter":"ws"
}
with open(path,mode="w",encoding="utf-8") as fp:
json.dump(default_config,fp,ensure_ascii=False,indent=4,sort_keys=True)
return default_config
def bing_daily(msg:miraicle.GroupMessage|miraicle.FriendMessage) -> list:
args=msg.text.strip().lower().split(" ")[1:]
if args==[]:
args=["0"]
elif args[0]=="help":
return [miraicle.Plain("获取每日必应的图片\n参数为距今的天数,默认为 0,即获取当天的图片\n执行 /bingdaily 等价于 /bingdaily 0")]
try:
day=int(args[0])
except Exception:
day=1
realtime=(datetime.datetime.now()-datetime.timedelta(days=day)).strftime("%Y-%m-%d")
image_date=miraicle.Plain(realtime+" 的每日必应图片:\n")
if os.path.exists(os.path.join("cache",realtime+".jpg")):
image=miraicle.Image(base64=os.path.join("cache",realtime+".jpg"))
image_info=miraicle.Plain(copyrights[realtime])
else:
j=requests.get("https://cn.bing.com/HPImageArchive.aspx",params={"format":"js","idx":day,"n":1}).json()
img=j["images"][0]
image_info=miraicle.Plain(text=img["copyright"])
copyrights[realtime]=image_info.text
with open(os.path.join("cache",realtime+".jpg"),mode="wb") as fp:
fp.write(requests.get("https://cn.bing.com"+str(img["url"]).split("&")[0]).content)
with open(os.path.join("cache","copyrights.json"),mode="w",encoding="utf-8") as fp:
json.dump(copyrights,fp,ensure_ascii=False,sort_keys=True,indent=4)
image=miraicle.Image(url="https://cn.bing.com"+str(img["url"]).split("&")[0])
return [image,image_date,image_info]
# Handlers
@miraicle.Mirai.receiver("FriendMessage")
def private_handler(bot: miraicle.Mirai, msg: miraicle.FriendMessage) -> None:
if msg.text.strip().lower()=="/test_bingdaily":
bot.send_friend_msg(qq=msg.sender,msg="测试成功")
elif msg.text.strip().lower().startswith("/bingdaily"):
bot.send_friend_msg(qq=msg.sender,msg=bing_daily(msg))
@miraicle.Mirai.receiver("GroupMessage")
def group_handler(bot:miraicle.Mirai,msg:miraicle.GroupMessage) -> None:
if msg.text.strip().lower()=="/test_bingdaily":
bot.send_group_msg(group=msg.group,msg="测试成功",quote=msg.id)
elif msg.text.strip().lower().startswith("/bingdaily"):
bot.send_group_msg(group=msg.group,msg=bing_daily(msg),quote=msg.id)
if __name__=="__main__":
if os.path.exists("cache")==False:
os.mkdir("cache")
if os.path.exists(os.path.join("cache","copyrights.json"))==False:
with open(os.path.join("cache","copyrights.json"),mode="w",encoding="utf-8") as fp:
json.dump({},fp,ensure_ascii=False,sort_keys=True,indent=4)
print("已生成默认设置,请按照您在 mirai-api-http 的设置编辑 config.json 并重启脚本")
exit(1)
else:
with open(os.path.join("cache","copyrights.json"),mode="r",encoding="utf-8") as fp:
copyrights=json.load(fp)
conf=get_config()
app=miraicle.Mirai(port=conf["port"],verify_key=conf["verify_key"],qq=conf["qq"],adapter=conf["adapter"])
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment