mirror of
https://github.com/PaiGramTeam/PaiGram.git
synced 2024-11-21 22:58:05 +00:00
🎨 url_to_file
获取资源错误的时抛出异常并捕获
This commit is contained in:
parent
400ba876bf
commit
595762b060
@ -10,6 +10,7 @@ from telegram.error import BadRequest, TimedOut, Forbidden
|
|||||||
from telegram.ext import CallbackContext, ConversationHandler
|
from telegram.ext import CallbackContext, ConversationHandler
|
||||||
|
|
||||||
from logger import Log
|
from logger import Log
|
||||||
|
from utils.error import UrlResourcesNotFoundError
|
||||||
|
|
||||||
|
|
||||||
async def send_user_notification(update: Update, _: CallbackContext, text: str):
|
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")
|
Log.error("python-telegram-bot 模块连接服务器 TimedOut")
|
||||||
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ")
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ")
|
||||||
return ConversationHandler.END
|
return ConversationHandler.END
|
||||||
|
except UrlResourcesNotFoundError as exc:
|
||||||
|
Log.error("URL数据资源未找到", exc)
|
||||||
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 资源未找到 ~ ")
|
||||||
|
return ConversationHandler.END
|
||||||
except InvalidCookies as exc:
|
except InvalidCookies as exc:
|
||||||
Log.warning("Cookie错误", exc)
|
|
||||||
if "[10001]" in str(exc):
|
if "[10001]" in str(exc):
|
||||||
await send_user_notification(update, context, "Cookies无效,请尝试重新绑定账户")
|
await send_user_notification(update, context, "Cookies无效,请尝试重新绑定账户")
|
||||||
elif "[-100]" in str(exc):
|
elif "[-100]" in str(exc):
|
||||||
@ -82,6 +86,7 @@ def error_callable(func: Callable) -> Callable:
|
|||||||
elif "[10103]" in str(exc):
|
elif "[10103]" in str(exc):
|
||||||
await send_user_notification(update, context, "Cookie有效,但没有绑定到游戏帐户,请尝试重新绑定邮游戏账户")
|
await send_user_notification(update, context, "Cookie有效,但没有绑定到游戏帐户,请尝试重新绑定邮游戏账户")
|
||||||
else:
|
else:
|
||||||
|
Log.warning("Cookie错误", exc)
|
||||||
await send_user_notification(update, context, "Cookies无效,具体原因未知")
|
await send_user_notification(update, context, "Cookies无效,具体原因未知")
|
||||||
return ConversationHandler.END
|
return ConversationHandler.END
|
||||||
except TooManyRequests as exc:
|
except TooManyRequests as exc:
|
||||||
|
@ -12,3 +12,8 @@ class NotFoundError(Exception):
|
|||||||
class RegionNotFoundError(NotFoundError):
|
class RegionNotFoundError(NotFoundError):
|
||||||
entity_name: str = "RegionEnum"
|
entity_name: str = "RegionEnum"
|
||||||
entity_value_name: str = "region"
|
entity_value_name: str = "region"
|
||||||
|
|
||||||
|
|
||||||
|
class UrlResourcesNotFoundError(NotFoundError):
|
||||||
|
entity_name: str = "url resources"
|
||||||
|
entity_value_name: str = "url"
|
||||||
|
@ -12,6 +12,7 @@ from core.cookies.services import CookiesService
|
|||||||
from core.user.services import UserService
|
from core.user.services import UserService
|
||||||
from logger import Log
|
from logger import Log
|
||||||
from models.base import RegionEnum
|
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) " \
|
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"
|
"Chrome/90.0.4430.72 Safari/537.36"
|
||||||
@ -54,9 +55,13 @@ async def url_to_file(url: str, prefix: str = "file://") -> str:
|
|||||||
return ""
|
return ""
|
||||||
if data.is_error:
|
if data.is_error:
|
||||||
Log.error(f"请求出现错误 url[{url}] status_code[{data.status_code}]")
|
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:
|
async with aiofiles.open(file_dir, mode='wb') as f:
|
||||||
await f.write(data.content)
|
await f.write(data.content)
|
||||||
|
Log.debug(f"url_to_file 获取url[{url}] 并下载到 file_dir[{file_dir}]")
|
||||||
return prefix + file_dir
|
return prefix + file_dir
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user