From d68a0907c657a8b400401859a586b080931befb4 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Wed, 12 May 2010 16:02:29 +0100 Subject: [PATCH] Further work on parsing Reddit comments --- reddit/api.py | 29 +++++++++++++++++++++++++++++ reddit/models.py | 7 ++++++- templates/hr/applications/view.html | 8 ++++---- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/reddit/api.py b/reddit/api.py index 5d59b07..ef08b77 100644 --- a/reddit/api.py +++ b/reddit/api.py @@ -6,6 +6,35 @@ from datetime import datetime class NotLoggedIn(Exception): pass +class Comment: + """ Abstraction for comment data provided by JSON + Comments can be identifed by Kind = 1 """ + + kind = 1 + + def __init__(self, data): + self.id = data['id'] + self.post = data['link_id'][3:] + self.body = data['body'] + self.ups = data['likes'] + self.downs = data['downs'] + self.subreddit_id = data['subreddit_id'] + self.subreddit = data['subreddit'] + self.author = data['author'] + + self._rawdata = data + + @property + def permalink(self): + return u'http://reddit.com/comments/%s/c/%s' % (self.post, self.id) + + def __unicode__(self): + return u'/r/%s - %s' % (self.subreddit, self.author) + + def __str__(self): + return self.__unicode__() + + class Message(dict): """ Abstract for a Reddit Message """ diff --git a/reddit/models.py b/reddit/models.py index c28a243..26af106 100644 --- a/reddit/models.py +++ b/reddit/models.py @@ -3,6 +3,8 @@ from django.contrib.auth.models import User import simplejson as json import urllib from datetime import datetime +from reddit.api import Comment + class RedditAccount(models.Model): """ @@ -53,7 +55,10 @@ class RedditAccount(models.Model): posts = [] for item in jsondoc['data']['children']: - posts.append(item['data']) + if item['kind'] == 't1': + posts.append(Comment(item['data'])) + elif item['kind'] == 't3': + posts.append(item['data']) return posts diff --git a/templates/hr/applications/view.html b/templates/hr/applications/view.html index 23c2419..4e175de 100644 --- a/templates/hr/applications/view.html +++ b/templates/hr/applications/view.html @@ -75,11 +75,11 @@

Reddit Posts

{% endif %}