Created
June 18, 2012 21:57
-
-
Save tkh44/2950970 to your computer and use it in GitHub Desktop.
Get latest comics from reddit.com/r/fffffffuuuuuuuuuuu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'kye.hohenberger' | |
from urllib2 import urlopen | |
from django.utils import simplejson as json | |
import datetime | |
from ragedigest.models import Comic, Queue | |
import reddit | |
import sys | |
def update_reddit(subreddit='fffffffuuuuuuuuuuuu'): | |
rank = 1 | |
created = True | |
#Create and save the queue so we can save to a valid object | |
q = Queue() | |
q.save() | |
#Get the raw json from the reddit server | |
r = reddit.Reddit(user_agent="Rage Browser") | |
posts = r.get_subreddit(subreddit).get_hot(limit=100) | |
for post in posts: | |
try: | |
c = Comic.objects.get( | |
reddit_id = _36_decode(post.id), | |
) | |
created = False | |
except Comic.DoesNotExist: | |
c = Comic() | |
#We base our rank off the reddit rank, for usablility | |
c.rank = rank | |
rank += 1 | |
#Assign appropriate values | |
c.domain = post.domain | |
c.author = post.author | |
c.reddit_id = _36_decode(post.id) | |
c.score = post.score | |
c.thumbnail = _fix_thumbnail(post.thumbnail) | |
c.subbreddit_id = post.subreddit_id | |
print post.permalink | |
c.permalink = post.permalink | |
c.name = post.name | |
c.created = _utc_local(post.created) | |
c.url = post.url | |
c.title = post.title | |
c.created_utc = _utc_local(post.created_utc) | |
c.num_comments = post.num_comments | |
c.ups = post.ups | |
c.downs = post.downs | |
#Save the comic | |
c.save() | |
#Add to current queue | |
q.comics.add(c) | |
if created: | |
print 'New Comic!' | |
#print '[Rank: %s][Score: %s] %s' % (c.rank, c.score, c.title) | |
#Save the finalized queue | |
q.save() | |
#Converts reddit times to CST | |
def _utc_local(time): | |
#print time | |
converted_time = datetime.datetime.utcfromtimestamp(time) | |
#print converted_time | |
return converted_time | |
#Decode Base36 to base10 | |
def _36_decode(number): | |
return int(number, 36) | |
def _fix_thumbnail(thumbnail): | |
#print 'Thumbnail: %s' % thumbnail | |
if thumbnail == '/media/images/static/nsfw2.png': | |
print 'Changed thumbnail from "%s" to "/media/images/static/nsfw2.png"' % thumbnail | |
return 'http://www.reddit.com/static/nsfw2.png' | |
elif thumbnail == '/static/nsfw2.png': | |
print 'Changed thumbnail from "%s" to "/media/images/static/nsfw2.png"' % thumbnail | |
return 'http://www.reddit.com/static/nsfw2.png' | |
elif thumbnail == '/media/images/static/noimage.png': | |
print 'Changed thumbnail from "%s" to "/media/images/static/noimage.png"' % thumbnail | |
return 'http://www.reddit.com/static/noimage.png' | |
elif thumbnail == '/static/noimage.png': | |
print 'Changed thumbnail from "%s" to "/media/images/static/noimage.png"' % thumbnail | |
return 'http://www.reddit.com/static/noimage.png' | |
else: | |
return thumbnail |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment