refactor: 改善错误信息的显示方式

master
Mufanc 3 years ago
parent 67ae87f319
commit 703266fd1b

@ -1,11 +1,13 @@
import re import asyncio
import sys
import json import json
import re
import traceback
import urllib.parse as parser
import httpx import httpx
import asyncio
import websockets import websockets
import urllib.parse as parser
from loguru import logger from loguru import logger
from configs import configs from configs import configs
@ -45,8 +47,7 @@ class Browser(object):
logger.info('[账号校验] | 校验通过') logger.info('[账号校验] | 校验通过')
return True return True
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[账号校验] | 账号校验出错:\n{traceback.format_exc()}')
logger.warning(f'[账号校验] | 账号校验出错:{exceptionInformation}')
async def wait_for_page(self, regexp): # 等待符合条件的页面出现 async def wait_for_page(self, regexp): # 等待符合条件的页面出现
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:

@ -1,7 +1,8 @@
import sys import traceback
from os import path
import cv2 import cv2
import numpy as np import numpy as np
from os import path
from loguru import logger from loguru import logger
from numpy import average, dot, linalg from numpy import average, dot, linalg
@ -21,8 +22,7 @@ def similarity(img_1, img_2):
a_norm, b_norm = norms a_norm, b_norm = norms
return dot(a / a_norm, b / b_norm) return dot(a / a_norm, b / b_norm)
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[验证码识别] | 运算出错:\n{traceback.format_exc()}')
logger.warning(f'[验证码识别] | 运算出错:{exceptionInformation}')
def recognize(img_content: bytes): def recognize(img_content: bytes):
@ -40,5 +40,4 @@ def recognize(img_content: bytes):
logger.info(f'[验证码识别] | 识别结果:{code}') logger.info(f'[验证码识别] | 识别结果:{code}')
return code return code
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[验证码识别] | 识别出错:\n{traceback.format_exc()}')
logger.warning(f'[验证码识别] | 识别出错:{exceptionInformation}')

