Import OPML

This commit is contained in:
John Doty 2024-07-10 08:21:40 +09:00
parent 491df5f942
commit cbeadbd302

View file

@ -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):