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

skayser at users.sourceforge.net skayser at users.sourceforge.net
Thu Oct 27 15:11:33 CEST 2011


Revision: 576
          http://opencsw.svn.sourceforge.net/opencsw/?rev=576&view=rev
Author:   skayser
Date:     2011-10-27 13:11:33 +0000 (Thu, 27 Oct 2011)
Log Message:
-----------
twitter/sweets: add -n to cap the amount of stored results

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

Modified: twitter/sweets/sweets.py
===================================================================
--- twitter/sweets/sweets.py	2011-10-26 09:51:35 UTC (rev 575)
+++ twitter/sweets/sweets.py	2011-10-27 13:11:33 UTC (rev 576)
@@ -82,7 +82,7 @@
     )
     print msg.encode(sys.stdout.encoding, 'replace')
 
-def run(interval, query, outfile, state={}):
+def run(interval, query, outfile, keep, state={}):
     """Run the main loop which queries Twitter and writes the output."""
     tweets = read_jsonfile(outfile)
     while 1:
@@ -92,7 +92,7 @@
                 tweets.extend(new_tweets)
                 tweets = dedup_tweets(tweets)
                 for t in new_tweets: log_tweet(t)
-                write_jsonfile(outfile, tweets)
+                write_jsonfile(outfile, tweets[-keep:])
             else:
                 print "No new tweets (%s in archive)" % len(tweets)
         except urllib2.URLError, e:
@@ -111,16 +111,16 @@
     op.add_option("-s", dest="query", help="query term to run on Twitter")
     op.add_option("-t", dest="interval", default=300,
                         help="query every X seconds (def: 300)")
+    op.add_option("-n", dest="limit", default=100,
+                        help="amount of results to keep (def: 100)")
     op.add_option("-o", dest="output", metavar="FILE",
                         help="store tweets in FILE (def: tweets-<query>.json)")
     op.add_option("-d", dest="debug", action="store_true",
                         help=SUPPRESS_HELP)
     (options, args) = op.parse_args()
 
-    required_options = ( "query", )
-    for o in required_options:
-        if not getattr(options, o):
-            parser.print_help()
+    if not options.query:
+            op.print_help()
             sys.exit(1)
 
     if not options.output:
@@ -128,5 +128,8 @@
 
     try:
         if options.debug: options.interval = sys.stdin.readline
-        run(options.interval, options.query, options.output)
+        run(interval=options.interval,
+                query=options.query,
+                output=options.output,
+                keep=options.limit)
     except KeyboardInterrupt: pass

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