-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path100-count.py
More file actions
executable file
·54 lines (50 loc) · 1.89 KB
/
100-count.py
File metadata and controls
executable file
·54 lines (50 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python3
"""Function to count words in all hot posts of a given Reddit subreddit."""
import requests
def count_words(subreddit, word_list, instances={}, after="", count=0):
"""Prints counts of given words found in hot posts of a given subreddit.
Args:
subreddit (str): The subreddit to search.
word_list (list): The list of words to search for in post titles.
instances (obj): Key/value pairs of words/counts.
after (str): The parameter for the next page of the API results.
count (int): The parameter of results matched thus far.
"""
url = "https://www.reddit.com/r/{}/hot/.json".format(subreddit)
headers = {
"User-Agent": "linux:0x16.api.advanced:v1.0.0 (by /u/bdov_)"
}
params = {
"after": after,
"count": count,
"limit": 100
}
response = requests.get(url, headers=headers, params=params,
allow_redirects=False)
try:
results = response.json()
if response.status_code == 404:
raise Exception
except Exception:
print("")
return
results = results.get("data")
after = results.get("after")
count += results.get("dist")
for c in results.get("children"):
title = c.get("data").get("title").lower().split()
for word in word_list:
if word.lower() in title:
times = len([t for t in title if t == word.lower()])
if instances.get(word) is None:
instances[word] = times
else:
instances[word] += times
if after is None:
if len(instances) == 0:
print("")
return
instances = sorted(instances.items(), key=lambda kv: (-kv[1], kv[0]))
[print("{}: {}".format(k, v)) for k, v in instances]
else:
count_words(subreddit, word_list, instances, after, count)