feat: 监控面板 / 登录鉴权 / 多平台切换 / MySQL
在上游 MediaCrawler 之上新增一层: - 监控层 api/monitor/ —— 多博主/多笔记的定时采集、指标快照差分、报表、 企业微信通知。每轮采集写入独立目录,差分才成立。 - WebUI 登录鉴权 api/auth.py —— PBKDF2 口令 + 服务端会话,/api 全接口防护。 WebSocket 单独加依赖:BaseHTTPMiddleware 对 ws 作用域直接放行,覆盖不到。 - 全局平台切换 + 能力矩阵 —— 如实区分「爬虫模块支持」与「监控层已接线」, 未接通的平台直接拒绝建任务,而不是静默跑空。 - 监控库改用 MySQL 5.7(可回退 SQLite 供测试):逐表强制 utf8mb4 (服务端与库默认都是 latin1),启动校验所连 schema 以防写错库, 连接池 recycle + pre_ping 应对 MySQL 的 8 小时空闲断连。 修复上游缺陷: - xhs/core.py: 主页抓取失败会跳掉整个博主,导致一条作品都抓不到, 而那份资料只喂给一个空函数。改为尽力而为,失败不中断。 - xhs/login.py: cookie 登录只注入 web_session,冷启动签名会失败。 新增 INJECT_ALL_COOKIES 开关(默认关闭,原有行为不变)。 - requirements.txt: 补上 websockets。它在上游 pyproject.toml 里有声明、 这里漏了,导致 uvicorn 没有 WebSocket 能力,实时日志流从未工作。 改动过的上游文件清单及合并方式见 UPSTREAM.md。 测试:492 passed(另有 1 个既有的 Windows/gbk 上游测试失败,与本改动无关)
This commit is contained in:
@@ -0,0 +1,299 @@
|
||||
/** Types for the scheduled monitoring layer. Mirrors api/schemas/monitor.py and
|
||||
* the JSON shapes returned by api/routers/monitor.py. */
|
||||
|
||||
export type MonitorMode = 'creator' | 'note'
|
||||
|
||||
export type RunStatus =
|
||||
| 'pending'
|
||||
| 'running'
|
||||
| 'success'
|
||||
| 'partial'
|
||||
| 'failed'
|
||||
| 'timeout'
|
||||
| 'interrupted'
|
||||
|
||||
export type EventType =
|
||||
| 'new_note'
|
||||
| 'new_comment_posted'
|
||||
| 'new_comment_seen'
|
||||
| 'metric_delta'
|
||||
| 'run_failed'
|
||||
| 'suspected_auth_failure'
|
||||
| 'no_data_found'
|
||||
|
||||
export interface MonitorTarget {
|
||||
id: number
|
||||
external_id: string
|
||||
raw_value: string
|
||||
enabled: boolean
|
||||
}
|
||||
|
||||
export interface MonitorTask {
|
||||
id: number
|
||||
name: string
|
||||
platform: string
|
||||
mode: MonitorMode
|
||||
enabled: boolean
|
||||
interval_minutes: number
|
||||
max_notes_count: number
|
||||
enable_comments: boolean
|
||||
max_comments_count: number
|
||||
run_timeout_seconds: number
|
||||
/** Opt-in per task so one webhook does not get flooded. */
|
||||
notify_enabled: boolean
|
||||
/** Epoch milliseconds. */
|
||||
next_run_at: number | null
|
||||
last_run_at: number | null
|
||||
last_status: string
|
||||
last_error: string | null
|
||||
last_notified_at: number | null
|
||||
target_count: number
|
||||
targets: MonitorTarget[]
|
||||
unread_events: number
|
||||
}
|
||||
|
||||
/** Any count may be null: a value the platform returned that we could not parse
|
||||
* is stored as null rather than 0, so it is never mistaken for a real change. */
|
||||
export interface NoteMetrics {
|
||||
liked_count: number | null
|
||||
comment_count: number | null
|
||||
collected_count: number | null
|
||||
share_count: number | null
|
||||
}
|
||||
|
||||
export interface MonitorNote {
|
||||
task_id: number
|
||||
note_id: string
|
||||
title: string
|
||||
note_url: string
|
||||
cover: string
|
||||
first_seen_at: number
|
||||
last_seen_at: number
|
||||
is_new: boolean
|
||||
metrics: NoteMetrics
|
||||
/** Change vs the previous run; null when either side is unknown. */
|
||||
deltas: NoteMetrics
|
||||
snapshot_count: number
|
||||
}
|
||||
|
||||
export interface MetricPoint {
|
||||
run_id: number
|
||||
captured_at: number
|
||||
liked_count: number | null
|
||||
comment_count: number | null
|
||||
collected_count: number | null
|
||||
share_count: number | null
|
||||
}
|
||||
|
||||
export interface MonitorComment {
|
||||
task_id: number
|
||||
note_id: string
|
||||
comment_id: string
|
||||
content: string
|
||||
nickname: string
|
||||
create_time: number | null
|
||||
like_count: number | null
|
||||
sub_comment_count: number
|
||||
first_seen_at: number
|
||||
/** The work this comment sits under. Empty when the note row is missing. */
|
||||
note_title: string
|
||||
note_cover: string
|
||||
note_url: string
|
||||
}
|
||||
|
||||
/** One work that has comments, for the filter dropdown. */
|
||||
export interface CommentNoteOption {
|
||||
note_id: string
|
||||
note_title: string
|
||||
note_cover: string
|
||||
note_url: string
|
||||
comment_count: number
|
||||
latest_at: number
|
||||
}
|
||||
|
||||
/** Comments bucketed under their work, as returned by `group_by=note`. */
|
||||
export interface CommentBucket {
|
||||
note_id: string
|
||||
note_title: string
|
||||
note_cover: string
|
||||
note_url: string
|
||||
comments: MonitorComment[]
|
||||
}
|
||||
|
||||
export interface MonitorEvent {
|
||||
id: number
|
||||
task_id: number
|
||||
run_id: number | null
|
||||
type: EventType
|
||||
severity: 'info' | 'warning' | 'error'
|
||||
target_kind: string
|
||||
target_id: string
|
||||
title: string
|
||||
created_at: number
|
||||
is_read: boolean
|
||||
}
|
||||
|
||||
export interface MonitorRun {
|
||||
id: number
|
||||
task_id: number
|
||||
status: RunStatus
|
||||
trigger: string
|
||||
queued_at: number
|
||||
started_at: number | null
|
||||
finished_at: number | null
|
||||
exit_code: number | null
|
||||
notes_fetched: number
|
||||
comments_fetched: number
|
||||
new_notes: number
|
||||
new_comments: number
|
||||
/** The first successful run of a task; it establishes the baseline and
|
||||
* deliberately emits no change events. */
|
||||
is_baseline: boolean
|
||||
max_comments_count: number
|
||||
error_message: string | null
|
||||
}
|
||||
|
||||
export interface CookieStatus {
|
||||
present: boolean
|
||||
length: number
|
||||
updated_at: number | null
|
||||
/** Last run that actually authenticated — the useful expiry signal. */
|
||||
last_ok_at: number | null
|
||||
}
|
||||
|
||||
export interface MonitorOverview {
|
||||
tasks: number
|
||||
enabled_tasks: number
|
||||
notes: number
|
||||
comments: number
|
||||
events_24h: number
|
||||
unread_events: number
|
||||
running_runs: number
|
||||
}
|
||||
|
||||
export interface TaskCreatePayload {
|
||||
name: string
|
||||
mode: MonitorMode
|
||||
interval_minutes: number
|
||||
max_notes_count: number
|
||||
enable_comments: boolean
|
||||
max_comments_count: number
|
||||
run_timeout_seconds: number
|
||||
enabled: boolean
|
||||
notify_enabled: boolean
|
||||
targets: string[]
|
||||
}
|
||||
|
||||
// --- Report ---------------------------------------------------------------
|
||||
|
||||
/** Metric keys used by both the per-day rows and the totals. */
|
||||
export type MetricKey =
|
||||
| 'liked_count_delta'
|
||||
| 'comment_count_delta'
|
||||
| 'collected_count_delta'
|
||||
| 'share_count_delta'
|
||||
|
||||
export interface ReportRow {
|
||||
/** ISO date. */
|
||||
date: string
|
||||
new_notes: number
|
||||
new_comments: number
|
||||
liked_count_delta: number
|
||||
comment_count_delta: number
|
||||
collected_count_delta: number
|
||||
share_count_delta: number
|
||||
/** Metrics that could not be compared on this day (unparseable counts). */
|
||||
partial_metrics: string[]
|
||||
}
|
||||
|
||||
export interface ReportTotals {
|
||||
new_notes: number
|
||||
new_comments: number
|
||||
liked_count_delta: number
|
||||
comment_count_delta: number
|
||||
collected_count_delta: number
|
||||
share_count_delta: number
|
||||
}
|
||||
|
||||
export interface ReportResult {
|
||||
start_date: string
|
||||
end_date: string
|
||||
/** null means "all tasks". */
|
||||
task_ids: number[] | null
|
||||
rows: ReportRow[]
|
||||
totals: ReportTotals
|
||||
note_count: number
|
||||
has_partial_data: boolean
|
||||
partial_metrics: string[]
|
||||
metric_labels: Record<string, string>
|
||||
}
|
||||
|
||||
// --- WeCom webhook --------------------------------------------------------
|
||||
|
||||
/**
|
||||
* What one platform can do. `crawler_modes` / `metrics` / `comment_levels`
|
||||
* describe the upstream crawler; `monitor_wired` says whether the monitoring
|
||||
* layer has been hooked up for it. The UI must never conflate the two -- a
|
||||
* platform can be fully crawlable upstream and still unusable here.
|
||||
*/
|
||||
export interface PlatformCapability {
|
||||
value: string
|
||||
label: string
|
||||
crawler_modes: string[]
|
||||
metrics: string[]
|
||||
metric_labels: Record<string, string>
|
||||
comment_levels: number
|
||||
media: boolean
|
||||
monitor_wired: boolean
|
||||
}
|
||||
|
||||
export interface WebhookStatus {
|
||||
configured: boolean
|
||||
/** Key is masked server-side; the full URL is never returned. */
|
||||
masked: string
|
||||
}
|
||||
|
||||
// --- Settings -------------------------------------------------------------
|
||||
|
||||
export type SettingType = 'bool' | 'int' | 'str' | 'secret'
|
||||
export type SettingCategory = 'login' | 'notify' | 'collect' | 'proxy'
|
||||
|
||||
/**
|
||||
* `platform` settings are stored per platform; `system` settings are shared by
|
||||
* the whole instance. The two are rendered on different screens -- putting a
|
||||
* system value inside a per-platform page would imply a scope it does not have.
|
||||
*/
|
||||
export type SettingScope = 'platform' | 'system'
|
||||
|
||||
/**
|
||||
* Describes one setting. Supplied by the server so the form is generated from
|
||||
* the same registry the backend validates against -- adding a setting does not
|
||||
* mean editing a matching list here.
|
||||
*/
|
||||
export interface SettingSpec {
|
||||
/** Fully resolved storage key, e.g. `platform.xhs.crawl_sleep_sec`. */
|
||||
key: string
|
||||
/** Leaf name, e.g. `crawl_sleep_sec`. */
|
||||
name: string
|
||||
scope: SettingScope
|
||||
type: SettingType
|
||||
label: string
|
||||
help: string
|
||||
default: unknown
|
||||
minimum: number | null
|
||||
maximum: number | null
|
||||
choices: string[] | null
|
||||
affects_new_runs: boolean
|
||||
}
|
||||
|
||||
export interface SecretStatus {
|
||||
present: boolean
|
||||
length: number
|
||||
}
|
||||
|
||||
export interface SettingsResponse {
|
||||
values: Record<string, boolean | number | string>
|
||||
/** Secrets are never returned as values -- only whether one is set. */
|
||||
secrets: Record<string, SecretStatus>
|
||||
specs: SettingSpec[]
|
||||
}
|
||||
Reference in New Issue
Block a user