|
#!/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin/python |
|
|
|
import urllib,json |
|
import argparse |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument( 'subreddit', nargs=1,help="Subreddit to browse, f.e. programming") |
|
args= parser.parse_args() |
|
subreddit = args.subreddit[0] |
|
a = urllib.urlopen('http://www.reddit.com/r/'+str(subreddit)+'/.json') |
|
try: |
|
k = json.load(a) |
|
j=k['data'] |
|
except KeyError: |
|
print "Problem reading reddit data. Please, try again in a few seconds" |
|
exit() |
|
storieslist = j['children'] |
|
counter=0 |
|
for x in storieslist[0:9]: |
|
counter=counter+1 |
|
title = x['data']['title'] |
|
URL = x['data']['url'] |
|
shortened = urllib.urlopen("http://is.gd/api.php?longurl="+URL).read() |
|
votes = x['data']['score'] |
|
longer="..." if len(URL)>35 else "\t" |
|
print str(counter)+"\t"+str(votes)+"\t"+title.encode("utf-8", "ignore")[0:35]+"..." |
|
print "\t\t"+URL.encode("utf-8", "ignore")[0:35]+longer+ "\t" + shortened |