From cbeadbd3020816a3f510a22981335843be0f29cf Mon Sep 17 00:00:00 2001 From: John Doty Date: Wed, 10 Jul 2024 08:21:40 +0900 Subject: [PATCH] Import OPML --- cry/cli.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/cry/cli.py b/cry/cli.py index ac39f26..bc78c03 100644 --- a/cry/cli.py +++ b/cry/cli.py @@ -6,6 +6,9 @@ import click from . import feed from . import database +from . import opml + +LOG = logging.getLogger(__name__) @click.group() @@ -49,6 +52,38 @@ def subscribe(url): click.echo(f"Subscribed to {meta.url}") +@cli.command(name="import") +@click.argument("opml_file", type=click.File("r", encoding="utf-8")) +def import_opml(opml_file): + "Import the specified OPML file." + + db = database.Database.local() + urls = opml.parse_opml(opml_file.read()) + metas = [feed.FeedMeta.from_url(url, db.origin) for url in urls] + + click.echo(f"Fetching {len(urls)} feeds ...") + results = asyncio.run(feed.fetch_many(metas)) + + subscribed = 0 + for index, result in enumerate(results): + d, meta = result + url = urls[index] + if d is None: + LOG.warn(f"Unable to fetch {url}, skipping...") + continue + + existing = db.load_feed(meta.url) + if existing is not None: + LOG.info(f"{url} already exists (as {meta.url})") + continue + + f = feed.Feed.from_parsed(d, meta) + db.store_feed(f) + subscribed = subscribed + 1 + + click.echo(f"Subscribed to {subscribed} new feeds") + + @cli.command(name="refresh") @click.argument("url", required=False, default=None) def refresh(url):