84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import logging
|
|
import os
|
|
import sys
|
|
|
|
import repub.crawl as crawl_module
|
|
from repub.web import create_app
|
|
|
|
FeedNameFilter = crawl_module.FeedNameFilter
|
|
check_runtime = crawl_module.check_runtime
|
|
|
|
__all__ = ["FeedNameFilter", "check_runtime", "entrypoint", "parse_args"]
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger.setLevel(logging.DEBUG)
|
|
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)
|
|
|
|
|
|
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:]
|
|
|
|
parser = argparse.ArgumentParser(description="Mirror RSS and Atom feeds")
|
|
subparsers = parser.add_subparsers(dest="command")
|
|
|
|
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",
|
|
)
|
|
|
|
crawl_parser = subparsers.add_parser("crawl", help="Run the feed crawler once")
|
|
crawl_parser.add_argument(
|
|
"-c",
|
|
"--config",
|
|
default="repub.toml",
|
|
help="Path to runtime config TOML file",
|
|
)
|
|
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]
|
|
|
|
args = parser.parse_args(raw_args)
|
|
command = args.command or "serve"
|
|
return command, args
|
|
|
|
|
|
def entrypoint(argv: list[str] | None = None) -> int:
|
|
command, args = parse_args(argv)
|
|
|
|
if command == "crawl":
|
|
crawl_module.check_runtime = check_runtime
|
|
return crawl_module.crawl_from_config(args.config)
|
|
|
|
try:
|
|
port = int(args.port)
|
|
except ValueError:
|
|
logger.error("Invalid REPUB_PORT/--port value: %s", args.port)
|
|
return 2
|
|
|
|
app = create_app()
|
|
app.run(host=args.host, port=port)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(entrypoint())
|