1 Commits

Author SHA1 Message Date
suyiiyii 5a7396bada 👷 fix case and PYTHONPATH env warning in Dockerfile
test-build / Build Frontend (push) Has been cancelled
test-build / Smoke-test Coverage (macos-latest, 3.10) (push) Has been cancelled
test-build / Smoke-test Coverage (macos-latest, 3.11) (push) Has been cancelled
test-build / Smoke-test Coverage (macos-latest, 3.12) (push) Has been cancelled
test-build / Smoke-test Coverage (ubuntu-latest, 3.10) (push) Has been cancelled
test-build / Smoke-test Coverage (ubuntu-latest, 3.11) (push) Has been cancelled
test-build / Smoke-test Coverage (ubuntu-latest, 3.12) (push) Has been cancelled
test-build / Smoke-test Coverage (windows-latest, 3.10) (push) Has been cancelled
test-build / Smoke-test Coverage (windows-latest, 3.11) (push) Has been cancelled
test-build / Smoke-test Coverage (windows-latest, 3.12) (push) Has been cancelled
test-build / All-test Coverage (macos-latest, 3.10) (push) Has been cancelled
test-build / All-test Coverage (macos-latest, 3.11) (push) Has been cancelled
test-build / All-test Coverage (macos-latest, 3.12) (push) Has been cancelled
test-build / All-test Coverage (ubuntu-latest, 3.10) (push) Has been cancelled
test-build / All-test Coverage (ubuntu-latest, 3.11) (push) Has been cancelled
test-build / All-test Coverage (ubuntu-latest, 3.12) (push) Has been cancelled
test-build / All-test Coverage (windows-latest, 3.10) (push) Has been cancelled
test-build / All-test Coverage (windows-latest, 3.11) (push) Has been cancelled
test-build / All-test Coverage (windows-latest, 3.12) (push) Has been cancelled
pydantic1-compat-test / pydantic1 test (ubuntu-latest, 3.11) (push) Has been cancelled
Ruff Lint / Ruff Lint (push) Has been cancelled
test-build / Docker main (push) Has been cancelled
test-build / Docker main sentry (push) Has been cancelled
2024-12-17 16:29:10 +08:00
5 changed files with 10 additions and 51 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
FROM node:22.11.0 as frontend
FROM node:22.11.0 AS frontend
ADD . /app
WORKDIR /app/admin-frontend
RUN npm install -g pnpm
+3 -4
View File
@@ -1,7 +1,7 @@
# syntax=docker/dockerfile:1.10
FROM python:3.11-slim-bullseye as base
FROM python:3.11-slim-bullseye AS base
FROM base as builder
FROM base AS builder
ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
@@ -12,7 +12,6 @@ ENV PYTHONFAULTHANDLER=1 \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=false \
PATH="$PATH:/runtime/bin" \
PYTHONPATH="$PYTHONPATH:/runtime/lib/python3.10/site-packages" \
# Versions:
POETRY_VERSION=1.7.1
RUN apt-get update && apt-get install -y build-essential unzip wget python-dev git
@@ -25,7 +24,7 @@ COPY README.md pyproject.toml poetry.lock /src/
RUN poetry export --only=main,docker --without-hashes --no-interaction --no-ansi -f requirements.txt -o requirements.txt
RUN pip install --prefix=/runtime --force-reinstall -r requirements.txt
FROM base as runtime
FROM base AS runtime
WORKDIR /app
RUN --mount=type=cache,target=/var/cache/apt \
+3 -4
View File
@@ -1,7 +1,7 @@
# syntax=docker/dockerfile:1.10
FROM python:3.11-slim-bullseye as base
FROM python:3.11-slim-bullseye AS base
FROM base as builder
FROM base AS builder
ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
@@ -12,7 +12,6 @@ ENV PYTHONFAULTHANDLER=1 \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=false \
PATH="$PATH:/runtime/bin" \
PYTHONPATH="$PYTHONPATH:/runtime/lib/python3.10/site-packages" \
# Versions:
POETRY_VERSION=1.7.1
RUN apt-get update && apt-get install -y build-essential unzip wget python3-dev git
@@ -25,7 +24,7 @@ COPY pyproject.toml poetry.lock /src/
RUN poetry export --only=main,docker --without-hashes --no-interaction --no-ansi -f requirements.txt -o requirements.txt
RUN pip install --prefix=/runtime --force-reinstall -r requirements.txt
FROM base as runtime
FROM base AS runtime
WORKDIR /app
RUN --mount=type=cache,target=/var/cache/apt \
+1 -40
View File
@@ -1,17 +1,14 @@
from collections.abc import Callable
from datetime import datetime, timedelta
import json
import random
from typing import TYPE_CHECKING, ClassVar, TypeVar
from typing_extensions import override
from httpx import AsyncClient, Response
from httpx import AsyncClient
from nonebot import logger, require
from playwright.async_api import Cookie
from nonebot_bison.config import config
from nonebot_bison.config.db_model import Cookie as CookieModel
from nonebot_bison.config.db_model import Target
from nonebot_bison.utils import Site, http_client
from nonebot_bison.utils.site import CookieClientManager
@@ -26,8 +23,6 @@ B = TypeVar("B", bound="Bilibili")
class BilibiliClientManager(CookieClientManager):
_default_cookie_cd = timedelta(seconds=120)
_current_user_cookie: CookieModel | None = None
_site_name = "bilibili.com"
async def _get_cookies(self) -> list[Cookie]:
browser = await get_browser()
@@ -62,40 +57,6 @@ class BilibiliClientManager(CookieClientManager):
)
return cookie
def _generate_hook(self, cookie: CookieModel) -> Callable:
"""hook 函数生成器,用于回写请求状态到数据库"""
async def _response_hook(resp: Response):
await resp.aread()
if resp.status_code == 200 and "-352" not in resp.text:
logger.trace(f"请求成功: {cookie.id} {resp.request.url}")
cookie.status = "success"
else:
logger.warning(f"请求失败: {cookie.id} {resp.request.url}, 状态码: {resp.status_code}")
cookie.status = "failed"
self._current_user_cookie = None
cookie.last_usage = datetime.now()
await config.update_cookie(cookie)
return _response_hook
async def _get_next_user_cookie(self) -> CookieModel | None:
"""选择下一个用户 cookie"""
cookies = await config.get_cookie(self._site_name, is_anonymous=False)
available_cookies = [cookie for cookie in cookies if cookie.last_usage + cookie.cd < datetime.now()]
if not available_cookies:
return None
cookie = min(available_cookies, key=lambda x: x.last_usage)
return cookie
async def _choose_cookie(self, target: Target | None) -> CookieModel:
"""选择 cookie 的具体算法"""
if self._current_user_cookie is None:
self._current_user_cookie = await self._get_next_user_cookie()
if self._current_user_cookie:
return self._current_user_cookie
return (await config.get_cookie(self._site_name, is_anonymous=True))[0]
@override
async def refresh_client(self):
await self._refresh_anonymous_cookie()
+2 -2
View File
@@ -124,8 +124,8 @@ class CookieClientManager(ClientManager):
async def _choose_cookie(self, target: Target | None) -> Cookie:
"""选择 cookie 的具体算法"""
cookies = await config.get_cookie(self._site_name, target)
available_cookies = (cookie for cookie in cookies if cookie.last_usage + cookie.cd < datetime.now())
cookie = min(available_cookies, key=lambda x: x.last_usage)
avaliable_cookies = (cookie for cookie in cookies if cookie.last_usage + cookie.cd < datetime.now())
cookie = min(avaliable_cookies, key=lambda x: x.last_usage)
return cookie
async def get_client(self, target: Target | None) -> AsyncClient: