zhenxun_bot/plugins/epic/data_source.py

57 lines
2.1 KiB
Python
Raw Normal View History

2021-05-20 19:23:32 +08:00
import aiohttp
import aiofiles
2021-06-30 19:50:55 +08:00
from utils.utils import get_local_proxy
2021-05-20 19:23:32 +08:00
import feedparser
import platform
2021-07-30 21:21:51 +08:00
from utils.message_builder import image
2021-05-20 19:23:32 +08:00
from configs.path_config import IMAGE_PATH
2021-06-30 19:50:55 +08:00
from utils.user_agent import get_user_agent
2021-07-30 21:21:51 +08:00
from asyncio.exceptions import TimeoutError
from configs.config import RSSHUBAPP
2021-05-20 19:23:32 +08:00
2021-07-30 21:21:51 +08:00
if platform.system() == "Windows":
import asyncio
2021-05-20 19:23:32 +08:00
2021-07-30 21:21:51 +08:00
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
2021-05-20 19:23:32 +08:00
2021-07-30 21:21:51 +08:00
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