script/lib/iri_tweet/utils.py
changeset 21 8003bcd8d9a2
parent 18 bd595ad770fc
child 24 71a4f21bc6fc
--- a/script/lib/iri_tweet/utils.py	Fri Jan 21 13:42:03 2011 +0100
+++ b/script/lib/iri_tweet/utils.py	Fri Jan 21 18:03:19 2011 +0100
@@ -1,7 +1,7 @@
 from models import *
+import anyjson
 import datetime
 import email.utils
-import anyjson
 import logging
 import os.path
 import sys
@@ -11,17 +11,26 @@
 import twitter_text
 
 
+CACHE_ACCESS_TOKEN = None
+
 def get_oauth_token(token_file_path=None):
     
+    global CACHE_ACCESS_TOKEN
+    
+    if CACHE_ACCESS_TOKEN is not None:
+        return CACHE_ACCESS_TOKEN
+    
     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)
+        CACHE_ACCESS_TOKEN = twitter.oauth.read_token_file(token_file_path)
+        return CACHE_ACCESS_TOKEN
         #read access token info from path
     
     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_file_path)
+    CACHE_ACCESS_TOKEN = twitter.oauth_dance.oauth_dance(APPLICATION_NAME, CONSUMER_KEY, CONSUMER_SECRET, token_file_path)
+    return CACHE_ACCESS_TOKEN
 
 def parse_date(date_str):
     ts = email.utils.parsedate_tz(date_str)
@@ -203,7 +212,7 @@
         # get or create user
         user = self.__get_user(self.json_dict["user"])
         if user is None:
-            log.warning("USER not found " + repr(ts["user"]))
+            logging.warning("USER not found " + repr(ts["user"]))
             ts_copy["user"] = None
             ts_copy["user_id"] = None
         else:
@@ -251,7 +260,7 @@
         
         user = self.__get_user(user_fields)
         if user is None:
-            log.warning("USER not found " + repr(user_fields))
+            logging.warning("USER not found " + repr(user_fields))
             tweet_fields["user"] = None
             tweet_fields["user_id"] = None
         else:
@@ -260,7 +269,7 @@
         
         tweet_fields = adapt_fields(tweet_fields, fields_adapter["rest"]["tweet"])
         self.tweet = Tweet(**tweet_fields)
-        session.add(self.tweet)
+        self.session.add(self.tweet)
         
         text = self.tweet.text