-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreddit.py
More file actions
32 lines (25 loc) · 1012 Bytes
/
reddit.py
File metadata and controls
32 lines (25 loc) · 1012 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
import json
from urllib2 import urlopen, Request, HTTPError
def get_items(subreddit, sort='hot'):
""" Returns a list of items from a subreddit, optionally sorted by
hot, new, controversial or top. """
BASE_URL = 'http://www.reddit.com/r/%s/%s.json' % (subreddit, sort)
HEADER = { 'User-Agent' : 'RipReddit script' }
try:
request = Request(BASE_URL, headers=HEADER)
raw_json = urlopen(request).read()
json_data = json.JSONDecoder().decode(raw_json)
items = [x['data'] for x in json_data['data']['children']]
except HTTPError as ERROR:
print '\t%s HTTP Error received for %s' % (ERROR.code, BASE_URL)
items = []
return items
if __name__ == '__main__':
print "Recent items from the Wallpaper subreddit:"
items = get_items('wallpaper')
for item in items:
print '\t%s - %s' % (item['title'], item['url'])
print "\nRecent items from the Wallpaper subreddit, sorted by Top:"
items = get_items('wallpaper', 'top')
for item in items:
print '\t%s - %s' % (item['title'], item['url'])