Import OPML
This commit is contained in:
parent
491df5f942
commit
cbeadbd302
1 changed files with 35 additions and 0 deletions
35
cry/cli.py
35
cry/cli.py
|
|
@ -6,6 +6,9 @@ import click
|
||||||
|
|
||||||
from . import feed
|
from . import feed
|
||||||
from . import database
|
from . import database
|
||||||
|
from . import opml
|
||||||
|
|
||||||
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
|
|
@ -49,6 +52,38 @@ def subscribe(url):
|
||||||
click.echo(f"Subscribed to {meta.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")
|
@cli.command(name="refresh")
|
||||||
@click.argument("url", required=False, default=None)
|
@click.argument("url", required=False, default=None)
|
||||||
def refresh(url):
|
def refresh(url):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue