-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit.py
36 lines (25 loc) · 1.05 KB
/
reddit.py
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
import requests
class RedditException(Exception):
pass
class Reddit:
TimePeriods = set(['day', 'week', 'month', 'year', 'all'])
BaseUrl = 'https://www.reddit.com'
ThreadType = "t3"
def __init__(self, subreddit):
self.subreddit = subreddit
def top(self, period, limit):
if period not in Reddit.TimePeriods:
raise RedditException('Invalid time period string: {}. Expected one of {}'.format(period, ', '.join(Reddit.TimePeriods)))
params = {'t': period, 'limit': limit}
listing = self.__get('top', params)
titles = [l['data']['title'] for l in listing['data']['children'] if l['kind'] == Reddit.ThreadType]
return titles
def __get(self, dest, params):
url = '{}/r/{}/{}.json'.format(Reddit.BaseUrl, self.subreddit, dest)
headers = {
'User-Agent': 'listentothis-to-spotify',
'Accept': 'application/json'
}
r = requests.get(url, params=params, headers=headers)
if not r.raise_for_status():
return r.json()