From 595762b06098283740c68bbe410c8682ff68d860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Thu, 1 Sep 2022 09:51:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20`url=5Fto=5Ffile`=20=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E8=B5=84=E6=BA=90=E9=94=99=E8=AF=AF=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8=E5=B9=B6=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/decorators/error.py | 7 ++++++- utils/error.py | 5 +++++ utils/helpers.py | 7 ++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/utils/decorators/error.py b/utils/decorators/error.py index 012c3439..2d48a27a 100644 --- a/utils/decorators/error.py +++ b/utils/decorators/error.py @@ -10,6 +10,7 @@ from telegram.error import BadRequest, TimedOut, Forbidden from telegram.ext import CallbackContext, ConversationHandler from logger import Log +from utils.error import UrlResourcesNotFoundError async def send_user_notification(update: Update, _: CallbackContext, text: str): @@ -73,8 +74,11 @@ def error_callable(func: Callable) -> Callable: Log.error("python-telegram-bot 模块连接服务器 TimedOut") await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ") return ConversationHandler.END + except UrlResourcesNotFoundError as exc: + Log.error("URL数据资源未找到", exc) + await send_user_notification(update, context, "出错了呜呜呜 ~ 资源未找到 ~ ") + return ConversationHandler.END except InvalidCookies as exc: - Log.warning("Cookie错误", exc) if "[10001]" in str(exc): await send_user_notification(update, context, "Cookies无效,请尝试重新绑定账户") elif "[-100]" in str(exc): @@ -82,6 +86,7 @@ def error_callable(func: Callable) -> Callable: elif "[10103]" in str(exc): await send_user_notification(update, context, "Cookie有效,但没有绑定到游戏帐户,请尝试重新绑定邮游戏账户") else: + Log.warning("Cookie错误", exc) await send_user_notification(update, context, "Cookies无效,具体原因未知") return ConversationHandler.END except TooManyRequests as exc: diff --git a/utils/error.py b/utils/error.py index 5f3bc3a7..f29729ab 100644 --- a/utils/error.py +++ b/utils/error.py @@ -12,3 +12,8 @@ class NotFoundError(Exception): class RegionNotFoundError(NotFoundError): entity_name: str = "RegionEnum" entity_value_name: str = "region" + + +class UrlResourcesNotFoundError(NotFoundError): + entity_name: str = "url resources" + entity_value_name: str = "url" diff --git a/utils/helpers.py b/utils/helpers.py index c53f0d1c..654c0016 100644 --- a/utils/helpers.py +++ b/utils/helpers.py @@ -12,6 +12,7 @@ from core.cookies.services import CookiesService from core.user.services import UserService from logger import Log from models.base import RegionEnum +from utils.error import UrlResourcesNotFoundError USER_AGENT: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \ "Chrome/90.0.4430.72 Safari/537.36" @@ -54,9 +55,13 @@ async def url_to_file(url: str, prefix: str = "file://") -> str: return "" if data.is_error: Log.error(f"请求出现错误 url[{url}] status_code[{data.status_code}]") - return "" + raise UrlResourcesNotFoundError(url) + if data.status_code != 200: + Log.error(f"url_to_file 获取url[{url}] 错误 status_code[f{data.status_code}]") + raise UrlResourcesNotFoundError(url) async with aiofiles.open(file_dir, mode='wb') as f: await f.write(data.content) + Log.debug(f"url_to_file 获取url[{url}] 并下载到 file_dir[{file_dir}]") return prefix + file_dir