1
1
#!/usr/bin/python3
2
- """parses the title of all hot articles, and prints a sorted count of given keywords"""
2
+ """
3
+ parses the title of all hot articles,
4
+ and prints a sorted count of given keywords
5
+ """
3
6
4
7
import requests
5
8
import re
6
9
from collections import defaultdict
7
10
11
+
8
12
def count_words (subreddit , word_list , after = None , word_counts = None ):
9
13
"""
10
- Recursively count the number of occurrences of each word from word_list in the hot articles of the given subreddit.
14
+ Recursively count the number of occurrences of each word
15
+ from word_list in the hot articles of the given subreddit.
11
16
12
17
Args:
13
18
subreddit (str): The subreddit to search.
14
19
word_list (list): A list of case-insensitive words to search for.
15
- after (str, optional): The fullname of a reddit thing. Used for pagination.
16
- word_counts (defaultdict, optional): A defaultdict to store the word counts.
20
+ after (str, optional): The fullname of a reddit thing.
21
+ Used for pagination.
22
+ word_counts (defaultdict, optional):
23
+ A defaultdict to store the word counts.
17
24
18
25
Returns:
19
26
defaultdict: A defaultdict containing the word counts.
@@ -29,7 +36,8 @@ def count_words(subreddit, word_list, after=None, word_counts=None):
29
36
response .raise_for_status ()
30
37
data = response .json ()
31
38
except (requests .exceptions .RequestException , ValueError , KeyError ):
32
- return defaultdict (int ) # Return an empty defaultdict if an error occurs
39
+ return defaultdict (int )
40
+ # Return an empty defaultdict if an error occurs
33
41
34
42
if word_counts is None :
35
43
word_counts = defaultdict (int )
@@ -46,6 +54,7 @@ def count_words(subreddit, word_list, after=None, word_counts=None):
46
54
47
55
return word_counts
48
56
57
+
49
58
if __name__ == "__main__" :
50
59
import sys
51
60
0 commit comments