Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ jobs:
- name: Check whitespace errors
run: git diff --check

- name: Lint (ruff)
run: uv run --locked ruff check .

- name: Run tests
run: uv run --locked pytest -q
run: uv run --locked pytest -q --cov --cov-fail-under=80

docker:
name: Docker build
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ __pycache__/
*.py[cod]
*.egg-info/
.pytest_cache/
.coverage
htmlcov/
.ruff_cache/
.venv/
.env
*.db
Expand Down
1 change: 0 additions & 1 deletion goodprice/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from goodprice.main import main


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion goodprice/analysis/judge.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def parse_value_typed(raw: str) -> dict[str, Any]:
try:
score = max(1, min(10, int(data.get("value_score", 0))))
except (TypeError, ValueError):
raise ValueError(f"类型化性价比缺少整数 value_score: {raw!r}")
raise ValueError(f"类型化性价比缺少整数 value_score: {raw!r}") from None
return {
"value_score": score,
"probability": _clamp01(data.get("probability", 0.5)),
Expand Down
6 changes: 3 additions & 3 deletions goodprice/crawler/xianyu.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def search(
card_selector = sel.RESULT_CARD
try:
page.wait_for_selector(card_selector, timeout=30000)
except Exception:
except Exception as exc:
if page.locator(sel.RESULT_CARD_FALLBACK).count() > 0:
card_selector = sel.RESULT_CARD_FALLBACK
else:
Expand All @@ -83,10 +83,10 @@ def search(
if "加载中" in body_text:
raise CrawlerAuthError(
"搜索结果一直显示加载中,Cookie 可能已失效或未登录,请重新获取"
)
) from exc
raise RuntimeError(
f"未在页面中找到商品卡片,页面可能改版或触发风控。页面摘要: {body_text[:150]}"
)
) from exc
# 等待真实结果渲染(刚出现卡片时可能只是占位/推荐位)
page.wait_for_timeout(3000)
for item in parse_search_html(page.content(), card_selector=card_selector):
Expand Down
4 changes: 2 additions & 2 deletions goodprice/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def _make_crawl_service(session_factory, settings_service, guard):
runtime = settings_service.get()
from goodprice.analysis.llm import LLMClient
from goodprice.crawler.xianyu import XianyuAdapter
from goodprice.notify.feishu import FeishuNotifier
from goodprice.notify.gotify import GotifyNotifier
from goodprice.notify.log import LogNotifier
from goodprice.notify.serverchan import ServerChanNotifier
from goodprice.notify.wecom_robot import WeComRobotNotifier
from goodprice.notify.feishu import FeishuNotifier
from goodprice.notify.gotify import GotifyNotifier

adapter = XianyuAdapter(cookie=runtime.xianyu_cookie, proxy=runtime.proxy)
seller_service = SellerService(session_factory, adapter=adapter)
Expand Down
6 changes: 6 additions & 0 deletions goodprice/web/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ def save_settings(
@router.post("/settings/test-notification/{channel}")
def test_notification(request: Request, channel: str):
from fastapi import HTTPException

from goodprice.notify.base import NotificationMessage
from goodprice.notify.feishu import FeishuNotifier
from goodprice.notify.gotify import GotifyNotifier
Expand Down Expand Up @@ -850,7 +851,9 @@ def _safe_test_url(raw: str) -> str:
@router.post("/settings/test-llm")
async def test_llm_endpoint(request: Request):
import time

from fastapi.responses import JSONResponse

from goodprice.analysis.llm import LLMClient

try:
Expand Down Expand Up @@ -894,7 +897,9 @@ async def test_llm_endpoint(request: Request):
@router.post("/settings/test-vision")
async def test_vision_endpoint(request: Request):
import time

from fastapi.responses import JSONResponse

from goodprice.analysis.llm import LLMClient

try:
Expand Down Expand Up @@ -938,6 +943,7 @@ async def test_vision_endpoint(request: Request):
@router.post("/settings/test-jev")
async def test_jev_endpoint(request: Request):
import time

from fastapi.responses import JSONResponse

try:
Expand Down
21 changes: 20 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies = [
]

[project.optional-dependencies]
dev = ["pytest>=8.0", "pytest-cov>=4.1"]
dev = ["pytest>=8.0", "pytest-cov>=4.1", "ruff>=0.6"]
jev = ["typesafe-sdk>=0.7"]

[project.scripts]
Expand All @@ -38,3 +38,22 @@ include = ["goodprice*"]
testpaths = ["tests"]
pythonpath = ["."]

[tool.ruff]
target-version = "py311"
line-length = 100

[tool.ruff.lint]
# 起步基线:pyflakes + 导入排序 + 核心 pycodestyle + bugbear。
# E501 交给将来的 formatter;B008 是 FastAPI 依赖注入惯用法。
select = ["E", "F", "I", "B"]
ignore = ["E501", "B008"]

[tool.ruff.lint.isort]
known-first-party = ["goodprice"]

[tool.coverage.run]
source = ["goodprice"]

[tool.coverage.report]
show_missing = true

1 change: 0 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re
import time

from datetime import datetime

from fastapi.testclient import TestClient
Expand Down
4 changes: 2 additions & 2 deletions tests/test_crawl_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def test_two_tasks_same_keyword_keep_separate_listings(session_factory, base_set
with session_factory() as session:
listings = session.query(Listing).all()
assert len(listings) == 2
assert {l.task_id for l in listings} == {t1.id, t2.id}
assert {listing.task_id for listing in listings} == {t1.id, t2.id}


def test_last_run_count_increments(session_factory, base_settings):
Expand Down Expand Up @@ -599,7 +599,7 @@ def test_exclude_words_filter(session_factory, base_settings):
assert len(notifier.messages) == 1
with session_factory() as session:
listings = session.query(Listing).all()
assert [l.external_id for l in listings] == ["1001"]
assert [listing.external_id for listing in listings] == ["1001"]


def test_requirement_mismatch_blocks_and_skips_vision(session_factory, base_settings):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_judge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import httpx
import pytest

from goodprice.analysis.jev_typesafe import TypeSafeJudger
from goodprice.analysis.judge import (
JevJudger,
build_judger,
parse_requirement_typed,
parse_value_typed,
)
from goodprice.analysis.jev_typesafe import TypeSafeJudger
from goodprice.analysis.llm import LLMClient
from goodprice.config import Settings
from goodprice.crawler.base import ListingData
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from logging.handlers import RotatingFileHandler

from fastapi.testclient import TestClient
from logging.handlers import RotatingFileHandler

from goodprice.main import build_app

Expand Down
9 changes: 4 additions & 5 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest
from sqlalchemy.exc import IntegrityError

from goodprice.models import Listing, Notification, PriceSnapshot, WatchTask
from goodprice.models import Seller
from goodprice.models import Listing, Notification, PriceSnapshot, Seller, WatchTask


def test_watch_task_crud(session_factory):
Expand Down Expand Up @@ -92,11 +91,11 @@ def test_seller_crud_and_listing_columns(session_factory):

def test_blocked_flags(session_factory):
with session_factory() as session:
l = Listing(platform="xianyu", external_id="1", title="t", price=1, url="u", blocked=True)
listing = Listing(platform="xianyu", external_id="1", title="t", price=1, url="u", blocked=True)
s = Seller(platform="xianyu", seller_uid="u1", blocked=True)
session.add_all([l, s])
session.add_all([listing, s])
session.commit()
assert l.blocked is True
assert listing.blocked is True
assert s.blocked is True


Expand Down
2 changes: 1 addition & 1 deletion tests/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import pytest

from goodprice.notify.base import NotificationMessage
from goodprice.notify.feishu import FeishuNotifier
from goodprice.notify.log import LogNotifier
from goodprice.notify.serverchan import ServerChanNotifier
from goodprice.notify.wecom_robot import WeComRobotNotifier
from goodprice.notify.feishu import FeishuNotifier


def test_log_notifier_logs(caplog):
Expand Down
1 change: 0 additions & 1 deletion tests/test_settings_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from goodprice.config import Settings
from goodprice.services.settings_service import RuntimeSettings, SettingsService


Expand Down
31 changes: 29 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading