A small script that exports your Earth Reader subscrition list into an HTML document.
$ pip install -r requirements.txt
$ ./export.py --help
.*.swp | |
*.html | |
*.pyc |
A small script that exports your Earth Reader subscrition list into an HTML document.
$ pip install -r requirements.txt
$ ./export.py --help
#!/usr/bin/env python3 | |
"""A small script that exports your Earth Reader subscrition list into | |
an HTML document. See also README.rst. | |
""" | |
import logging | |
import re | |
from click import argument, command, echo, option | |
from jinja2.environment import Environment | |
from libearth.repository import from_url | |
from libearth.session import Session | |
from libearth.stage import Stage | |
from libearth.subscribe import SubscriptionSet | |
__version__ = '1.0.0' | |
TEMPLATE_STRING = ''' | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="generator" content="earthreader-export.py/{{ version }}"> | |
<meta name="author" content="{{ subscriptions.owner|string }}"> | |
<title>{{ subscriptions.title }}</title> | |
</head> | |
<body> | |
<h1>{{ subscriptions.title }}</h1> | |
<ul> | |
{% for outline in subscriptions recursive %} | |
{% if outline is set %} | |
<li class="set"> | |
{{ outline.label }} | |
<ul>{{ loop(outline) }}</ul> | |
</li> | |
{% else %} | |
<li class="subscription"> | |
<a href="{{ outline.alternate_uri }}"> | |
{%- if outline.icon_uri -%} | |
<img src="{{ outline.icon_uri }}"> | |
{%- endif %} | |
{{ outline.label }} | |
</a> | |
</li> | |
{% endif %} | |
{% endfor %} | |
</ul> | |
</body> | |
</html> | |
''' | |
def from_url_or_path(url_or_path): | |
if re.match(r'^[A-Za-z0-9+-.]+://', url_or_path): | |
url = url_or_path | |
else: | |
url = 'file://' + url_or_path | |
return from_url(url) | |
def is_subscription_set(outline): | |
return isinstance(outline, SubscriptionSet) | |
@command() | |
@option('-i', '--session-id', | |
metavar='SESSION_ID', | |
default=Session().identifier, | |
show_default=True, | |
help='Session identifier.') | |
@option('-v', '--verbose', is_flag=True, help='Print debug logs as well.') | |
@argument('repository') | |
def main(session_id, verbose, repository): | |
"""A small script that exports your Earth Reader subscrition list into | |
an HTML document. | |
""" | |
if verbose: | |
logging.basicConfig(level=logging.DEBUG) | |
session = Session(session_id) | |
repository = from_url_or_path(repository) | |
stage = Stage(session, repository) | |
environment = Environment(autoescape=True) | |
environment.tests['set'] = is_subscription_set | |
template = environment.from_string(TEMPLATE_STRING) | |
with stage: | |
subs = stage.subscriptions | |
stream = template.stream(subscriptions=subs, version=__version__) | |
for chunk in stream: | |
echo(chunk.encode('utf-8'), nl=False) | |
if __name__ == '__main__': | |
main() |
libearth >= 0.3.1 | |
Click >= 3.3 | |
Jinja2 >= 2.7.3 |