[csw-devel] SF.net SVN: opencsw:[578] twitter/sweets

skayser at users.sourceforge.net skayser at users.sourceforge.net
Thu Oct 27 19:31:52 CEST 2011


Revision: 578
          http://opencsw.svn.sourceforge.net/opencsw/?rev=578&view=rev
Author:   skayser
Date:     2011-10-27 17:31:52 +0000 (Thu, 27 Oct 2011)
Log Message:
-----------
twitter/sweets: add ATOM generator and template

Added Paths:
-----------
    twitter/sweets/sweets2feed.py
    twitter/sweets/template.feed

Added: twitter/sweets/sweets2feed.py
===================================================================
--- twitter/sweets/sweets2feed.py	                        (rev 0)
+++ twitter/sweets/sweets2feed.py	2011-10-27 17:31:52 UTC (rev 578)
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+#
+# sweets2feed.py:
+#   Reads a tweets file generated by sweets.py and generates an
+#   ATOM feed containing the tweets as items.
+#
+# TODO:
+# * Set output file via -o (to be able to redirect stdout for debugging)
+# * Add logging for operational purposes
+
+import sweets
+import os
+import simplejson
+import sys
+from dateutil.parser import parse
+from datetime import *
+from mako.template import Template
+
+def tweet_to_feeditem(tweet):
+    """Convert a tweet into a feed item."""
+    title = "%s: %s" % (tweet['from_user'], tweet['text'])
+    url = "http://twitter.com/#!/%s/status/%s" % (
+            tweet['from_user'],
+            tweet['id_str'])
+    timestamp = parse(tweet['created_at']).isoformat()
+    return { 'title': title, 'url': url, 'updated': timestamp }
+
+usage = "%s <json-file>" % os.path.basename(sys.argv[0])
+if not len(sys.argv) > 1:
+    print usage
+    sys.exit(1)
+
+feedtemplate = Template(filename="template.feed")
+r = sweets.TSResponse(file=sys.argv[1])
+
+print feedtemplate.render(
+        items=map(tweet_to_feeditem, r.get_tweets()),
+        query=r.data['query'],
+        updated=datetime.now().isoformat())


Property changes on: twitter/sweets/sweets2feed.py
___________________________________________________________________
Added: svn:executable
   + *

Added: twitter/sweets/template.feed
===================================================================
--- twitter/sweets/template.feed	                        (rev 0)
+++ twitter/sweets/template.feed	2011-10-27 17:31:52 UTC (rev 578)
@@ -0,0 +1,28 @@
+<%!
+    import getpass
+    import platform
+    myuser = getpass.getuser()
+    mynode = platform.node()
+%>
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+
+    <title>Tweets matching "${query}"</title>
+    <id>tag:opencsw.org,2011-10-27:sweets:${query}</id>
+    <author>${myuser}@${mynode}</author>
+    <updated>${updated}</updated>
+    <generator uri="http://opencsw.svn.sf.net/svnroot/opencsw/twitter/sweets/">
+        sweets2feed.py
+    </generator>
+
+%for item in items:
+    <entry>
+        <title>${item['title']}</title>
+        <link rel="alternate" type="text/html">${item['url']}</link>
+        <id>${item['url']}</id>
+        <updated>${item['updated']}</updated>
+        <content></content>
+    <entry>
+%endfor
+
+</feed>

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the devel mailing list