refactor code

This commit is contained in:
hibiki
2021-07-30 21:21:51 +08:00
parent 2ad891aa1e
commit cc24822dca
165 changed files with 7815 additions and 8174 deletions
+14 -29
View File
@@ -7,9 +7,9 @@ from .data_source import get_epic_game
from models.group_remind import GroupRemind
from nonebot.adapters.cqhttp.exception import ActionFailed
__plugin_name__ = 'epic免费游戏提醒'
__plugin_name__ = "epic免费游戏提醒"
__plugin_usage__ = '用法:发送’epic‘'
__plugin_usage__ = "用法:发送’epic‘"
epic = on_command("epic", priority=5, block=True)
@@ -17,44 +17,29 @@ epic = on_command("epic", priority=5, block=True)
@epic.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
# try:
try:
result = await get_epic_game()
except:
result = '网络出错了!'
result, code = await get_epic_game()
await epic.send(result)
logger.info(
f"(USER {event.user_id}, GROUP {event.group_id if event.message_type != 'private' else 'private'})"
f" 获取epic免费游戏")
# except Exception as e:
# logger.error(f'epic 出错 e:{e}')
# await epic.finish('网络好像炸了,再试一次?', at_sender=True)
f" 获取epic免费游戏"
)
# epic免费游戏
@scheduler.scheduled_job(
'cron',
"cron",
hour=12,
minute=1,
)
async def _():
# try:
bot = get_bot()
gl = await bot.get_group_list(self_id=bot.self_id)
gl = [g['group_id'] for g in gl]
gl = [g["group_id"] for g in gl]
for g in gl:
if await GroupRemind.get_status(g, 'epic'):
result = await get_epic_game()
if result == '今天没有游戏可以白嫖了!':
return
try:
await bot.send_group_msg(group_id=g,
message=result)
except ActionFailed:
logger.error(f'{g}群 epic免费游戏推送错误')
# except Exception as e:
# logger.error(f'epic免费游戏推送错误 e:{e}')
if await GroupRemind.get_status(g, "epic"):
result, code = await get_epic_game()
if code == 200:
try:
await bot.send_group_msg(group_id=g, message=result)
except ActionFailed:
logger.error(f"{g}群 epic免费游戏推送错误")
+43 -32
View File
@@ -3,43 +3,54 @@ import aiofiles
from utils.utils import get_local_proxy
import feedparser
import platform
from utils.init_result import image
from utils.message_builder import image
from configs.path_config import IMAGE_PATH
from utils.user_agent import get_user_agent
if platform.system() == 'Windows':
from asyncio.exceptions import TimeoutError
from configs.config import RSSHUBAPP
if platform.system() == "Windows":
import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
url = 'https://rsshub.app/epicgames/freegames'
async def get_epic_game() -> str:
result = '今天没有游戏可以白嫖了!'
async with aiohttp.ClientSession(headers=get_user_agent()) as session:
async with session.get(url, proxy=get_local_proxy(), timeout=7) as response:
data = feedparser.parse(await response.text())['entries']
if len(data) == 0:
return result
index = 0
for item in data:
title = item['title']
img_url = item['summary'][item['summary'].find('src="')+5: item['summary'].rfind('"')]
async with session.get(img_url, proxy=get_local_proxy(), timeout=7) as res:
async with aiofiles.open(IMAGE_PATH + f'temp/epic_{index}.jpg', 'wb') as f:
await f.write(await res.read())
link = item['link']
result += image(f'epic_{index}.jpg', 'temp') + f'\n【游戏】| {title}\n【链接】 | {link}\n'
index += 1
if result != '':
result = 'epic限免游戏(速速白嫖):\n' + result
else:
result = '今天没有游戏可以白嫖了!'
return result
# print(asyncio.get_event_loop().run_until_complete(get_epic_game()))
url = f"{RSSHUBAPP}/epicgames/freegames"
async def get_epic_game() -> "str, int":
result = "今天没有游戏可以白嫖了!"
code = 999
try:
async with aiohttp.ClientSession(headers=get_user_agent()) as session:
async with session.get(url, proxy=get_local_proxy(), timeout=7) as response:
data = feedparser.parse(await response.text())["entries"]
if len(data) == 0:
return result
index = 0
for item in data:
title = item["title"]
img_url = item["summary"][
item["summary"].find('src="') + 5 : item["summary"].rfind('"')
]
async with session.get(
img_url, proxy=get_local_proxy(), timeout=7
) as res:
async with aiofiles.open(
IMAGE_PATH + f"temp/epic_{index}.jpg", "wb"
) as f:
await f.write(await res.read())
link = item["link"]
result += (
image(f"epic_{index}.jpg", "temp")
+ f"\n【游戏】| {title}\n【链接】 | {link}\n"
)
code = 200
index += 1
if result != "":
result = "epic限免游戏(速速白嫖):\n" + result
else:
result = "今天没有游戏可以白嫖了!"
except TimeoutError:
return "请求超时!", code
return result, code