-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path0-subs.py
More file actions
executable file
·36 lines (28 loc) · 825 Bytes
/
0-subs.py
File metadata and controls
executable file
·36 lines (28 loc) · 825 Bytes
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
#!/usr/bin/python3
"""
Query a subreddit and return the number of
total subscribers in that subredit
"""
from requests import get
from sys import argv
headers = {
"User-Agent": "Of course I had to use a custom User-Agent",
"X-Forwared-For": "iamthecavalry"
}
def number_of_subscribers(subreddit: str) -> int:
"""
Query the subreddit and return the number of
Active subs. If its an invalid subredit, return 0
"""
response = get("https://www.reddit.com/r/{}/about.json".format(subreddit),
headers=headers)
data = response.json()
try:
if 'error' in data.keys():
return 0
else:
return data['data']['subscribers']
except Exception as e:
return 0
if __name__ == "__main__":
print(number_of_subscribers(argv[1]))