feat: add temp client for bilibili

This commit is contained in:
felinae98 2022-10-03 22:14:33 +08:00
parent 356b5841a5
commit 6851bb5765
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610
2 changed files with 101 additions and 56 deletions

View File

@ -1,14 +1,42 @@
import functools
import json
import re
from datetime import datetime, timedelta
from typing import Any, Optional
import httpx
from nonebot.log import logger
from ..post import Post
from ..types import Category, RawPost, Tag, Target
from ..utils import http_client
from ..utils.http import http_args
from .platform import CategoryNotSupport, NewMessage, StatusChange
class Bilibili(NewMessage):
class _BilibiliClient:
_http_client: httpx.AsyncClient
_client_refresh_time: Optional[datetime]
cookie_expire_time = timedelta(hours=5)
async def _init_session(self):
self._http_client = httpx.AsyncClient(**http_args)
res = await self._http_client.get("https://bilibili.com")
if res.status_code != 200:
logger.warning("unable to refresh temp cookie")
else:
self._client_refresh_time = datetime.now()
async def _refresh_client(self):
if (
self._client_refresh_time is None
or datetime.now() - self._client_refresh_time > self.cookie_expire_time
or self._http_client is None
):
await self._init_session()
class Bilibili(_BilibiliClient, NewMessage):
categories = {
1: "一般动态",
@ -28,9 +56,17 @@ class Bilibili(NewMessage):
has_target = True
parse_target_promot = "请输入用户主页的链接"
def ensure_client(fun):
@functools.wraps(fun)
async def wrapped(self, *args, **kwargs):
await self._refresh_client()
return await fun(self, *args, **kwargs)
return wrapped
@ensure_client
async def get_target_name(self, target: Target) -> Optional[str]:
async with http_client() as client:
res = await client.get(
res = await self._http_client.get(
"https://api.bilibili.com/x/space/acc/info", params={"mid": target}
)
res_data = json.loads(res.text)
@ -48,10 +84,10 @@ class Bilibili(NewMessage):
else:
raise self.ParseTargetException()
@ensure_client
async def get_sub_list(self, target: Target) -> list[RawPost]:
async with http_client() as client:
params = {"host_uid": target, "offset": 0, "need_top": 0}
res = await client.get(
res = await self._http_client.get(
"https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history",
params=params,
timeout=4.0,
@ -157,7 +193,7 @@ class Bilibili(NewMessage):
return Post("bilibili", text=text, url=url, pics=pic, target_name=target_name)
class Bilibililive(StatusChange):
class Bilibililive(_BilibiliClient, StatusChange):
# Author : Sichongzou
# Date : 2022-5-18 8:54
# Description : bilibili开播提醒
@ -172,9 +208,17 @@ class Bilibililive(StatusChange):
name = "Bilibili直播"
has_target = True
def ensure_client(fun):
@functools.wraps(fun)
async def wrapped(self, *args, **kwargs):
await self._refresh_client()
return await fun(self, *args, **kwargs)
return wrapped
@ensure_client
async def get_target_name(self, target: Target) -> Optional[str]:
async with http_client() as client:
res = await client.get(
res = await self._http_client.get(
"https://api.bilibili.com/x/space/acc/info", params={"mid": target}
)
res_data = json.loads(res.text)
@ -182,10 +226,10 @@ class Bilibililive(StatusChange):
return None
return res_data["data"]["name"]
@ensure_client
async def get_status(self, target: Target):
async with http_client() as client:
params = {"mid": target}
res = await client.get(
res = await self._http_client.get(
"https://api.bilibili.com/x/space/acc/info",
params=params,
timeout=4.0,

View File

@ -4,8 +4,9 @@ import httpx
from ..plugin_config import plugin_config
http_client = functools.partial(
httpx.AsyncClient,
proxies=plugin_config.bison_proxy or None,
headers={"user-agent": plugin_config.bison_ua},
)
http_args = {
"proxies": plugin_config.bison_proxy or None,
"headers": {"user-agent": plugin_config.bison_ua},
}
http_client = functools.partial(httpx.AsyncClient, **http_args)