From 6511e548bbbac7f05572393e272f95d47829d967 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Tue, 26 Apr 2011 02:25:24 -0400 Subject: reimpleted JSON serialization manually --- README | 5 +++++ piccast/feeds/models.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ piccast/feeds/views.py | 18 ++++++---------- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/README b/README index 527611e..0eb3f7f 100644 --- a/README +++ b/README @@ -34,3 +34,8 @@ JSON API /json/sets_by_feed// /json/pics_by_set// +Feed Scrape Command +------------------- +Run "./manage.py scrape_feeds acidcow" to fetch the RSS feed for acid cow and +scape all the (new) picture sets. + diff --git a/piccast/feeds/models.py b/piccast/feeds/models.py index 925bc87..ea7a34a 100644 --- a/piccast/feeds/models.py +++ b/piccast/feeds/models.py @@ -6,6 +6,10 @@ class Category(models.Model): def __unicode__(self): return self.name + + def serializable(self): + return {'id': self.id, + 'name': self.name } class PicFeed(models.Model): #id @@ -16,9 +20,25 @@ class PicFeed(models.Model): title = models.CharField(max_length=80, blank=True) description = models.TextField(blank=True) image = models.ForeignKey('feeds.Pic', blank=True, null=True) + category = models.ForeignKey('feeds.Category', null=True,blank=True) def __unicode__(self): return self.shortname + + def serializable(self): + return {'id': self.id, + 'created_unixtime': + (int(self.created.strftime("%s")) if self.created else None), + 'shortname': self.shortname, + 'source_url': self.source_url, + 'rssfeed_url': self.rssfeed_url, + 'title': self.title, + 'description': self.description, + 'image': + (self.image.serializable_short() if self.image else None), + 'category': + (self.category.name if self.category else None), + } class PicSet(models.Model): #id @@ -34,6 +54,20 @@ class PicSet(models.Model): def __unicode__(self): return self.title + + def serializable(self): + return {'id': self.id, + 'created_unixtime': + (int(self.created.strftime("%s")) if self.created else None), + 'source_url': self.source_url, + 'title': self.title, + 'description': self.description, + 'keywords': self.keywords, + 'image': + (self.image.serializable_short() if self.image else None), + 'category': + (self.category.name if self.category else None), + } class Pic(models.Model): #id @@ -51,4 +85,26 @@ class Pic(models.Model): def __unicode__(self): return self.title + + def serializable(self): + return {'id': self.id, + 'set': {'id': self.set.id, 'title': self.set.title }, + 'thumbnail_url': self.thumbnail_url, + 'thumbnail_height': self.thumbnail_height, + 'thumbnail_width': self.thumbnail_width, + 'original_url': self.original_url, + 'original_height': self.original_height, + 'original_width': self.original_width, + 'source_url': self.source_url, + 'caption': self.caption, + 'is_nsfw': self.is_nsfw, + } + + def serializable_short(self): + # TODO: more here? + return {'id': self.id, + 'original_url': self.original_url, + 'original_height': self.original_height, + 'original_width': self.original_width, + } diff --git a/piccast/feeds/views.py b/piccast/feeds/views.py index f126932..8ea4b98 100644 --- a/piccast/feeds/views.py +++ b/piccast/feeds/views.py @@ -1,6 +1,7 @@ from django.http import HttpResponse from django.core import serializers from models import * +import json JSONSerializer = serializers.get_serializer("json") basic_html = """ @@ -10,34 +11,27 @@ basic_html = """

PicCast

""" + def basic_frontpage(request): return HttpResponse(basic_html); def newest_feeds(request): feeds = PicFeed.objects.order_by('-created')[:20] - json_serializer = JSONSerializer() - json_serializer.serialize(feeds) - return HttpResponse(json_serializer.getvalue(), mimetype="application/json") + return HttpResponse(json.dumps({'list': [f.serializable() for f in feeds]}, indent=2), mimetype="application/json") def sets_by_feed(request, req_feed): # TODO: validate req_feed sets = PicSet.objects.filter(feed=req_feed) - json_serializer = JSONSerializer() - json_serializer.serialize(sets) - return HttpResponse(json_serializer.getvalue(), mimetype="application/json") + return HttpResponse(json.dumps({'list': [s.serializable() for s in sets]}, indent=2), mimetype="application/json") def sets_by_feedslug(request, req_feedslug): # TODO: validate req_feedslug feed = PicFeed.objects.filter(shortname=req_feedslug) sets = PicSet.objects.filter(feed=feed) - json_serializer = JSONSerializer() - json_serializer.serialize(sets) - return HttpResponse(json_serializer.getvalue(), mimetype="application/json") + return HttpResponse(json.dumps({'list': [s.serializable() for s in sets]}, indent=2), mimetype="application/json") def pics_by_set(request, req_picset): # TODO: validate req_picset pics = Pic.objects.filter(set=req_picset) - json_serializer = JSONSerializer() - json_serializer.serialize(pics) - return HttpResponse(json_serializer.getvalue(), mimetype="application/json") + return HttpResponse(json.dumps({'list':[p.serializable() for p in pics]}, indent=2), mimetype="application/json") -- cgit v1.2.3