republisher/repub/entrypoint.py

85 lines
2.3 KiB
Python
Raw Normal View History

2026-03-29 13:52:23 +02:00
from __future__ import annotations
import argparse
import logging
2026-03-30 11:42:13 +02:00
import os
2026-03-29 13:52:23 +02:00
import sys
2026-03-30 11:42:13 +02:00
import repub.crawl as crawl_module
from repub.web import create_app
2026-03-30 11:42:13 +02:00
FeedNameFilter = crawl_module.FeedNameFilter
check_runtime = crawl_module.check_runtime
__all__ = ["FeedNameFilter", "check_runtime", "entrypoint", "parse_args"]
2024-04-18 11:57:24 +02:00
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
2026-03-29 13:52:23 +02:00
logger.propagate = False
if not logger.handlers:
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
)
logger.addHandler(handler)
2024-04-18 11:57:24 +02:00
2026-03-30 11:42:13 +02:00
def parse_args(argv: list[str] | None = None) -> tuple[str, argparse.Namespace]:
raw_args = list(argv) if argv is not None else sys.argv[1:]
2024-04-18 11:57:24 +02:00
2026-03-30 11:42:13 +02:00
parser = argparse.ArgumentParser(description="Mirror RSS and Atom feeds")
subparsers = parser.add_subparsers(dest="command")
2024-04-18 11:57:24 +02:00
2026-03-30 11:42:13 +02:00
serve_parser = subparsers.add_parser("serve", help="Start the republisher web UI")
serve_parser.add_argument(
"--host",
default=os.environ.get("REPUB_HOST", "127.0.0.1"),
help="Host interface for the web UI",
)
serve_parser.add_argument(
"--port",
default=os.environ.get("REPUB_PORT", "8080"),
help="Port for the web UI",
)
2024-04-18 11:57:24 +02:00
2026-03-30 11:42:13 +02:00
crawl_parser = subparsers.add_parser("crawl", help="Run the feed crawler once")
crawl_parser.add_argument(
2026-03-29 13:52:23 +02:00
"-c",
"--config",
default="repub.toml",
help="Path to runtime config TOML file",
)
2026-03-30 11:42:13 +02:00
if not raw_args:
raw_args = ["serve"]
elif raw_args[0] in {"-c", "--config"}:
raw_args = ["crawl", *raw_args]
elif raw_args[0] not in {"serve", "crawl"}:
raw_args = ["serve", *raw_args]
2026-03-29 13:52:23 +02:00
2026-03-30 11:42:13 +02:00
args = parser.parse_args(raw_args)
command = args.command or "serve"
return command, args
2026-03-29 13:52:23 +02:00
2026-03-30 11:42:13 +02:00
def entrypoint(argv: list[str] | None = None) -> int:
command, args = parse_args(argv)
2026-03-29 13:52:23 +02:00
2026-03-30 11:42:13 +02:00
if command == "crawl":
crawl_module.check_runtime = check_runtime
return crawl_module.crawl_from_config(args.config)
2026-03-29 13:52:23 +02:00
try:
2026-03-30 11:42:13 +02:00
port = int(args.port)
except ValueError:
logger.error("Invalid REPUB_PORT/--port value: %s", args.port)
return 2
2026-03-29 13:52:23 +02:00
2026-03-30 11:42:13 +02:00
app = create_app()
app.run(host=args.host, port=port)
return 0
2026-03-29 13:52:23 +02:00
if __name__ == "__main__":
sys.exit(entrypoint())