Skip to content

Commit

Permalink
🎨 更换部分 Lock 为 Semaphore,为专栏请求添加 cookies (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Well2333 authored Sep 3, 2023
1 parent 8f475f6 commit a969f16
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
43 changes: 17 additions & 26 deletions nonebot_plugin_bilichat/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

cd: Dict[str, int] = {}
cd_size_limit = plugin_config.bilichat_cd_time // 2
locks = {}
sem = asyncio.Semaphore(1)


def check_cd(uid: Union[int, str], check: bool = True):
Expand Down Expand Up @@ -93,31 +93,22 @@ async def get_content_info_from_state(state: T_State):


async def get_futuer_fuctions(content: Union[Video, Column, Any]):
global locks
if not (FUTUER_FUCTIONS and content) or not isinstance(content, (Video, Column)):
raise FinishedException

# add lock
if content.id not in locks:
locks[content.id] = asyncio.Lock()

async with locks[content.id]:
try:
subtitle = await content.get_subtitle()
if not subtitle:
raise AbortError("视频无有效字幕")

# wordcloud
wc_image = ""
if plugin_config.bilichat_word_cloud:
wc_image = await wordcloud(cache=content.cache) or "" # type: ignore

# summary
summary = ""
if ENABLE_SUMMARY:
summary = await summarization(cache=content.cache) or "" # type: ignore

return wc_image, summary
finally:
if content.id in locks:
del locks[content.id]
async with sem:
subtitle = await content.get_subtitle()
if not subtitle:
raise AbortError("视频无有效字幕")

# wordcloud
wc_image = ""
if plugin_config.bilichat_word_cloud:
wc_image = await wordcloud(cache=content.cache) or "" # type: ignore

# summary
summary = ""
if ENABLE_SUMMARY:
summary = await summarization(cache=content.cache) or "" # type: ignore

return wc_image, summary
3 changes: 2 additions & 1 deletion nonebot_plugin_bilichat/content/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pydantic import BaseModel

from ..lib.bilibili_request import get_b23_url, hc
from ..lib.bilibili_request.auth import get_cookies
from ..lib.cache import BaseCache, Cache
from ..model.arguments import Options
from ..model.exception import AbortError
Expand All @@ -27,7 +28,7 @@ class Column(BaseModel):
async def from_id(cls, bili_number: str, options: Optional[Options] = None):
try:
cvid = bili_number[2:]
cv = await hc.get(f"https://www.bilibili.com/read/cv{cvid}")
cv = await hc.get(f"https://www.bilibili.com/read/cv{cvid}", cookies=get_cookies())
if cv.status_code != 200:
raise AbortError("未找到此专栏,可能已被 UP 主删除。")
cv.encoding = "utf-8"
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_bilichat/lib/video_subtitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ async def get_subtitle(aid: int, cid: int) -> List[str]:
try:
asr = await get_bcut_asr(audio)
return [x.transcript for x in asr]
except httpx.ReadTimeout as e:
except (httpx.ReadTimeout, httpx.WriteTimeout):
logger.error(
f"except httpx.ReadTimeout, retry count remaining {plugin_config.bilichat_neterror_retry-count-1}"
f"except network error, retry count remaining {plugin_config.bilichat_neterror_retry-count-1}"
)
await asyncio.sleep(5)
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "nonebot-plugin-bilichat"

version = "4.3.0"
version = "4.3.1"

description = "多种B站链接解析,视频词云,AI总结,你想要的都在 bilichat"
authors = [
Expand Down

0 comments on commit a969f16

Please sign in to comment.