Files
rakuten-api/app/scraping/services/rakuma_session.py
T
q792602257andClaude Opus 5 104d7fef6b 拆分抓取与交易服务
把需要账号登录态的链路从抓取服务里拆出成独立进程。分界线不是「要不要登录」,
而是抓取无状态、幂等、可多开实例,而交易的写操作不可逆、登录态全局唯一、
订单监控是常驻轮询——同进程时抓取一扩容就会复制出 N 份登录态与 N 个轮询,
同一账号会被并发操作。

- app/shared:配置、错误码、日志、ApiResponse 信封 + Bearer 鉴权 + 异常处理器、
  导航请求头构造器
- app/scraping:站点常量、会话、解析器与 10 个抓取接口,:31107,可多开
- app/trading:登录态查询/重载与健康检查,:31108,只能单实例
- 依赖方向锁为 scraping→shared、trading→shared,两侧互不 import;
  tests/test_architecture.py 用 AST 检查 import 并校验两个 app 的路径不串
- 登录态 UA 在 trading 独立持有:与抓取 UA 值相同但变更理由不同,抓取 UA 为绕
  反爬可随时调整,登录 UA 一改可能触发设备校验使已落盘 cookie 失效
- scripts/login.py 与 AuthSession 共用 auth_site.PROFILES 与 is_logged_in,判据只写一遍
- 同一镜像两个启动命令,交易容器覆盖 command 并设 RAKUTEN_HEALTH_PORT

同时带上此前未提交的 ラクマ 分类接口与登录态基础设施。

验证:239 个离线用例全绿;两个入口真实启动,/health 与鉴权正常。
未验证:真实探测登录态(当前开发机无外网,对站点的连接全部超时)。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-27 15:05:01 +08:00

136 lines
4.7 KiB
Python

"""ラクマ(fril.jp)站点会话:单通道 HTTP 抓取
与乐天市场那条链路(app/services/site_session.py)分开维护,因为两站的
抓取前提完全不同:
- 乐天前置 Akamai Bot Manager,无 cookie 时每个响应被拖到 ~11s,必须先访问
首页预热再复用 cookie;且搜索页与详情页要用不同 UA,需要两条指纹通道。
- ラクマ 实测没有这类限速:冷请求(无 cookie、无预热)即 0.5-1.1s,与预热后
持平;PC UA 在搜索页、详情页、店铺页上都能拿到完整模板。
因此这里只有一条通道、不做预热,也不接浏览器兜底——没有需要兜底的拦截行为。
若将来站点加了防护,再按乐天那套补预热与升级链路。
"""
from __future__ import annotations
import asyncio
import logging
from typing import Any
import httpx
from app.scraping.core import rakuma_site as site
from app.shared.config import Settings
from app.shared.errors import (
ItemNotFoundError,
ResourceBusyError,
UpstreamBlockedError,
UpstreamRequestError,
)
logger = logging.getLogger(__name__)
class RakumaSession:
"""ラクマ 站点抓取会话,管理并发限流与失败重试"""
def __init__(self, settings: Settings):
self._settings = settings
self._semaphore = asyncio.Semaphore(settings.max_site_concurrency)
self._client: httpx.AsyncClient | None = None
# ---- 生命周期 ----
async def start(self) -> None:
"""创建 HTTP 客户端"""
if self._client is not None:
return
self._client = httpx.AsyncClient(
headers=site.default_headers(),
timeout=self._settings.request_timeout_seconds,
follow_redirects=True,
proxy=self._settings.httpx_proxy,
http2=True,
)
logger.info(
"ラクマ 会话已就绪:concurrency=%s proxy=%s",
self._settings.max_site_concurrency,
bool(self._settings.proxy_server),
)
async def close(self) -> None:
"""关闭 HTTP 客户端"""
if self._client is None:
return
try:
await self._client.aclose()
except Exception:
logger.debug("关闭 ラクマ HTTP 客户端失败", exc_info=True)
self._client = None
# ---- 状态 ----
def status(self) -> dict[str, Any]:
"""会话状态,供健康检查展示"""
return {"ready": self._client is not None}
# ---- 抓取 ----
async def fetch_html(self, url: str) -> str:
"""抓取页面 HTML
Raises:
ItemNotFoundError: 目标页面 404(商品已下架或 ID 不存在)
UpstreamRequestError: 网络异常或上游 5xx
UpstreamBlockedError: 反复取不到正常页面
ResourceBusyError: 等待并发槽位超时
"""
client = self._client
if client is None:
raise UpstreamRequestError("ラクマ 会话尚未初始化")
max_attempts = max(1, self._settings.http_max_attempts)
last_error = "unknown error"
try:
await asyncio.wait_for(
self._semaphore.acquire(),
timeout=self._settings.request_timeout_seconds,
)
except TimeoutError as exc:
raise ResourceBusyError() from exc
try:
for attempt in range(1, max_attempts + 1):
try:
response = await client.get(url)
except httpx.HTTPError as exc:
last_error = f"{type(exc).__name__}: {exc}"
logger.warning(
"ラクマ 抓取请求异常:url=%s attempt=%s/%s err=%s",
url, attempt, max_attempts, last_error,
)
continue
if response.status_code == 404:
raise ItemNotFoundError(f"Page not found: {url}")
if response.status_code < 400:
return response.text
last_error = (
f"upstream status {response.status_code}"
if response.status_code >= 500
else f"status {response.status_code}"
)
logger.warning(
"ラクマ 抓取结果异常:url=%s attempt=%s/%s %s",
url, attempt, max_attempts, last_error,
)
if last_error.startswith("upstream status") or ":" in last_error:
raise UpstreamRequestError(f"Upstream request failed: {last_error}")
raise UpstreamBlockedError(f"Failed to fetch {url}: {last_error}")
finally:
self._semaphore.release()