Permalink
Browse files

More revisions--added reddit comments and flickr photos.

  • Loading branch information...
1 parent 06ac3cc commit f4638fe7dee8fe7ee38dbf87677b17e9dbdec151 @ericflo committed Nov 11, 2008
@@ -1,4 +1,5 @@
from django.db.models import signals
+from django.contrib.contenttypes.models import ContentType
from couch_lifestream import models, db, COUCHDB_DESIGN_DOCNAME
from couchdb.design import ViewDefinition
from textwrap import dedent
@@ -20,4 +21,9 @@
def create_couchdb_views(app, created_models, verbosity, **kwargs):
ViewDefinition.sync_many(db, [item_type_date, by_date])
+ ContentType.objects.get_or_create(
+ name='CouchDB Item',
+ app_label='couch_lifestream',
+ model='couchdbitem'
+ )
signals.post_syncdb.connect(create_couchdb_views, sender=models)
@@ -43,6 +43,51 @@ def fetch_pownce_items():
db.create(item)
print "Pownce items fetched"
+REDDIT_USERNAME = getattr(settings, 'REDDIT_USERNAME', None)
+
+def fetch_reddit_items():
+ import feedparser
+ print "Fetching Reddit items"
+ d = feedparser.parse('http://www.reddit.com/user/%s/comments/.rss' % (
+ REDDIT_USERNAME,))
+ map_fun = 'function(doc) { emit(doc.link, null); }'
+ for item in map(dict, d['entries']):
+ item['item_type'] = 'reddit-comment'
+ item['couch_lifestream_date'] = datetime.datetime.fromtimestamp(
+ time.mktime(item['updated_parsed']))
+ if len(db.query(map_fun, key=item['link'])) == 0:
+ for (key, val) in item.items():
+ if 'parsed' in key:
+ del item[key]
+ elif isinstance(val, datetime.datetime):
+ item[key] = val.isoformat()
+ elif isinstance(val, datetime.date):
+ item[key] = val.isoformat()
+ db.create(item)
+ print "Reddit items fetched"
+
+FLICKR_USER_ID = getattr(settings, 'FLICKR_USER_ID', None)
+
+def fetch_flickr_items():
+ import feedparser
+ print "Fetching Flickr items"
+ d = feedparser.parse('http://api.flickr.com/services/feeds/photos_public.gne?format=atom&id=%s' % (FLICKR_USER_ID,))
+ map_fun = 'function(doc) { emit(doc.id, null); }'
+ for item in map(dict, d['entries']):
+ item['item_type'] = 'flickr'
+ item['couch_lifestream_date'] = datetime.datetime.fromtimestamp(
+ time.mktime(item['updated_parsed']))
+ if len(db.query(map_fun, key=item['id'])) == 0:
+ for (key, val) in item.items():
+ if 'parsed' in key:
+ del item[key]
+ elif isinstance(val, datetime.datetime):
+ item[key] = val.isoformat()
+ elif isinstance(val, datetime.date):
+ item[key] = val.isoformat()
+ db.create(item)
+ print "Flickr items fetched"
+
class Command(NoArgsCommand):
help = 'Fetch the latest lifestream items and insert them into CouchDB.'
@@ -51,4 +96,8 @@ def handle_noargs(self, **options):
fetch_twitter_items()
if POWNCE_USERNAME is not None:
fetch_pownce_items()
+ if REDDIT_USERNAME is not None:
+ fetch_reddit_items()
+ if FLICKR_USER_ID is not None:
+ fetch_flickr_items()
print "Finished loading lifestream items."
@@ -1 +1 @@
-# Nothing to see here. We're using CouchDB :)
+# Not much to see here. We're using CouchDB :)
@@ -0,0 +1 @@
+{% block content %}{% endblock %}
@@ -0,0 +1 @@
+{% extends "couch_lifestream/base_item.html" %}{% load couch_lifestream_tags %}{% block content %}<a href="http://www.flickr.com/photos/ericflo/" rel="me">Flick&rsquo;d</a>: {{ item.content.0.value|extract_tags:"img"|safe }}{% endblock %}
@@ -1 +1 @@
-<a href="http://pownce.com/ericflo" rel="me">Pownced</a>: {{ item.body }}
+{% extends "couch_lifestream/base_item.html" %}{% block content %}<a href="http://pownce.com/ericflo" rel="me">Pownced</a>: {{ item.body }}{% endblock %}
@@ -0,0 +1 @@
+{% extends "couch_lifestream/base_item.html" %}{% block content %}<a href="http://www.reddit.com/user/ericflo/comments/" rel="me">Said</a>: {{ item.summary }}{% endblock %}
@@ -1 +1 @@
-{% load couch_lifestream_tags %}<a href="http://twitter.com/ericflo" rel="me">Tweeted</a>: {{ item.text|twitterfy }}
+{% extends "couch_lifestream/base_item.html" %}{% block content %}{% load couch_lifestream_tags %}<a href="http://twitter.com/ericflo" rel="me">Tweeted</a>: {{ item.text|urlize|twitterfy }}{% endblock %}
@@ -1,5 +1,6 @@
import re
from django import template
+from django import forms
from django.template.loader import render_to_string
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
@@ -50,6 +51,12 @@ def render(self, context):
context[self.context_var] = doc['_id']
return ''
+def extract_tags(value, arg):
+ from BeautifulSoup import BeautifulSoup
+ soup = BeautifulSoup(value)
+ safe_tags = [s.strip() for s in arg.split()]
+ return u''.join(map(unicode, soup.findAll(safe_tags)))
+
TWITTER_RE = re.compile('@(\S+)')
def twitterfy(value):
@@ -61,4 +68,5 @@ def twitterfy(value):
register.tag('display_lifestream_item', do_display_lifestream_item)
register.tag('get_id_for_doc', do_get_id_for_doc)
-register.filter('twitterfy', twitterfy)
+register.filter('twitterfy', twitterfy)
+register.filter('extract_tags', extract_tags)
View
@@ -1,6 +1,7 @@
from django.conf.urls.defaults import *
urlpatterns = patterns('couch_lifestream.views',
+ url(r'^$', 'items', name='clife_index'),
url(r'^all/$', 'items', name='clife_all'),
url(r'^service/(?P<service>\S+)/$', 'items', name='clife_service'),
url(r'^item/(?P<id>\S+)/$', 'item', name='clife_item'),
@@ -1,4 +1,3 @@
-import datetime
from couch_lifestream import db, COUCHDB_DESIGN_DOCNAME
from django.shortcuts import render_to_response
from django.template import RequestContext
@@ -29,7 +28,7 @@ def item(request, id):
except client.ResourceNotFound:
raise Http404
context = {
- 'item': db[id],
+ 'item': obj,
}
return render_to_response(
'couch_lifestream/item.html',

0 comments on commit f4638fe

Please sign in to comment.