File tree Expand file tree Collapse file tree 2 files changed +27
-6
lines changed Expand file tree Collapse file tree 2 files changed +27
-6
lines changed Original file line number Diff line number Diff line change 3
3
Usage:
4
4
------
5
5
6
+ $ realpython [options] [<id>]
7
+
6
8
List the latest tutorials:
7
9
8
10
$ realpython
18
20
$ realpython 0
19
21
20
22
23
+ Available options are:
24
+
25
+ -h, --help Show this help
26
+ -l, --show-links Show links in text
27
+
28
+
21
29
Contact:
22
30
--------
23
31
38
46
39
47
def main () -> None :
40
48
"""Read the Real Python article feed"""
49
+ args = [a for a in sys .argv [1 :] if not a .startswith ("-" )]
50
+ opts = [o for o in sys .argv [1 :] if o .startswith ("-" )]
51
+
41
52
# Show help message
42
- if "-h" in sys . argv or "--help" in sys . argv :
53
+ if "-h" in opts or "--help" in opts :
43
54
viewer .show (__doc__ )
44
55
return
45
56
57
+ # Should links be shown in the text
58
+ show_links = ("-l" in opts or "--show-links" in opts )
59
+
46
60
# An article ID is given, show article
47
- if len (sys .argv ) > 1 :
48
- article = feed .get_article (sys .argv [1 ])
49
- viewer .show (article )
61
+ if args :
62
+ for article_id in args :
63
+ article = feed .get_article (article_id , show_links )
64
+ viewer .show (article )
50
65
51
66
# No ID is given, show list of articles
52
67
else :
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ def get_site() -> str:
24
24
return f"{ info .title } ({ info .link } )"
25
25
26
26
27
- def get_article (article_id : str ) -> str :
27
+ def get_article (article_id : str , links : bool = False ) -> str :
28
28
"""Get article from feed with the given ID"""
29
29
articles = _feed ().entries
30
30
try :
@@ -34,11 +34,17 @@ def get_article(article_id: str) -> str:
34
34
msg = f"Unknown article ID, use ID from 0 to { max_id } "
35
35
raise SystemExit (f"Error: { msg } " )
36
36
37
+ # Get article as HTML
37
38
try :
38
39
html = article .content [0 ].value
39
40
except AttributeError :
40
41
html = article .summary
41
- text = html2text .html2text (html )
42
+
43
+ # Convert HTML to plain text
44
+ to_text = html2text .HTML2Text ()
45
+ to_text .ignore_links = not links
46
+ text = to_text .handle (html )
47
+
42
48
return f"# { article .title } \n \n { text } "
43
49
44
50
You can’t perform that action at this time.
0 commit comments