mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-08 04:43:00 +08:00
add http_client
This commit is contained in:
parent
5057d28e6d
commit
905476f9d5
@ -1,12 +1,12 @@
|
|||||||
import json
|
import json
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import httpx
|
|
||||||
from bs4 import BeautifulSoup as bs
|
from bs4 import BeautifulSoup as bs
|
||||||
from nonebot.plugin import require
|
from nonebot.plugin import require
|
||||||
|
|
||||||
from ..post import Post
|
from ..post import Post
|
||||||
from ..types import Category, RawPost, Target
|
from ..types import Category, RawPost, Target
|
||||||
|
from ..utils import http_client
|
||||||
from .platform import CategoryNotSupport, NewMessage, StatusChange
|
from .platform import CategoryNotSupport, NewMessage, StatusChange
|
||||||
|
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ class Arknights(NewMessage):
|
|||||||
return "明日方舟游戏信息"
|
return "明日方舟游戏信息"
|
||||||
|
|
||||||
async def get_sub_list(self, _) -> list[RawPost]:
|
async def get_sub_list(self, _) -> list[RawPost]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
raw_data = await client.get(
|
raw_data = await client.get(
|
||||||
"https://ak-conf.hypergryph.com/config/prod/announce_meta/IOS/announcement.meta.json"
|
"https://ak-conf.hypergryph.com/config/prod/announce_meta/IOS/announcement.meta.json"
|
||||||
)
|
)
|
||||||
@ -44,7 +44,7 @@ class Arknights(NewMessage):
|
|||||||
async def parse(self, raw_post: RawPost) -> Post:
|
async def parse(self, raw_post: RawPost) -> Post:
|
||||||
announce_url = raw_post["webUrl"]
|
announce_url = raw_post["webUrl"]
|
||||||
text = ""
|
text = ""
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
raw_html = await client.get(announce_url)
|
raw_html = await client.get(announce_url)
|
||||||
soup = bs(raw_html.text, "html.parser")
|
soup = bs(raw_html.text, "html.parser")
|
||||||
pics = []
|
pics = []
|
||||||
@ -99,7 +99,7 @@ class AkVersion(StatusChange):
|
|||||||
return "明日方舟游戏信息"
|
return "明日方舟游戏信息"
|
||||||
|
|
||||||
async def get_status(self, _):
|
async def get_status(self, _):
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res_ver = await client.get(
|
res_ver = await client.get(
|
||||||
"https://ak-conf.hypergryph.com/config/prod/official/IOS/version"
|
"https://ak-conf.hypergryph.com/config/prod/official/IOS/version"
|
||||||
)
|
)
|
||||||
@ -155,7 +155,7 @@ class MonsterSiren(NewMessage):
|
|||||||
return "明日方舟游戏信息"
|
return "明日方舟游戏信息"
|
||||||
|
|
||||||
async def get_sub_list(self, _) -> list[RawPost]:
|
async def get_sub_list(self, _) -> list[RawPost]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
raw_data = await client.get("https://monster-siren.hypergryph.com/api/news")
|
raw_data = await client.get("https://monster-siren.hypergryph.com/api/news")
|
||||||
return raw_data.json()["data"]["list"]
|
return raw_data.json()["data"]["list"]
|
||||||
|
|
||||||
@ -170,7 +170,7 @@ class MonsterSiren(NewMessage):
|
|||||||
|
|
||||||
async def parse(self, raw_post: RawPost) -> Post:
|
async def parse(self, raw_post: RawPost) -> Post:
|
||||||
url = f'https://monster-siren.hypergryph.com/info/{raw_post["cid"]}'
|
url = f'https://monster-siren.hypergryph.com/info/{raw_post["cid"]}'
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.get(
|
res = await client.get(
|
||||||
f'https://monster-siren.hypergryph.com/api/news/{raw_post["cid"]}'
|
f'https://monster-siren.hypergryph.com/api/news/{raw_post["cid"]}'
|
||||||
)
|
)
|
||||||
@ -207,7 +207,7 @@ class TerraHistoricusComic(NewMessage):
|
|||||||
return "明日方舟游戏信息"
|
return "明日方舟游戏信息"
|
||||||
|
|
||||||
async def get_sub_list(self, _) -> list[RawPost]:
|
async def get_sub_list(self, _) -> list[RawPost]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
raw_data = await client.get(
|
raw_data = await client.get(
|
||||||
"https://terra-historicus.hypergryph.com/api/recentUpdate"
|
"https://terra-historicus.hypergryph.com/api/recentUpdate"
|
||||||
)
|
)
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import json
|
import json
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
import httpx
|
|
||||||
|
|
||||||
from ..post import Post
|
from ..post import Post
|
||||||
from ..types import Category, RawPost, Tag, Target
|
from ..types import Category, RawPost, Tag, Target
|
||||||
|
from ..utils import http_client
|
||||||
from .platform import CategoryNotSupport, NewMessage
|
from .platform import CategoryNotSupport, NewMessage
|
||||||
|
|
||||||
|
|
||||||
@ -28,7 +27,7 @@ class Bilibili(NewMessage):
|
|||||||
has_target = True
|
has_target = True
|
||||||
|
|
||||||
async def get_target_name(self, target: Target) -> Optional[str]:
|
async def get_target_name(self, target: Target) -> Optional[str]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.get(
|
res = await client.get(
|
||||||
"https://api.bilibili.com/x/space/acc/info", params={"mid": target}
|
"https://api.bilibili.com/x/space/acc/info", params={"mid": target}
|
||||||
)
|
)
|
||||||
@ -38,7 +37,7 @@ class Bilibili(NewMessage):
|
|||||||
return res_data["data"]["name"]
|
return res_data["data"]["name"]
|
||||||
|
|
||||||
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
params = {"host_uid": target, "offset": 0, "need_top": 0}
|
params = {"host_uid": target, "offset": 0, "need_top": 0}
|
||||||
res = await client.get(
|
res = await client.get(
|
||||||
"https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history",
|
"https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history",
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import httpx
|
|
||||||
|
|
||||||
from ..post import Post
|
from ..post import Post
|
||||||
from ..types import RawPost, Target
|
from ..types import RawPost, Target
|
||||||
|
from ..utils import http_client
|
||||||
from .platform import NewMessage
|
from .platform import NewMessage
|
||||||
|
|
||||||
|
|
||||||
@ -23,7 +22,7 @@ class FF14(NewMessage):
|
|||||||
return "最终幻想XIV官方公告"
|
return "最终幻想XIV官方公告"
|
||||||
|
|
||||||
async def get_sub_list(self, _) -> list[RawPost]:
|
async def get_sub_list(self, _) -> list[RawPost]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
raw_data = await client.get(
|
raw_data = await client.get(
|
||||||
"https://ff.web.sdo.com/inc/newdata.ashx?url=List?gameCode=ff&category=5309,5310,5311,5312,5313&pageIndex=0&pageSize=5"
|
"https://ff.web.sdo.com/inc/newdata.ashx?url=List?gameCode=ff&category=5309,5310,5311,5312,5313&pageIndex=0&pageSize=5"
|
||||||
)
|
)
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
import httpx
|
|
||||||
|
|
||||||
from ..post import Post
|
from ..post import Post
|
||||||
from ..types import RawPost, Target
|
from ..types import RawPost, Target
|
||||||
|
from ..utils import http_client
|
||||||
from .platform import NewMessage
|
from .platform import NewMessage
|
||||||
|
|
||||||
|
|
||||||
@ -20,7 +19,7 @@ class NcmArtist(NewMessage):
|
|||||||
has_target = True
|
has_target = True
|
||||||
|
|
||||||
async def get_target_name(self, target: Target) -> Optional[str]:
|
async def get_target_name(self, target: Target) -> Optional[str]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.get(
|
res = await client.get(
|
||||||
"https://music.163.com/api/artist/albums/{}".format(target),
|
"https://music.163.com/api/artist/albums/{}".format(target),
|
||||||
headers={"Referer": "https://music.163.com/"},
|
headers={"Referer": "https://music.163.com/"},
|
||||||
@ -31,7 +30,7 @@ class NcmArtist(NewMessage):
|
|||||||
return res_data["artist"]["name"]
|
return res_data["artist"]["name"]
|
||||||
|
|
||||||
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.get(
|
res = await client.get(
|
||||||
"https://music.163.com/api/artist/albums/{}".format(target),
|
"https://music.163.com/api/artist/albums/{}".format(target),
|
||||||
headers={"Referer": "https://music.163.com/"},
|
headers={"Referer": "https://music.163.com/"},
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
import httpx
|
|
||||||
|
|
||||||
from ..post import Post
|
from ..post import Post
|
||||||
from ..types import RawPost, Target
|
from ..types import RawPost, Target
|
||||||
|
from ..utils import http_client
|
||||||
from .platform import NewMessage
|
from .platform import NewMessage
|
||||||
|
|
||||||
|
|
||||||
@ -20,7 +19,7 @@ class NcmRadio(NewMessage):
|
|||||||
has_target = True
|
has_target = True
|
||||||
|
|
||||||
async def get_target_name(self, target: Target) -> Optional[str]:
|
async def get_target_name(self, target: Target) -> Optional[str]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.post(
|
res = await client.post(
|
||||||
"http://music.163.com/api/dj/program/byradio",
|
"http://music.163.com/api/dj/program/byradio",
|
||||||
headers={"Referer": "https://music.163.com/"},
|
headers={"Referer": "https://music.163.com/"},
|
||||||
@ -32,7 +31,7 @@ class NcmRadio(NewMessage):
|
|||||||
return res_data["programs"][0]["radio"]["name"]
|
return res_data["programs"][0]["radio"]["name"]
|
||||||
|
|
||||||
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.post(
|
res = await client.post(
|
||||||
"http://music.163.com/api/dj/program/byradio",
|
"http://music.163.com/api/dj/program/byradio",
|
||||||
headers={"Referer": "https://music.163.com/"},
|
headers={"Referer": "https://music.163.com/"},
|
||||||
|
@ -2,11 +2,11 @@ import calendar
|
|||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
import feedparser
|
import feedparser
|
||||||
import httpx
|
|
||||||
from bs4 import BeautifulSoup as bs
|
from bs4 import BeautifulSoup as bs
|
||||||
|
|
||||||
from ..post import Post
|
from ..post import Post
|
||||||
from ..types import RawPost, Target
|
from ..types import RawPost, Target
|
||||||
|
from ..utils import http_client
|
||||||
from .platform import NewMessage
|
from .platform import NewMessage
|
||||||
|
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ class Rss(NewMessage):
|
|||||||
has_target = True
|
has_target = True
|
||||||
|
|
||||||
async def get_target_name(self, target: Target) -> Optional[str]:
|
async def get_target_name(self, target: Target) -> Optional[str]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.get(target, timeout=10.0)
|
res = await client.get(target, timeout=10.0)
|
||||||
feed = feedparser.parse(res.text)
|
feed = feedparser.parse(res.text)
|
||||||
return feed["feed"]["title"]
|
return feed["feed"]["title"]
|
||||||
@ -35,7 +35,7 @@ class Rss(NewMessage):
|
|||||||
return post.id
|
return post.id
|
||||||
|
|
||||||
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.get(target, timeout=10.0)
|
res = await client.get(target, timeout=10.0)
|
||||||
feed = feedparser.parse(res)
|
feed = feedparser.parse(res)
|
||||||
entries = feed.entries
|
entries = feed.entries
|
||||||
|
@ -3,12 +3,12 @@ import re
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
import httpx
|
|
||||||
from bs4 import BeautifulSoup as bs
|
from bs4 import BeautifulSoup as bs
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
|
|
||||||
from ..post import Post
|
from ..post import Post
|
||||||
from ..types import *
|
from ..types import *
|
||||||
|
from ..utils import http_client
|
||||||
from .platform import NewMessage
|
from .platform import NewMessage
|
||||||
|
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ class Weibo(NewMessage):
|
|||||||
has_target = True
|
has_target = True
|
||||||
|
|
||||||
async def get_target_name(self, target: Target) -> Optional[str]:
|
async def get_target_name(self, target: Target) -> Optional[str]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
param = {"containerid": "100505" + target}
|
param = {"containerid": "100505" + target}
|
||||||
res = await client.get(
|
res = await client.get(
|
||||||
"https://m.weibo.cn/api/container/getIndex", params=param
|
"https://m.weibo.cn/api/container/getIndex", params=param
|
||||||
@ -42,7 +42,7 @@ class Weibo(NewMessage):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
async def get_sub_list(self, target: Target) -> list[RawPost]:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
params = {"containerid": "107603" + target}
|
params = {"containerid": "107603" + target}
|
||||||
res = await client.get(
|
res = await client.get(
|
||||||
"https://m.weibo.cn/api/container/getIndex?", params=params, timeout=4.0
|
"https://m.weibo.cn/api/container/getIndex?", params=params, timeout=4.0
|
||||||
@ -128,7 +128,7 @@ class Weibo(NewMessage):
|
|||||||
retweeted = True
|
retweeted = True
|
||||||
pic_num = info["retweeted_status"]["pic_num"] if retweeted else info["pic_num"]
|
pic_num = info["retweeted_status"]["pic_num"] if retweeted else info["pic_num"]
|
||||||
if info["isLongText"] or pic_num > 9:
|
if info["isLongText"] or pic_num > 9:
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.get(
|
res = await client.get(
|
||||||
"https://m.weibo.cn/detail/{}".format(info["mid"]), headers=header
|
"https://m.weibo.cn/detail/{}".format(info["mid"]), headers=header
|
||||||
)
|
)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import nonebot
|
import nonebot
|
||||||
from pydantic import BaseSettings
|
from pydantic import BaseSettings
|
||||||
|
|
||||||
@ -15,6 +17,7 @@ class PlugConfig(BaseSettings):
|
|||||||
bison_use_pic_merge: int = 0 # 多图片时启用图片合并转发(仅限群),当bison_use_queue为False时该配置不会生效
|
bison_use_pic_merge: int = 0 # 多图片时启用图片合并转发(仅限群),当bison_use_queue为False时该配置不会生效
|
||||||
# 0:不启用;1:首条消息单独发送,剩余照片合并转发;2以及以上:所有消息全部合并转发
|
# 0:不启用;1:首条消息单独发送,剩余照片合并转发;2以及以上:所有消息全部合并转发
|
||||||
bison_resend_times: int = 0
|
bison_resend_times: int = 0
|
||||||
|
bison_proxy: Optional[str]
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
extra = "ignore"
|
extra = "ignore"
|
||||||
|
@ -3,13 +3,12 @@ from functools import reduce
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
|
|
||||||
import httpx
|
|
||||||
from nonebot.adapters.onebot.v11.message import Message, MessageSegment
|
from nonebot.adapters.onebot.v11.message import Message, MessageSegment
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from .plugin_config import plugin_config
|
from .plugin_config import plugin_config
|
||||||
from .utils import parse_text
|
from .utils import http_client, parse_text
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -34,7 +33,7 @@ class Post:
|
|||||||
async def _pic_url_to_image(self, data: Union[str, bytes]) -> Image.Image:
|
async def _pic_url_to_image(self, data: Union[str, bytes]) -> Image.Image:
|
||||||
pic_buffer = BytesIO()
|
pic_buffer = BytesIO()
|
||||||
if isinstance(data, str):
|
if isinstance(data, str):
|
||||||
async with httpx.AsyncClient() as client:
|
async with http_client() as client:
|
||||||
res = await client.get(data)
|
res = await client.get(data)
|
||||||
pic_buffer.write(res.content)
|
pic_buffer.write(res.content)
|
||||||
else:
|
else:
|
||||||
|
@ -8,7 +8,10 @@ from nonebot.adapters.onebot.v11.message import MessageSegment
|
|||||||
from nonebot.log import default_format, logger
|
from nonebot.log import default_format, logger
|
||||||
from nonebot.plugin import require
|
from nonebot.plugin import require
|
||||||
|
|
||||||
from .plugin_config import plugin_config
|
from ..plugin_config import plugin_config
|
||||||
|
from .http import http_client
|
||||||
|
|
||||||
|
__all__ = ["http_client", "Singleton", "parse_text", "html_to_text"]
|
||||||
|
|
||||||
|
|
||||||
class Singleton(type):
|
class Singleton(type):
|
12
src/plugins/nonebot_bison/utils/http.py
Normal file
12
src/plugins/nonebot_bison/utils/http.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import functools
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from ..plugin_config import plugin_config
|
||||||
|
|
||||||
|
if plugin_config.bison_proxy:
|
||||||
|
http_client = functools.partial(
|
||||||
|
httpx.AsyncClient, proxies=plugin_config.bison_proxy
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
http_client = httpx.AsyncClient
|
Loading…
x
Reference in New Issue
Block a user