def create_profile(self, logger, msisdn): assert type(msisdn) in (int, long, str, list) """phonelist = [] if(type(msisdn) in (int, long, str)): phonelist = [str(msisdn)] else: phonelist = list """ #prepare sql statement db = DBCon().get_connection(logger) conf = UVConf(); l_lang = conf.get("core", "lang") l_telcoid = conf.get("core", "telcoid") l_query = "INSERT INTO tbl_user_profile(msisdn, user_name, display_name, channel, created_ts, operator_id, lang, privacy) VALUES ('%s','%s','%s',21, now(), '%s', '%s', 'public')"%(msisdn, msisdn, msisdn, l_lang, l_telcoid) #l_valuepart = '' #for phnum in phonelist: # l_valuepart = l_valuepart + "('%s','%s','%s',now(), '%s', '%s', 'public')"%(phnum, phnum, phnum, l_lang, l_telcoid) #l_query = l_query + l_valuepart logger.debug("user profile creation query = %s", l_query) try: cursor = db.cursor() cursor.execute(l_query) except: logger.critical("Failed to run query %s. Terminating now", l_query) logger.exception(traceback.print_exc()) sys.exit(1)
class TweetFlyApp: app_logger = None log_filename = "/usr/local/uv/log/tweetfly.log" conf_filename = "/usr/local/uv/conf/tweetfly.conf" log_level = logging.DEBUG app_conf = None normalizer = None def init(self, p_conf_filename, p_log_filename, p_log_level): if ( False == os.path.exists(os.path.dirname(p_log_filename)) ): sys.stderr.write("Log directory %s does not exists. Creating new directory\n" % (os.path.dirname(p_log_filename))) sys.stderr.flush() os.makedirs(os.path.dirname(p_log_filename)) self.app_logger = genutils.init_logger(__name__, p_log_filename, p_log_level); self.app_logger.info("TweetFlyApp logger initialized" ) self.app_logger.info("p_conf_filename=%s p_log_filename=%s p_log_level=%s" ,p_conf_filename, p_log_filename ,str(p_log_level) ) #check log_filename and its path existance if ( False == os.path.exists(p_conf_filename) ): self.app_logger.critical("Configuration file %s does not exists. Terminating application now", p_conf_filename) sys.exit(1) conf_filename = p_conf_filename self.app_logger.info("Initializing TweetFlyApp with configuration file %s", conf_filename ) self.app_conf = UVConf() self.app_conf.init(self.app_logger, conf_filename); self.app_logger.info("TweetFlyApp configuration initialization done") #Loading normalization rules self.normalizer = NumNormalizer() self.normalizer.load_rules_from_db(self.app_logger) def start(self): #TODO Initialize TweetStreamListner #TODO Authenticate #TODO Wait for updates for i in range(200000): phnum = '91988' + '{num:05d}'.format(num=i) self.app_logger.info("I am creating userprofile - %d - %s " % (i, phnum)) uprofile = UserProfile() uprofile.create_profile(self.app_logger,phnum) def on_tweet_update(self): #TODO Write tweet into db pass
def get_connection(self, logger): conf = UVConf(); l_host = conf.get("db","coredb_host") l_user = conf.get("db","coredb_user") l_passwd = conf.get("db","coredb_passwd") l_db = conf.get("db","coredb_name") logger.info("Mysql Connection setup params - host = %s, user = %s, passwd = %s, db = %s", l_host, l_user, l_passwd, l_db) try: self.db = MySQLdb.connect(host=l_host, user=l_user, passwd=l_passwd, db=l_db) logger.info("connected to database. %s", str(self.db)) except: logger.critical("Failed to connect to database. Terminating now. Traceback ...") logger.exception(traceback.print_exc()) sys.exit(1) return self.db
def init(self, p_conf_filename, p_log_filename, p_log_level): if ( False == os.path.exists(os.path.dirname(p_log_filename)) ): sys.stderr.write("Log directory %s does not exists. Creating new directory\n" % (os.path.dirname(p_log_filename))) sys.stderr.flush() os.makedirs(os.path.dirname(p_log_filename)) self.app_logger = genutils.init_logger(__name__, p_log_filename, p_log_level); self.app_logger.info("TweetFlyApp logger initialized" ) self.app_logger.info("p_conf_filename=%s p_log_filename=%s p_log_level=%s" ,p_conf_filename, p_log_filename ,str(p_log_level) ) #check log_filename and its path existance if ( False == os.path.exists(p_conf_filename) ): self.app_logger.critical("Configuration file %s does not exists. Terminating application now", p_conf_filename) sys.exit(1) conf_filename = p_conf_filename self.app_logger.info("Initializing TweetFlyApp with configuration file %s", conf_filename ) self.app_conf = UVConf() self.app_conf.init(self.app_logger, conf_filename); self.app_logger.info("TweetFlyApp configuration initialization done") #Loading normalization rules self.normalizer = NumNormalizer() self.normalizer.load_rules_from_db(self.app_logger)