Commits

Steve Losh  committed 2c33950

More work. Not done.

  • Participants
  • Parent commits e60341c

Comments (0)

Files changed (1)

File grabtweets.py

 #!/usr/bin/env python
 
-import os, sys
+import os, sys, json
+from optparse import OptionParser
 
 BASE_DIR = os.path.abspath(os.path.dirname(__file__))
 PYTHON_TWITTER_PATH = os.path.join(BASE_DIR, 'python_twitter')
 sys.path.append(PYTHON_TWITTER_PATH)
 
-import json
 from python_twitter import twitter
 
-api = twitter.Api()
-statuses = api.GetUserTimeline('stevelosh')
+#tweets = dict((s.id, s.text) for s in statuses)
+#print json.dumps(tweets)
 
-tweets = [s.text for s in statuses]
-print json.dump(tweets)
+def _load_file(filename):
+    if not os.path.exists(filename):
+        return {}
+    with open(filename, 'r') as f:
+        return json.load(f)
+
+def _get_more_tweets(username, existing_tweets):
+    api = twitter.Api()
+    
+    
+    last_id = int(max(existing_tweets.keys())) if existing_tweets else 0
+    first_id = int(min(existing_tweets.keys())) if existing_tweets else None
+    
+    statuses = api.GetUserTimeline(username, max_id=first_id, count=5)
+    existing_tweets.update(dict((s.id, s.text) for s in statuses))
+    
+    statuses = api.GetUserTimeline(username, since_id=0, count=5)
+    existing_tweets.update(dict((s.id, s.text) for s in statuses))
+    
+    return existing_tweets
+
+def _dump_to_file(filename, tweets):
+    with open(filename, 'w') as f:
+        json.dump(tweets, f)
+    
+
+def _build_parser():
+    """Return a parser for the command-line interface."""
+    usage = "Usage: %prog -u USERNAME FILE"
+    parser = OptionParser(usage=usage)
+    
+    parser.add_option("-u", "--username", dest="username", default=None,
+        help="grab (new) tweets from USERNAME", metavar="USERNAME")
+    parser.add_option("-p", "--print",
+        dest="print_tweets", action="store_true", default=False,
+        help="print the tweets in FILE", metavar="FILE")
+    
+    return parser
+
+def _main():
+    """Run the command-line interface."""
+    parser = _build_parser()
+    (options, args) = parser.parse_args()
+    
+    if len(args) != 1:
+        parser.error('You must specify a FILE!')
+    
+    tweets = _load_file(args[0])
+    
+    if options.print_tweets:
+        print tweets
+        for tid in tweets:
+            print tid, tweets[tid]
+            print ''
+        return
+    
+    if not options.username:
+        parser.error('You must specify a USERNAME!')
+    
+    tweets = _get_more_tweets(options.username, tweets)
+    _dump_to_file(args[0], tweets)
+
+
+if __name__ == '__main__':
+    _main()