Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions reader/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Usage:
------

$ realpython [options] [<id>]

List the latest tutorials:

$ realpython
Expand All @@ -18,6 +20,12 @@
$ realpython 0


Available options are:

-h, --help Show this help
-l, --show-links Show links in text


Contact:
--------

Expand All @@ -38,15 +46,22 @@

def main() -> None:
"""Read the Real Python article feed"""
args = [a for a in sys.argv[1:] if not a.startswith("-")]
opts = [o for o in sys.argv[1:] if o.startswith("-")]

# Show help message
if "-h" in sys.argv or "--help" in sys.argv:
if "-h" in opts or "--help" in opts:
viewer.show(__doc__)
return

# Should links be shown in the text
show_links = ("-l" in opts or "--show-links" in opts)

# An article ID is given, show article
if len(sys.argv) > 1:
article = feed.get_article(sys.argv[1])
viewer.show(article)
if args:
for article_id in args:
article = feed.get_article(article_id, show_links)
viewer.show(article)

# No ID is given, show list of articles
else:
Expand Down
10 changes: 8 additions & 2 deletions reader/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_site() -> str:
return f"{info.title} ({info.link})"


def get_article(article_id: str) -> str:
def get_article(article_id: str, links: bool = False) -> str:
"""Get article from feed with the given ID"""
articles = _feed().entries
try:
Expand All @@ -34,11 +34,17 @@ def get_article(article_id: str) -> str:
msg = f"Unknown article ID, use ID from 0 to {max_id}"
raise SystemExit(f"Error: {msg}")

# Get article as HTML
try:
html = article.content[0].value
except AttributeError:
html = article.summary
text = html2text.html2text(html)

# Convert HTML to plain text
to_text = html2text.HTML2Text()
to_text.ignore_links = not links
text = to_text.handle(html)

return f"# {article.title}\n\n{text}"


Expand Down