Files
rss-to-opds/tools/fake-freshrss.py
2026-11-15 18:15:03 -05:00

146 lines
5.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""Run a fake FreshRSS, so the whole server can be tried without an account.
Serves the same in-process stand-in the test suite uses: three feeds across two
categories, a starred article, an already-read article, a feed with no category,
and article bodies that exercise the sanitiser. It also writes a ready-to-run
configuration pointing at itself, with a known demo password.
Run locally::
python3 tools/fake-freshrss.py
python3 -m app --config config.demo.json # in another shell
# then browse http://127.0.0.1:8080/opds as demo / demo
Or as the other half of the demo compose stack, where it binds the whole
container interface and writes a configuration addressing itself by service name
(see ``docker-compose.demo.yaml``).
Nothing here is a real credential. With the default loopback bind it is
reachable only from this machine; the demo compose stack puts both containers on
an internal network with no published ports other than the catalogue.
"""
from __future__ import annotations
import argparse
import json
import sys
import time
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT))
from app.config import password_digest # noqa: E402
from tests.stub_freshrss import build_demo_catalog # noqa: E402
DEMO_USER = "demo"
DEMO_PASSWORD = "demo"
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"--write-config",
metavar="PATH",
default="config.demo.json",
help="where to write a configuration that points at this fake server",
)
app = parser.add_argument_group("the server being configured")
app.add_argument("--app-port", type=int, default=8080, help="port for the server")
app.add_argument(
"--app-host",
default="127.0.0.1",
help="address the server should bind (use 0.0.0.0 inside a container)",
)
api = parser.add_argument_group("this fake FreshRSS")
api.add_argument(
"--api-port",
type=int,
default=0,
help="port to listen on; 0 asks the operating system for a free one",
)
api.add_argument(
"--api-bind",
default="127.0.0.1",
help="address to listen on (use 0.0.0.0 so another container can reach it)",
)
api.add_argument(
"--api-public-host",
default=None,
help="host name to write into the configuration (a compose service name, "
"for instance); defaults to the bind address",
)
return parser
def demo_configuration(stub, arguments: argparse.Namespace) -> dict:
"""The configuration that points a server at this fake account."""
return {
"_comment": "Written by tools/fake-freshrss.py; not a real configuration.",
"server": {
"host": arguments.app_host,
"port": arguments.app_port,
"log_level": "info",
},
"freshrss": {
"url": stub.api_url,
"username": stub.username,
"api_password": stub.password,
"items_per_page": 5,
# A short window so the demo reflects a download immediately rather
# than showing a stale unread count for minutes.
"retries": 1,
},
"catalog": {"title": "Demo Catalog", "author": "Demo"},
"epub": {"include_images": False},
"auth": {
"users": [{"username": DEMO_USER, "password_sha256": password_digest(DEMO_PASSWORD)}]
},
"cache": {"subscriptions_ttl_seconds": 30, "stream_ttl_seconds": 10},
}
def main(argv: list[str] | None = None) -> int:
arguments = build_parser().parse_args(argv)
stub = build_demo_catalog(
bind_host=arguments.api_bind,
bind_port=arguments.api_port,
public_host=arguments.api_public_host,
).start()
path = Path(arguments.write_config)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(
json.dumps(demo_configuration(stub, arguments), indent=2) + "\n",
encoding="utf-8",
)
browse_host = "127.0.0.1" if arguments.app_host == "0.0.0.0" else arguments.app_host
print(f"fake FreshRSS listening on {stub.api_url}", flush=True)
print(f" api user: {stub.username}", flush=True)
print(f" api password: {stub.password}", flush=True)
print(f"wrote {path}", flush=True)
print(f"catalogue will be at http://{browse_host}:{arguments.app_port}/opds", flush=True)
print(f" sign in as {DEMO_USER} / {DEMO_PASSWORD}", flush=True)
if arguments.app_host == "127.0.0.1":
print(f"\nnow run, in another shell:\n python3 -m app --config {path}", flush=True)
print("\npress ctrl-c to stop", flush=True)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nstopping", flush=True)
finally:
stub.stop()
return 0
if __name__ == "__main__":
raise SystemExit(main())