diff -r 861cae17abda -r 6e8930a1b8f7 script/lib/iri_tweet/utils.py --- a/script/lib/iri_tweet/utils.py Thu Mar 31 12:45:50 2011 +0200 +++ b/script/lib/iri_tweet/utils.py Wed Apr 06 16:39:42 2011 +0200 @@ -1,15 +1,17 @@ -from models import * -from sqlalchemy.sql import select, or_ -import anyjson +from models import Tweet, User, Hashtag, EntityHashtag, EntityUser, Url, \ + EntityUrl, CONSUMER_KEY, CONSUMER_SECRET, APPLICATION_NAME, ACCESS_TOKEN_KEY, \ + ACCESS_TOKEN_SECRET, adapt_date, adapt_json +from sqlalchemy.sql import select, or_ #@UnresolvedImport +import anyjson #@UnresolvedImport import datetime import email.utils -import logging +import logging #@UnresolvedImport import os.path import sys -import twitter -import twitter.oauth -import twitter.oauth_dance -import twitter_text +import twitter.oauth #@UnresolvedImport +import twitter.oauth_dance #@UnresolvedImport +import twitter_text #@UnresolvedImport + CACHE_ACCESS_TOKEN = {} @@ -22,7 +24,7 @@ return CACHE_ACCESS_TOKEN[application_name] if token_file_path and os.path.exists(token_file_path): - logging.debug("reading token from file %s" % token_file_path) + logging.debug("reading token from file %s" % token_file_path) #@UndefinedVariable CACHE_ACCESS_TOKEN[application_name] = twitter.oauth.read_token_file(token_file_path) return CACHE_ACCESS_TOKEN[application_name] #read access token info from path @@ -34,7 +36,7 @@ return CACHE_ACCESS_TOKEN[application_name] def parse_date(date_str): - ts = email.utils.parsedate_tz(date_str) + ts = email.utils.parsedate_tz(date_str) #@UndefinedVariable return datetime.datetime(*ts[0:7]) def clean_keys(dict_val): @@ -103,7 +105,7 @@ self.token_filename = token_filename def __get_user(self, user_dict): - logging.debug("Get user : " + repr(user_dict)) + logging.debug("Get user : " + repr(user_dict)) #@UndefinedVariable user_id = user_dict.get("id",None) user_name = user_dict.get("screen_name", user_dict.get("name", None)) @@ -130,8 +132,8 @@ else: user_dict = t.users.show(screen_name=user_name) except Exception as e: - logging.info("get_user : TWITTER ERROR : " + repr(e)) - logging.info("get_user : TWITTER ERROR : " + str(e)) + logging.info("get_user : TWITTER ERROR : " + repr(e)) #@UndefinedVariable + logging.info("get_user : TWITTER ERROR : " + str(e)) #@UndefinedVariable user_dict = adapt_fields(user_dict, fields_adapter["stream"]["user"]) if "id" not in user_dict: @@ -145,7 +147,7 @@ return user def __process_entity(self, ind, ind_type): - logging.debug("Process_entity : " + repr(ind) + " : " + repr(ind_type)) + logging.debug("Process_entity : " + repr(ind) + " : " + repr(ind_type)) #@UndefinedVariable ind = clean_keys(ind) @@ -200,7 +202,7 @@ 'urls' : process_urls }[ind_type]() - logging.debug("Process_entity entity_dict: " + repr(entity_dict)) + logging.debug("Process_entity entity_dict: " + repr(entity_dict)) #@UndefinedVariable if entity: self.session.add(entity) self.session.flush() @@ -217,7 +219,7 @@ # get or create user user = self.__get_user(self.json_dict["user"]) if user is None: - logging.warning("USER not found " + repr(self.json_dict["user"])) + logging.warning("USER not found " + repr(self.json_dict["user"])) #@UndefinedVariable ts_copy["user"] = None ts_copy["user_id"] = None else: @@ -265,7 +267,7 @@ user = self.__get_user(user_fields) if user is None: - logging.warning("USER not found " + repr(user_fields)) + logging.warning("USER not found " + repr(user_fields)) #@UndefinedVariable tweet_fields["user"] = None tweet_fields["user_id"] = None else: @@ -310,8 +312,8 @@ else: logging_config["filename"] = options.logfile - logging_config["level"] = max(logging.NOTSET, min(logging.CRITICAL, logging.WARNING - 10 * options.verbose + 10 * options.quiet)) - logging.basicConfig(**logging_config) + logging_config["level"] = max(logging.NOTSET, min(logging.CRITICAL, logging.WARNING - 10 * options.verbose + 10 * options.quiet)) #@UndefinedVariable + logging.basicConfig(**logging_config) #@UndefinedVariable options.debug = (options.verbose-options.quiet > 0) @@ -328,7 +330,7 @@ query = session.query(Tweet).join(EntityHashtag).join(Hashtag) if tweet_exclude_table is not None: - query = query.filter(~Tweet.id.in_(select([tweet_exclude_table.c.id]))) + query = query.filter(~Tweet.id.in_(select([tweet_exclude_table.c.id]))) #@UndefinedVariable query = query.filter(Tweet.created_at >= start_date).filter(Tweet.created_at <= end_date) @@ -338,7 +340,7 @@ return l htags = reduce(merge_hash, hashtags, []) - query = query.filter(or_(*map(lambda h: Hashtag.text.contains(h), htags))) + query = query.filter(or_(*map(lambda h: Hashtag.text.contains(h), htags))) #@UndefinedVariable return query @@ -347,7 +349,7 @@ query = session.query(User).join(Tweet).join(EntityHashtag).join(Hashtag) if tweet_exclude_table is not None: - query = query.filter(~Tweet.id.in_(select([tweet_exclude_table.c.id]))) + query = query.filter(~Tweet.id.in_(select([tweet_exclude_table.c.id]))) #@UndefinedVariable query = query.filter(Tweet.created_at >= start_date).filter(Tweet.created_at <= end_date) @@ -357,7 +359,7 @@ return l htags = reduce(merge_hash, hashtags, []) - query = query.filter(or_(*map(lambda h: Hashtag.text.contains(h), htags))) + query = query.filter(or_(*map(lambda h: Hashtag.text.contains(h), htags))) #@UndefinedVariable return query.distinct()