From 703266fd1b4c2e0636819f59b59a04ab87f12e8a Mon Sep 17 00:00:00 2001 From: Mufanc <1714511787@qq.com> Date: Tue, 22 Mar 2022 21:35:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=94=B9=E5=96=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=A1=E6=81=AF=E7=9A=84=E6=98=BE=E7=A4=BA=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- automaton/devtools.py | 13 +++++++------ automaton/spider/captcha/captcha.py | 11 +++++------ automaton/spider/spider.py | 30 ++++++++++++----------------- automaton/utils.py | 17 ++++++++-------- 4 files changed, 32 insertions(+), 39 deletions(-) diff --git a/automaton/devtools.py b/automaton/devtools.py index 19f2482..258e6af 100644 --- a/automaton/devtools.py +++ b/automaton/devtools.py @@ -1,11 +1,13 @@ -import re -import sys +import asyncio import json +import re +import traceback +import urllib.parse as parser + import httpx -import asyncio import websockets -import urllib.parse as parser from loguru import logger + from configs import configs @@ -45,8 +47,7 @@ class Browser(object): logger.info('[账号校验] | 校验通过') return True except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[账号校验] | 账号校验出错:{exceptionInformation}') + logger.warning(f'[账号校验] | 账号校验出错:\n{traceback.format_exc()}') async def wait_for_page(self, regexp): # 等待符合条件的页面出现 async with httpx.AsyncClient() as client: diff --git a/automaton/spider/captcha/captcha.py b/automaton/spider/captcha/captcha.py index ad73cc4..7d9f409 100644 --- a/automaton/spider/captcha/captcha.py +++ b/automaton/spider/captcha/captcha.py @@ -1,7 +1,8 @@ -import sys +import traceback +from os import path + import cv2 import numpy as np -from os import path from loguru import logger from numpy import average, dot, linalg @@ -21,8 +22,7 @@ def similarity(img_1, img_2): a_norm, b_norm = norms return dot(a / a_norm, b / b_norm) except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[验证码识别] | 运算出错:{exceptionInformation}') + logger.warning(f'[验证码识别] | 运算出错:\n{traceback.format_exc()}') def recognize(img_content: bytes): @@ -40,5 +40,4 @@ def recognize(img_content: bytes): logger.info(f'[验证码识别] | 识别结果:{code}') return code except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[验证码识别] | 识别出错:{exceptionInformation}') + logger.warning(f'[验证码识别] | 识别出错:\n{traceback.format_exc()}') diff --git a/automaton/spider/spider.py b/automaton/spider/spider.py index 09767db..6b50430 100644 --- a/automaton/spider/spider.py +++ b/automaton/spider/spider.py @@ -1,8 +1,10 @@ -import sys import json -import httpx +import traceback from hashlib import md5 + +import httpx from loguru import logger + from .captcha import recognize @@ -19,8 +21,7 @@ class Tree: # 任务树 for ch in self.child: ch.sort() except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[构建任务树] | 排序出错:{exceptionInformation}') + logger.warning(f'[构建任务树] | 排序出错:\n{traceback.format_exc()}') class Spider(httpx.AsyncClient): @@ -58,8 +59,7 @@ class Spider(httpx.AsyncClient): logger.success('[登录] | 登录成功') return info except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[登录] | 登录出错:{exceptionInformation}') + logger.warning(f'[登录] | 登录出错:\n{traceback.format_exc()}') async def get_courses(self): # 获取课程列表 try: @@ -75,8 +75,7 @@ class Spider(httpx.AsyncClient): logger.success('[获取课程列表] | 获取课程列表成功') return courses except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[获取课程列表] | 获取课程列表出错:{exceptionInformation}') + logger.warning(f'[获取课程列表] | 获取课程列表出错:\n{traceback.format_exc()}') async def get_books(self, course_id): # 获取某课程的书籍列表 try: @@ -95,8 +94,7 @@ class Spider(httpx.AsyncClient): )).json()['data'] return books except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[获取书籍列表] | 获取书籍列表出错:{exceptionInformation}') + logger.warning(f'[获取书籍列表] | 获取书籍列表出错:\n{traceback.format_exc()}') async def get_tasks(self, book_id, book_type, course_id): # 获取某书籍的任务树 try: @@ -133,8 +131,7 @@ class Spider(httpx.AsyncClient): logger.success('[获取任务列表] | 获取任务列表完成') return root except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[获取任务列表] | 获取任务列表出错:{exceptionInformation}') + logger.warning(f'[获取任务列表] | 获取任务列表出错:\n{traceback.format_exc()}') async def get_paper(self, paper_id): # 获取任务点信息(包括题目和答案) try: @@ -165,8 +162,7 @@ class Spider(httpx.AsyncClient): logger.success('[获取任务点] | 获取任务点信息完成') return paper_info except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[获取任务点] | 获取任务点出错:{exceptionInformation}') + logger.warning(f'[获取任务点] | 获取任务点出错:\n{traceback.format_exc()}') async def user_info(self): try: @@ -175,8 +171,7 @@ class Spider(httpx.AsyncClient): logger.success('[获取用户信息] | 获取用户信息完成') return info except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[获取用户信息] | 获取用户信息出错:{exceptionInformation}') + logger.warning(f'[获取用户信息] | 获取用户信息出错:\n{traceback.format_exc()}') async def book_info(self, book_id): try: @@ -208,5 +203,4 @@ class Spider(httpx.AsyncClient): logger.success('[获取书籍信息] | 获取书籍信息完成') return book_info except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[获取书籍信息] | 获取书籍信息出错:{exceptionInformation}') + logger.warning(f'[获取书籍信息] | 获取书籍信息出错:\n{traceback.format_exc()}') diff --git a/automaton/utils.py b/automaton/utils.py index 20e2f4a..0291386 100644 --- a/automaton/utils.py +++ b/automaton/utils.py @@ -1,7 +1,9 @@ -import sys -from loguru import logger -from bs4 import BeautifulSoup +import traceback from random import uniform, randint + +from bs4 import BeautifulSoup +from loguru import logger + from configs import configs from .devtools import Browser from .spider import Spider @@ -31,8 +33,7 @@ async def list_books(detail): hint = f'{book["bookName"]}' logger.info(f'[书籍信息] | {hint}') except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[读取信息] | 读取信息出错:{exceptionInformation}') + logger.warning(f'[读取信息] | 读取信息出错:\n{traceback.format_exc()}') async def _random(spider, paper_id): # 随机的分数和学习时长 @@ -67,8 +68,7 @@ async def _random(spider, paper_id): # 随机的分数和学习时长 return int(100 * score / total), time except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[处理随机数据] | 处理随机数据出错:{exceptionInformation}') + logger.warning(f'[处理随机数据] | 处理随机数据出错:\n{traceback.format_exc()}') 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) await dfs(root) except Exception: - exceptionInformation = sys.exc_info() - logger.warning(f'[刷任务点] | 刷任务点出错:{exceptionInformation}') + logger.warning(f'[刷任务点] | 刷任务点出错:\n{traceback.format_exc()}') async def flash_by_id(identity):