mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-13 22:32:15 +00:00
Further work on parsing Reddit comments
This commit is contained in:
@@ -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 """
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -75,11 +75,11 @@
|
||||
<h3>Reddit Posts</h3>
|
||||
<ul>
|
||||
{% for post in posts %}
|
||||
{% if post.permalink %}
|
||||
<p><b><a href="http://reddit.com{{ post.permalink }}">{{ post.title }}</a></b> - /r/{{ post.subreddit }}</p>
|
||||
{% ifequal post.kind 1 %}
|
||||
<p><b><a href="{{ post.permalink }}">{{ post.title }}</a></b> - /r/{{ post.subreddit }}</p>
|
||||
{% else %}
|
||||
<p>{{ post.body }} - <a href="http://reddit.com/comments/{{ post.link_id }}/">Permalink</a></p>
|
||||
{% endif %}
|
||||
<p>{{ post.body }} - (/r/{{post.subreddit}}) <a href="{{ post.permalink }}/">Permalink</a></p>
|
||||
{% endifequal %}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
Reference in New Issue
Block a user