Skip to content
facron2004Public

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Review Scraper

A Tmall / Taobao review-scraping + analysis system built on FastAPI, Celery, Playwright, PostgreSQL, and Redis.

Recent security & reliability work (audit rounds 1–15)

Rolling fixes on master closed the original code-review findings and more:

  • Authentication. Admin / task-management / download / analysis / WebSocket / /metrics all accept X-API-Key or HTTP Basic Auth. Non-local APP_ENV refuses to start with empty keys (override with ALLOW_INSECURE_AUTH=1). Dashboard JS stores the key in sessionStorage and uses downloadWithApiKey for artifact links.
  • Product uniqueness. UNIQUE(platform, external_product_id) plus a savepoint-based get_or_create_product shared by API + collector.
  • Task lifecycle. start_task CAS is queued-only; late-ack redelivery of done/failed is a no-op. SoftTimeLimit marks the row failed and balances the active gauge. Queued + running stale reapers run at startup and every 5 minutes.
  • Path / injection sandbox. Export filenames, report paths, and delete unlinks are all ensure_within-gated; CSV/XLSX cells are formula-safe.
  • Analysis. Review cap (10k), dialect-native upsert, Redis cache, asyncio.to_thread for jieba, and a 20/min rate limit.
  • Concurrency. Process-local semaphore + Redis Lua global scrape slot; thread fallback refused outside local/test.
  • Ops. /health reports db/redis/workers/stale counts; docker-compose binds to loopback and requires secrets via ${VAR:?}.
  • Alerting (r11). alert() delivers SMTP email via smtp_* / notify_to (STARTTLS or implicit SSL on 465) in addition to the Feishu webhook; deliver=False still logs only. Parser pure helpers now have JSONP/mtop fixture coverage (no network).
  • Failure artifacts (r12). On scrape failure the collector dumps live page HTML + screenshot into the task dir; write_failure_artifacts preserves those instead of overwriting with a traceback wrapper.
  • Retry lineage (r13). task_to_info / TaskInfo expose retry_count + parent_task_id; HTTP /retry keeps the full new_task payload (was silently dropped by the response model).
  • Create-task cache bust (r14). Successful create_tmall_scrape_task now calls invalidate_task_cache so the 30s tasks:list:* and frontend:overview caches cannot hide a just-created task (retry/delete already did this; create did not).
  • Task-list cache policy (r15). List reads and invalidation now share TASK_LIST_CACHE_LIMITS (10/20/50/100). Noncanonical limits bypass cache, while the dashboard's 50/100-item requests are cached and reliably invalidated.

See the recent git log for the full trail.

Quick start (Windows)

Open a terminal in this directory and run:

:: One-off setup (creates .venv, installs deps, installs Playwright)
run.bat

:: Start everything detached (no window)
start.vbs

:: Tail the live log
start_hidden.cmd

:: Stop everything
shutdown.bat

There is no start.bat / stop.bat / service_install.bat in this repo — those were renamed in an earlier audit round to avoid clashing with the Windows start builtin and because we no longer register a Scheduled Task. Please update any local docs you have that still reference those names.

Quick start (Docker)

cp .env.example .env
# Edit .env to set POSTGRES_PASSWORD, ADMIN_API_KEY, ADMIN_METRICS_BASIC_AUTH,
# GRAFANA_ADMIN_PASSWORD.
docker compose up -d

The compose file binds every service to 127.0.0.1 so nothing is exposed on the LAN by default.

Auth model (post-audit)

Endpoint Auth required
GET /, /reviews, /tasks ... none (HTML shell only)
GET/POST /api/reviews/tmall/... X-API-Key (when ADMIN_API_KEY set)
GET /api/products X-API-Key
WS /api/reviews/tmall/tasks/{id}/ws ?api_key= or X-API-Key
GET /api/analysis/tasks/... X-API-Key (20/min)
GET /api/admin/worker-health X-API-Key
POST /api/admin/reap-stale X-API-Key
GET /metrics HTTP Basic (ADMIN_METRICS_BASIC_AUTH)

The dashboard JS reads the key from sessionStorage (key review_scraper_api_key), attaches it to every same-origin fetch(), and uses window.downloadWithApiKey for artifact / report downloads (plain <a href> cannot send the header).

Configuration

Env var Default Notes
DATABASE_URL sqlite:///./data/app.db Postgres recommended for production
REDIS_URL memory:// Set to redis://... for any real use
REDIS_PASSWORD unset Required by docker-compose (:? fail-closed)
APP_ENV local local/test allow empty auth keys
ADMIN_API_KEY "" (disabled) When set, X-API-Key on admin / mutate / analysis / WS
ADMIN_METRICS_BASIC_AUTH "" (disabled) When set, Basic Auth on /metrics
ALLOW_INSECURE_AUTH 0 Non-local escape hatch for empty keys
TRUSTED_HOSTS localhost,127.0.0.1 Host header allow-list outside local/test
SCRAPE_MAX_CONCURRENT 3 Process + Redis-shared in-flight cap
SCRAPE_TASK_TIMEOUT 1800 seconds (30 min) Must exceed realistic task duration
TMALL_LOGIN_TIMEOUT 300 seconds Headed login-wall wait budget
BROWSER_POOL_ENABLED unset (pool disabled) Opt-in long-lived Playwright contexts

Exports always produce CSV and JSON. XLSX is best-effort (requires pandas/openpyxl); when it is missing the UI hides the XLSX download button instead of linking to a 404.

Tests

python -m pytest -q

The project also keeps python -m unittest discover -s tests -v working for non-pytest-aware callers; the two runs cover the same set of assertions today.

Health probes

Endpoint Use as Behaviour
GET /healthz liveness Always 200 if the process accepts HTTP. Body includes version. No DB/Redis I/O.
GET /health readiness Aggregates DB ping, Redis ping, worker heartbeats, and stale task counts. Status is healthy / degraded / unhealthy.

Example readiness payload:

{
  "status": "healthy",
  "components": {
    "api": "up",
    "database": "up",
    "redis": "up",
    "workers": {"any_alive": true, "alive_count": 1},
    "stale_tasks": {"running": 0, "queued": 0}
  }
}

Point orchestrator liveness at /healthz and readiness at /health so a brief Redis blip does not get the API process killed.

License

Internal project, no public license declared.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages