62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import time
|
|
|
|
from .config import get_settings
|
|
from .db import init_db, session
|
|
from .sync import analyze_pending, run_sync
|
|
|
|
|
|
def _platforms(value: str | None) -> list[str] | None:
|
|
if not value:
|
|
return None
|
|
selected = [part.strip().lower() for part in value.split(",") if part.strip()]
|
|
allowed = {"steam", "twitter"}
|
|
unknown = sorted(set(selected) - allowed)
|
|
if unknown:
|
|
raise argparse.ArgumentTypeError(f"Unsupported platform(s): {', '.join(unknown)}")
|
|
return selected
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="TOHOTOPIA community monitor")
|
|
sub = parser.add_subparsers(dest="command", required=True)
|
|
|
|
sub.add_parser("init-db", help="Initialize SQLite database")
|
|
|
|
sync_parser = sub.add_parser("sync", help="Fetch community content and analyze new items")
|
|
sync_parser.add_argument("--full", action="store_true", help="Run first full scan")
|
|
sync_parser.add_argument(
|
|
"--platform",
|
|
type=_platforms,
|
|
help="Comma-separated platform list: steam,twitter. Defaults to all enabled platforms.",
|
|
)
|
|
|
|
analyze_parser = sub.add_parser("analyze-pending", help="Analyze pending/error items")
|
|
analyze_parser.add_argument("--limit", type=int, default=50)
|
|
analyze_parser.add_argument("--since", help="Only analyze items since YYYY-MM-DD")
|
|
|
|
args = parser.parse_args()
|
|
settings = get_settings()
|
|
with session(settings.database_path) as conn:
|
|
init_db(conn)
|
|
if args.command == "init-db":
|
|
result = {"database": str(settings.database_path)}
|
|
elif args.command == "sync":
|
|
result = run_sync(conn, settings, full=args.full, platforms=args.platform)
|
|
elif args.command == "analyze-pending":
|
|
since_ts = None
|
|
if args.since:
|
|
parsed = time.strptime(args.since, "%Y-%m-%d")
|
|
since_ts = int(time.mktime(parsed))
|
|
result = analyze_pending(conn, settings, limit=args.limit, since_ts=since_ts)
|
|
else:
|
|
raise SystemExit(f"Unknown command: {args.command}")
|
|
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|