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

skayser at users.sourceforge.net skayser at users.sourceforge.net
Thu Oct 27 21:02:13 CEST 2011


Revision: 579
          http://opencsw.svn.sourceforge.net/opencsw/?rev=579&view=rev
Author:   skayser
Date:     2011-10-27 19:02:12 +0000 (Thu, 27 Oct 2011)
Log Message:
-----------
twitter/sweets: add input/output option handling and __main__ block to sweets2feed.py

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

Modified: twitter/sweets/sweets.py
===================================================================
--- twitter/sweets/sweets.py	2011-10-27 17:31:52 UTC (rev 578)
+++ twitter/sweets/sweets.py	2011-10-27 19:02:12 UTC (rev 579)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #
-# sweets_search.py:
+# sweets.py:
 #   Search twitter with a search term and store results in a JSON file which
 #   can be used for further processing (e.g. to generate RSS)
 #
@@ -54,6 +54,9 @@
         f.close()
         os.rename(name, destfile)
 
+    def get_query(self):
+        return self.data.get('query')
+
     def get_tweets(self):
         """Get the list of tweets in the response object."""
         return self.data.get('results', [])

Modified: twitter/sweets/sweets2feed.py
===================================================================
--- twitter/sweets/sweets2feed.py	2011-10-27 17:31:52 UTC (rev 578)
+++ twitter/sweets/sweets2feed.py	2011-10-27 19:02:12 UTC (rev 579)
@@ -5,16 +5,17 @@
 #   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
+import tempfile
 from dateutil.parser import parse
 from datetime import *
 from mako.template import Template
+from optparse import OptionParser
 
 def tweet_to_feeditem(tweet):
     """Convert a tweet into a feed item."""
@@ -25,15 +26,36 @@
     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)
+def write_via_temp(text, destfile):
+    (fd, name) = tempfile.mkstemp()
+    f = os.fdopen(fd, 'w')
+    f.write(text.encode('utf-8'))
+    f.close()
+    os.rename(name, destfile)
 
-feedtemplate = Template(filename="template.feed")
-r = sweets.TSResponse(file=sys.argv[1])
+def write_feed(results, template, destfile):
+    feedtemplate = Template(filename=template)
+    items = map(tweet_to_feeditem, results.get_tweets())
+    feed = feedtemplate.render(
+            items = items,
+            query=results.get_query(),
+            updated=datetime.now().isoformat())
+    write_via_temp(feed, destfile)
+    
 
-print feedtemplate.render(
-        items=map(tweet_to_feeditem, r.get_tweets()),
-        query=r.data['query'],
-        updated=datetime.now().isoformat())
+if __name__ == '__main__':
+    usage = "%prog -i <inputfile> -o <outputfile>"
+    parser = OptionParser(usage=usage)
+    parser.add_option("-i", dest="input",  help="read JSON input from FILE")
+    parser.add_option("-o", dest="output", help="output ATOM feed to FILE")
+    parser.add_option("-t", dest="template", default="template.feed",
+                            help="template (def: template.feed")
+    (options, args) = parser.parse_args()
+    if not (options.input and options.output):
+        parser.print_usage()
+        sys.exit(1)
+
+    stored_results = sweets.TSResponse(file=options.input)
+    write_feed(stored_results, options.template, options.output)
+    print "Written %s tweets from %s to %s" % (
+        len(stored_results.get_tweets()), options.input, options.output)

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