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

skayser at users.sourceforge.net skayser at users.sourceforge.net
Thu Nov 3 11:46:12 CET 2011


Revision: 585
          http://opencsw.svn.sourceforge.net/opencsw/?rev=585&view=rev
Author:   skayser
Date:     2011-11-03 10:46:11 +0000 (Thu, 03 Nov 2011)
Log Message:
-----------
twitter/sweets: enable reverse chronological ordering, fix syntax error

Modified Paths:
--------------
    twitter/sweets/sweets.py
    twitter/sweets/sweets2feed.py

Modified: twitter/sweets/sweets.py
===================================================================
--- twitter/sweets/sweets.py	2011-10-27 21:28:22 UTC (rev 584)
+++ twitter/sweets/sweets.py	2011-11-03 10:46:11 UTC (rev 585)
@@ -18,6 +18,7 @@
 import time
 import types
 from optparse import OptionParser,SUPPRESS_HELP
+from dateutil.parser import parse
 
 class JsonParseError(Exception):
     """Specific error for simplejson which only raises unspecific ValueError."""
@@ -57,9 +58,16 @@
     def get_query(self):
         return self.data.get('query')
 
-    def get_tweets(self):
+    def _get_tweet_epoch(self, tweet):
+        return time.mktime(parse(tweet['created_at']).timetuple())
+
+    def get_tweets(self, reverse=False):
         """Get the list of tweets in the response object."""
-        return self.data.get('results', [])
+        tweets = self.data.get('results', [])
+        if tweets and reverse:
+            return sorted(tweets, key=self._get_tweet_epoch, reverse=True)
+        else:
+            return sorted(tweets, key=self._get_tweet_epoch)
 
     def set_tweets(self, tweets):
         """Set the list of tweets in the response object."""

Modified: twitter/sweets/sweets2feed.py
===================================================================
--- twitter/sweets/sweets2feed.py	2011-10-27 21:28:22 UTC (rev 584)
+++ twitter/sweets/sweets2feed.py	2011-11-03 10:46:11 UTC (rev 585)
@@ -19,7 +19,7 @@
 
 def tweet_to_feeditem(tweet):
     """Convert a tweet into a feed item."""
-    title = tweet['text'])
+    title = tweet['text']
     url = "http://twitter.com/#!/%s/status/%s" % (
             tweet['from_user'],
             tweet['id_str'])
@@ -34,8 +34,13 @@
     os.rename(name, destfile)
 
 def write_feed(results, template, destfile):
+    """
+    Write tweets from response object to feed file based on template.
+    We order feed items reverse-chronological, de-facto standard for
+    RSS feeds and baked into many feed clients, even for ATOM feeds.
+    """
     feedtemplate = Template(filename=template)
-    items = map(tweet_to_feeditem, results.get_tweets())
+    items = map(tweet_to_feeditem, results.get_tweets(reverse=True))
     feed = feedtemplate.render(
             items = items,
             query=results.get_query(),

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