script/lib/iri_tweet/utils.py
changeset 15 5d552b6a0e55
parent 12 4daf47fcf792
child 18 bd595ad770fc
--- a/script/lib/iri_tweet/utils.py	Tue Jan 18 18:25:18 2011 +0100
+++ b/script/lib/iri_tweet/utils.py	Thu Jan 20 10:44:04 2011 +0100
@@ -3,16 +3,17 @@
 import email.utils
 import json
 import logging
+import os.path
 import sys
 import twitter
+import twitter.oauth
+import twitter.oauth_dance
 import twitter_text
-import os.path
-import twitter.oauth
 
 
 def get_oauth_token(token_file_path=None):
     
-    if token_file_path and os.path.file_exists(token_file_path):
+    if token_file_path and os.path.exists(token_file_path):
         logging.debug("reading token from file %s" % token_file_path)
         return twitter.oauth.read_token_file(token_file_path)
         #read access token info from path
@@ -20,7 +21,7 @@
     if 'ACCESS_TOKEN_KEY' in dict() and 'ACCESS_TOKEN_SECRET' in dict() and ACCESS_TOKEN_KEY and ACCESS_TOKEN_SECRET:
         return ACCESS_TOKEN_KEY,ACCESS_TOKEN_SECRET
     
-    return twitter.oauth_dance.oauth_dance(APPLICATION_NAME, CONSUMER_KEY, CONSUMER_SECRET, token_filename)
+    return twitter.oauth_dance.oauth_dance(APPLICATION_NAME, CONSUMER_KEY, CONSUMER_SECRET, token_file_path)
 
 def parse_date(date_str):
     ts = email.utils.parsedate_tz(date_str)
@@ -68,7 +69,7 @@
 
 class TwitterProcessor(object):
     
-    def __init__(self, json_dict, json_txt, session):
+    def __init__(self, json_dict, json_txt, session, token_filename=None):
 
         if json_dict is None and json_txt is None:
             raise TwitterProcessorException("No json")
@@ -87,6 +88,7 @@
             raise TwitterProcessorException("No id in json")
         
         self.session = session
+        self.token_filename = token_filename
 
     def __get_user(self, user_dict):
         logging.debug("Get user : " + repr(user_dict))
@@ -108,8 +110,8 @@
         user_created_at = user_dict.get("created_at", None)
         
         if user_created_at is None:
-            acess_token_key, access_token_secret = get_oauth_token()
-            t = twitter.Twitter(auth=twitter.OAuth(token_key, token_secret, CONSUMER_KEY, CONSUMER_SECRET))
+            acess_token_key, access_token_secret = get_oauth_token(self.token_filename)
+            t = twitter.Twitter(auth=twitter.OAuth(acess_token_key, access_token_secret, CONSUMER_KEY, CONSUMER_SECRET))
             try:
                 if user_id:
                     user_dict = t.users.show(user_id=user_id)