File tree Expand file tree Collapse file tree 4 files changed +67
-11
lines changed Expand file tree Collapse file tree 4 files changed +67
-11
lines changed Original file line number Diff line number Diff line change
1
+ """Read the latest Real Python tutorials
2
+
3
+ Usage:
4
+ ------
5
+
6
+ List the latest tutorials:
7
+
8
+ $ realpython
9
+
10
+ Read one tutorial:
11
+
12
+ $ realpython <id>
13
+
14
+ where <id> is the number shown when listing tutorials.
15
+
16
+ Read the latest tutorial:
17
+
18
+ $ realpython 0
19
+
20
+
21
+ Contact:
22
+ --------
23
+
24
+ - https://realpython.com/contact/
25
+
26
+ More information is available at:
27
+
28
+ - https://pypi.org/project/realpython-reader/
29
+ - https://github.com/realpython/reader
30
+ """
31
+ # Standard library imports
1
32
import sys
2
33
34
+ # Reader imports
3
35
from reader import feed
4
36
from reader import viewer
5
37
6
38
7
39
def main () -> None :
8
40
"""Read the Real Python article feed"""
41
+ # Show help message
42
+ if "-h" in sys .argv or "--help" in sys .argv :
43
+ viewer .show (__doc__ )
44
+ return
45
+
9
46
# An article ID is given, show article
10
47
if len (sys .argv ) > 1 :
11
48
article = feed .get_article (sys .argv [1 ])
Original file line number Diff line number Diff line change 1
- from typing import List
1
+ """Interact with the Real Python feed"""
2
+ # Standard library imports
3
+ from typing import Dict , List
4
+
5
+ # Third party imports
2
6
import feedparser
3
7
import html2text
4
- import reader
5
8
6
- _CACHED_FEED = feedparser .FeedParserDict ()
9
+ # Reader imports
10
+ from reader import URL
11
+ _CACHED_FEEDS : Dict [str , feedparser .FeedParserDict ] = dict ()
7
12
8
13
9
14
def _feed () -> feedparser .FeedParserDict :
10
15
"""Cache contents of the feed, so it's only read once"""
11
- if not _CACHED_FEED :
12
- _CACHED_FEED . update ( feedparser .parse (reader . URL ) )
13
- return _CACHED_FEED
16
+ if URL not in _CACHED_FEEDS :
17
+ _CACHED_FEEDS [ URL ] = feedparser .parse (URL )
18
+ return _CACHED_FEEDS [ URL ]
14
19
15
20
16
21
def get_site () -> str :
@@ -25,9 +30,14 @@ def get_article(article_id: str) -> str:
25
30
try :
26
31
article = articles [int (article_id )]
27
32
except (IndexError , ValueError ):
28
- raise SystemExit ("Error: Unknown article ID" )
33
+ max_id = len (articles ) - 1
34
+ msg = f"Unknown article ID, use ID from 0 to { max_id } "
35
+ raise SystemExit (f"Error: { msg } " )
29
36
30
- html = article .content [0 ].value
37
+ try :
38
+ html = article .content [0 ].value
39
+ except AttributeError :
40
+ html = article .summary
31
41
text = html2text .html2text (html )
32
42
return f"# { article .title } \n \n { text } "
33
43
Original file line number Diff line number Diff line change
1
+ """Functions for displaying the Real Python feed"""
2
+
3
+ # Standard library imports
1
4
from typing import List
2
5
3
6
Original file line number Diff line number Diff line change
1
+ """Setup script for realpython-reader"""
2
+
1
3
import pathlib
2
- from setuptools import find_packages , setup
4
+ from setuptools import setup
3
5
4
6
# The directory containing this file
5
7
HERE = pathlib .Path (__file__ ).parent
6
8
9
+ # The text of the README file
10
+ README = (HERE / "README.md" ).read_text ()
11
+
7
12
13
+ # This call to setup() does all the work
8
14
setup (
9
15
name = "realpython-reader" ,
10
16
version = "0.0.1" ,
11
17
description = "Read Real Python Tutorials" ,
12
- long_description = ( HERE / " README.md" ). read_text () ,
18
+ long_description = README ,
13
19
long_description_content_type = "text/markdown" ,
14
20
url = "https://github.com/realpython/reader" ,
15
21
author = "Real Python" ,
20
26
"Programming Language :: Python" ,
21
27
"Programming Language :: Python :: 3" ,
22
28
],
23
- packages = find_packages ( exclude = ( "tests" ,)) ,
29
+ packages = [ "reader" ] ,
24
30
install_requires = ["feedparser" , "html2text" ],
25
31
entry_points = {
26
32
"console_scripts" : [
You can’t perform that action at this time.
0 commit comments