@ -1,8 +1,10 @@
import sys
import json import json
import httpx import traceback
from hashlib import md5 from hashlib import md5
import httpx
from loguru import logger from loguru import logger
from .captcha import recognize from .captcha import recognize
@ -19,8 +21,7 @@ class Tree: # 任务树
for ch in self.child: for ch in self.child:
ch.sort() ch.sort()
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[构建任务树] | 排序出错:\n{traceback.format_exc()}')
logger.warning(f'[构建任务树] | 排序出错:{exceptionInformation}')
class Spider(httpx.AsyncClient): class Spider(httpx.AsyncClient):
@ -58,8 +59,7 @@ class Spider(httpx.AsyncClient):
logger.success('[登录] | 登录成功') logger.success('[登录] | 登录成功')
return info return info
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[登录] | 登录出错:\n{traceback.format_exc()}')
logger.warning(f'[登录] | 登录出错:{exceptionInformation}')
async def get_courses(self): # 获取课程列表 async def get_courses(self): # 获取课程列表
try: try:
@ -75,8 +75,7 @@ class Spider(httpx.AsyncClient):
logger.success('[获取课程列表] | 获取课程列表成功') logger.success('[获取课程列表] | 获取课程列表成功')
return courses return courses
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[获取课程列表] | 获取课程列表出错:\n{traceback.format_exc()}')
logger.warning(f'[获取课程列表] | 获取课程列表出错:{exceptionInformation}')
async def get_books(self, course_id): # 获取某课程的书籍列表 async def get_books(self, course_id): # 获取某课程的书籍列表
try: try:
@ -95,8 +94,7 @@ class Spider(httpx.AsyncClient):
)).json()['data'] )).json()['data']
return books return books
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[获取书籍列表] | 获取书籍列表出错:\n{traceback.format_exc()}')
logger.warning(f'[获取书籍列表] | 获取书籍列表出错:{exceptionInformation}')
async def get_tasks(self, book_id, book_type, course_id): # 获取某书籍的任务树 async def get_tasks(self, book_id, book_type, course_id): # 获取某书籍的任务树
try: try:
@ -133,8 +131,7 @@ class Spider(httpx.AsyncClient):
logger.success('[获取任务列表] | 获取任务列表完成') logger.success('[获取任务列表] | 获取任务列表完成')
return root return root
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[获取任务列表] | 获取任务列表出错:\n{traceback.format_exc()}')
logger.warning(f'[获取任务列表] | 获取任务列表出错:{exceptionInformation}')
async def get_paper(self, paper_id): # 获取任务点信息(包括题目和答案) async def get_paper(self, paper_id): # 获取任务点信息(包括题目和答案)
try: try:
@ -165,8 +162,7 @@ class Spider(httpx.AsyncClient):
logger.success('[获取任务点] | 获取任务点信息完成') logger.success('[获取任务点] | 获取任务点信息完成')
return paper_info return paper_info
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[获取任务点] | 获取任务点出错:\n{traceback.format_exc()}')
logger.warning(f'[获取任务点] | 获取任务点出错:{exceptionInformation}')
async def user_info(self): async def user_info(self):
try: try:
@ -175,8 +171,7 @@ class Spider(httpx.AsyncClient):
logger.success('[获取用户信息] | 获取用户信息完成') logger.success('[获取用户信息] | 获取用户信息完成')
return info return info
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[获取用户信息] | 获取用户信息出错:\n{traceback.format_exc()}')
logger.warning(f'[获取用户信息] | 获取用户信息出错:{exceptionInformation}')
async def book_info(self, book_id): async def book_info(self, book_id):
try: try:
@ -208,5 +203,4 @@ class Spider(httpx.AsyncClient):
logger.success('[获取书籍信息] | 获取书籍信息完成') logger.success('[获取书籍信息] | 获取书籍信息完成')
return book_info return book_info
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[获取书籍信息] | 获取书籍信息出错:\n{traceback.format_exc()}')
logger.warning(f'[获取书籍信息] | 获取书籍信息出错:{exceptionInformation}')

@ -1,7 +1,9 @@
import sys import traceback
from loguru import logger
from bs4 import BeautifulSoup
from random import uniform, randint from random import uniform, randint
from bs4 import BeautifulSoup
from loguru import logger
from configs import configs from configs import configs
from .devtools import Browser from .devtools import Browser
from .spider import Spider from .spider import Spider
@ -31,8 +33,7 @@ async def list_books(detail):
hint = f'{book["bookName"]}' hint = f'{book["bookName"]}'
logger.info(f'[书籍信息] | {hint}') logger.info(f'[书籍信息] | {hint}')
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[读取信息] | 读取信息出错:\n{traceback.format_exc()}')
logger.warning(f'[读取信息] | 读取信息出错:{exceptionInformation}')
async def _random(spider, paper_id): # 随机的分数和学习时长 async def _random(spider, paper_id): # 随机的分数和学习时长
@ -67,8 +68,7 @@ async def _random(spider, paper_id): # 随机的分数和学习时长
return int(100 * score / total), time return int(100 * score / total), time
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[处理随机数据] | 处理随机数据出错:\n{traceback.format_exc()}')
logger.warning(f'[处理随机数据] | 处理随机数据出错:{exceptionInformation}')
async def _flash(course_id, book_id, spider): async def _flash(course_id, book_id, spider):
@ -99,8 +99,7 @@ async def _flash(course_id, book_id, spider):
root = await spider.get_tasks(book_id, book_type, course_id) root = await spider.get_tasks(book_id, book_type, course_id)
await dfs(root) await dfs(root)
except Exception: except Exception:
exceptionInformation = sys.exc_info() logger.warning(f'[刷任务点] | 刷任务点出错:\n{traceback.format_exc()}')
logger.warning(f'[刷任务点] | 刷任务点出错:{exceptionInformation}')
async def flash_by_id(identity): async def flash_by_id(identity):

Loading…
Cancel
Save