Skip to content

Commit

Permalink
优化目前已知问题的日志输出
Browse files Browse the repository at this point in the history
  • Loading branch information
SK-415 committed Feb 14, 2021
1 parent bfec42c commit ad30c6c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
22 changes: 15 additions & 7 deletions src/plugins/haruka_bot/bilireq.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import httpx
import qrcode
from httpx import ConnectTimeout, ReadTimeout
from nonebot.log import logger

from .config import Config

Expand All @@ -23,18 +25,24 @@ def __init__(self):
'Referer': 'https://www.bilibili.com/'
}
self.login = Config.get_login()

async def get(self, url, **kw):

async def request(self, method, url, **kw):
# TODO 处理 -412
async with httpx.AsyncClient(trust_env=False) as client:
r = await client.get(url, **kw)
try:
r = await client.request(method, url, **kw)
except ConnectTimeout:
logger.error(f"连接超时({url})")
except ReadTimeout:
logger.error(f"接收超时({url})")
r.encoding = 'utf-8'
return r

async def get(self, url, **kw):
return await self.request('GET', url, **kw)

async def post(self, url, **kw):
async with httpx.AsyncClient(trust_env=False) as client:
r = await client.post(url, **kw)
r.encoding = 'utf-8'
return r
return await self.request('POST', url, **kw)

async def get_info(self, uid):
url = f'https://api.bilibili.com/x/space/acc/info?mid={uid}'
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/haruka_bot/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ async def get_screenshot(self, retry=3):
await page.screenshot(clip=clip, encoding='base64')
break
except TimeoutError as e:
logger.error(f"截图失败连接超时。已重试 {i} 次,剩余 {retry - i}")
logger.error(f"截图失败连接超时) 已重试({i}/{retry}")
except BadStatusLine as e:
logger.error(f"截图失败连接错误。已重试 {i} 次,剩余 {retry - i}")
logger.error(f"截图失败连接错误) 已重试({i}/{retry}")
except:
logger.error("截图失败未知错误")
logger.error("截图失败未知错误),以下为错误日志")
await browser.close()
raise
finally:
Expand Down
9 changes: 1 addition & 8 deletions src/plugins/haruka_bot/dynamic_pusher.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import time
import os
from datetime import datetime, timedelta

import nonebot
from nonebot.log import logger

from .bilireq import BiliReq
from .config import Config
from .dynamic import Dynamic
from .utils import safe_send, scheduler
from .bilireq import BiliReq


last_time = {}

Expand Down Expand Up @@ -41,14 +37,11 @@ async def dy_sched():
for dynamic in dynamics[4::-1]: # 从旧到新取最近5条动态
dynamic = Dynamic(dynamic)
if dynamic.time > last_time[uid] and dynamic.time > datetime.now().timestamp() - timedelta(minutes=10).seconds:
start = time.time()
logger.info("开始截图")
try:
await dynamic.get_screenshot()
except AttributeError:
return
await dynamic.format()
logger.info(f"截图成功,共耗时{time.time()-start}")

for sets in push_list:
await safe_send(sets['bot_id'], sets['type'], sets['type_id'], dynamic.message)
Expand Down

0 comments on commit ad30c6c

Please sign in to comment.