2022-08-30 08:47:01 +00:00
|
|
|
""" PagerMaid module that 60s 看世界新闻 """
|
|
|
|
|
2023-06-10 16:44:52 +00:00
|
|
|
from datetime import date, datetime, timedelta, timezone
|
2022-08-30 08:47:01 +00:00
|
|
|
from os import sep
|
|
|
|
from os.path import isfile
|
2023-06-03 12:22:11 +00:00
|
|
|
from typing import Optional
|
2022-08-30 08:47:01 +00:00
|
|
|
|
|
|
|
from pagermaid import scheduler
|
|
|
|
from pagermaid.enums import Message
|
2023-06-10 16:44:52 +00:00
|
|
|
from pagermaid.listener import listener
|
2022-08-30 08:47:01 +00:00
|
|
|
from pagermaid.services import bot, client
|
|
|
|
from pagermaid.sub_utils import Sub
|
2023-06-10 16:44:52 +00:00
|
|
|
from pagermaid.utils import check_manage_subs, edit_delete
|
|
|
|
from pyrogram.enums.parse_mode import ParseMode
|
|
|
|
|
|
|
|
CACHE_PATH = f"data{sep}news60s.png"
|
2022-08-30 08:47:01 +00:00
|
|
|
|
|
|
|
news60s_sub = Sub("news60s")
|
2023-06-10 16:44:52 +00:00
|
|
|
news60s_cache_date: Optional[date] = None
|
2022-08-30 08:47:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def get_news60s() -> None:
|
2023-06-10 16:44:52 +00:00
|
|
|
global news60s_cache_date
|
|
|
|
today = datetime.now(timezone(timedelta(hours=8))).date()
|
|
|
|
force_update = not isfile(CACHE_PATH)
|
|
|
|
if news60s_cache_date == today and not force_update:
|
|
|
|
return
|
2024-01-07 14:02:56 +00:00
|
|
|
resp = await client.get("https://api.03c3.cn/api/zb?type=jsonImg")
|
2023-06-10 16:44:52 +00:00
|
|
|
res = resp.json()
|
2024-01-08 04:02:08 +00:00
|
|
|
assert res["msg"] == "ok", f"API 返回错误: {res['code']} ({res['msg']})"
|
2024-01-07 14:02:56 +00:00
|
|
|
news_date = datetime.strptime(res["data"]["datetime"], "%Y-%m-%d").date()
|
2023-06-10 16:44:52 +00:00
|
|
|
if news_date == news60s_cache_date and not force_update:
|
2022-08-30 08:47:01 +00:00
|
|
|
return
|
2024-01-07 14:02:56 +00:00
|
|
|
image = await client.get(res["data"]["imageurl"])
|
2023-06-10 16:44:52 +00:00
|
|
|
with open(CACHE_PATH, "wb") as fp:
|
|
|
|
fp.write(image.content)
|
|
|
|
news60s_cache_date = news_date
|
2022-08-30 08:47:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def push_news60s(gid: int) -> None:
|
2023-06-10 16:44:52 +00:00
|
|
|
if isfile(CACHE_PATH):
|
|
|
|
await bot.send_photo(gid, CACHE_PATH)
|
2022-08-30 08:47:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@scheduler.scheduled_job("cron", hour="8", id="news60s.push")
|
|
|
|
async def news60s_subscribe() -> None:
|
|
|
|
await get_news60s()
|
|
|
|
for gid in news60s_sub.get_subs():
|
|
|
|
try:
|
|
|
|
await push_news60s(gid)
|
|
|
|
except Exception as e: # noqa
|
|
|
|
news60s_sub.del_id(gid)
|
|
|
|
|
|
|
|
|
2023-06-03 12:22:11 +00:00
|
|
|
@listener(
|
|
|
|
command="news60s",
|
|
|
|
parameters="订阅/退订",
|
2023-06-10 16:44:52 +00:00
|
|
|
description="查看 60s 看世界新闻,支持订阅/退订每天上午八点定时发送",
|
2023-06-03 12:22:11 +00:00
|
|
|
)
|
2022-08-30 08:47:01 +00:00
|
|
|
async def news60s(message: Message):
|
|
|
|
if not message.arguments:
|
|
|
|
try:
|
|
|
|
await get_news60s()
|
2024-01-08 04:02:08 +00:00
|
|
|
except (ValueError, AssertionError) as e:
|
2022-08-30 08:47:01 +00:00
|
|
|
return await message.edit(e.__str__())
|
|
|
|
await message.safe_delete()
|
|
|
|
await push_news60s(message.chat.id)
|
|
|
|
elif message.arguments == "订阅":
|
|
|
|
if check_manage_subs(message):
|
|
|
|
if news60s_sub.check_id(message.chat.id):
|
2023-07-01 12:18:58 +00:00
|
|
|
return await edit_delete(
|
|
|
|
message, "❌ 你已经订阅了 60s 看世界新闻", parse_mode=ParseMode.HTML
|
|
|
|
)
|
2022-08-30 08:47:01 +00:00
|
|
|
news60s_sub.add_id(message.chat.id)
|
|
|
|
await message.edit("你已经成功订阅了 60s 看世界新闻")
|
|
|
|
else:
|
2023-07-01 12:18:58 +00:00
|
|
|
await edit_delete(
|
|
|
|
message, "❌ 权限不足,无法订阅 60s 看世界新闻", parse_mode=ParseMode.HTML
|
|
|
|
)
|
2022-08-30 08:47:01 +00:00
|
|
|
elif message.arguments == "退订":
|
|
|
|
if check_manage_subs(message):
|
|
|
|
if not news60s_sub.check_id(message.chat.id):
|
2023-07-01 12:18:58 +00:00
|
|
|
return await edit_delete(
|
|
|
|
message, "❌ 你还没有订阅 60s 看世界新闻", parse_mode=ParseMode.HTML
|
|
|
|
)
|
2022-08-30 08:47:01 +00:00
|
|
|
news60s_sub.del_id(message.chat.id)
|
|
|
|
await message.edit("你已经成功退订了 60s 看世界新闻")
|
|
|
|
else:
|
2023-07-01 12:18:58 +00:00
|
|
|
await edit_delete(
|
|
|
|
message, "❌ 权限不足,无法退订 60s 看世界新闻", parse_mode=ParseMode.HTML
|
|
|
|
)
|