class ImportDirect(object): """回调广告商通知激活""" def __init__(self, filename, filetype): self.initdb() self.logfileHandle = open(config.LOGPATH + "import_direct.txt", 'a') self.import_direct_fieldstr = '`imei`, `appkey`, `app_interest`, `pay_ability`, `game_frequency`' self.filename = filename self.filetype = filetype def initdb(self): self.db = MySQL(config.MOBGI_HOUSEAD['host'], config.MOBGI_HOUSEAD['user'], config.MYSQL['passwd'], port=config.MOBGI_HOUSEAD['port'], db=config.MOBGI_HOUSEAD['db']) return self.db def mylog(self, msg): print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + msg self.logfileHandle.write( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + msg + '\n') def getRows(self, select_sql): try: self.db.queryNotCatch(select_sql) except MySQLdb.Error as m: self.mylog("db reconnect!") self.initdb() self.db.query(select_sql) result = self.db.fetchAll() return result #新增记录 def insertRows(self, insert_sql): self.cur_sql = insert_sql try: self.db.queryNotCatch(insert_sql) return self.db.commit() except MySQLdb.Error as m: self.mylog("db reconnect!") self.initdb() self.db.queryNotCatch(insert_sql) return self.db.commit() def run(self): self.mylog('start ImportDirect.') appkey = config.APPKEY[self.filetype] self.mylog('filename:' + self.filename) data = xlrd.open_workbook(self.filename) table = data.sheets()[0] nrows = table.nrows #行数 ncols = table.ncols #列数 print nrows, ncols for i in range(0, nrows): imei = table.cell(i, 0).value if type(imei) is types.FloatType: #科学计数转成int型 imei = int(imei) interest = table.cell(i, 1).value user_type = table.cell(i, 2).value pay_type = table.cell(i, 3).value for key in config.INTEREST: # print key, config.INTEREST[key] interest = interest.replace(str(key), str(config.INTEREST[key])) select_sql = 'select * from delivery_device_direct where imei="' + str( imei) + '" and appkey= "' + appkey + '"' result = self.getRows(select_sql) if len(result): self.mylog('imei:' + str(imei) + ', appkey:' + appkey + " exist!") else: curTime = time.time() insert_sql = 'insert into delivery_device_direct(`imei`, `appkey`, `app_interest`, `pay_ability`, `game_frequency`, `create_time`, `update_time`) value("' + str( imei) + '", "' + str(appkey) + '","' + str( interest) + '","' + str(pay_type) + '","' + str( user_type) + '","' + str(curTime) + '","' + str( curTime) + '");' print insert_sql self.insertRows(insert_sql) print i self.mylog('end ImportDirect.')
class RecoveryExpiredVirtualMoney(object): """回调广告商通知激活""" def __init__(self): self.initdb() self.logfileHandle=open(config.LOGPATH + "recovery_expired_virtualmoney_setting.txt", 'a') def initdb(self): self.db = MySQL(config.MOBGI_HOUSEAD['host'], config.MOBGI_HOUSEAD['user'], config.MOBGI_HOUSEAD['passwd'], port=config.MOBGI_HOUSEAD['port'], db=config.MOBGI_HOUSEAD['db']) return self.db def mylog(self,msg): print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+ " " + msg self.logfileHandle.write(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+" " + msg + '\n') def getRows(self,select_sql): try: self.db.queryNotCatch(select_sql) except MySQLdb.Error as m: self.mylog("db reconnect!") self.initdb() self.db.query(select_sql) result = self.db.fetchAll(); return result #新增记录 def insertRows(self,insert_sql): try: self.db.queryNotCatch(insert_sql) return self.db.commit() except MySQLdb.Error as m: self.mylog("db reconnect!") self.initdb() self.db.query(insert_sql) return self.db.commit() #更新记录 def updateRows(self,update_sql): try: self.db.queryNotCatch(update_sql) return self.db.commit() except MySQLdb.Error as m: self.mylog("db reconnect!") self.initdb() self.db.query(update_sql) return self.db.commit() def getExpiredVirtualdetail(self): curtime = str(time.time()) expired_sql = "select * from advertiser_account_virtual_detail where status='normal' and expire_time < '" + curtime + "'" expired_result = self.getRows(expired_sql) return expired_result def getAccount(self, uid, account_type): account_sql = "select * from advertiser_account_detail where uid= '" + str(uid) + "' and account_type='" + account_type +"'" account_result = self.getRows(account_sql) if len(account_result) == 0: self.mylog('cant get account_result. sql:' + account_sql) return False else: return account_result[0] def run(self): self.mylog('start RecoveryExpiredVirtualMoney.') self.expired_records = self.getExpiredVirtualdetail() if len(self.expired_records) == 0: self.mylog('len(self.expired_records) == 0') self.mylog('sleep ' + str(config.SLEEPSECONDS) + ' seconds!\n') time.sleep(config.SLEEPSECONDS) return False; curtime = time.time() self.mylog('curtime:' + str(curtime)) if config.REDIS.has_key('password') is True: poolr=redis.ConnectionPool(host=config.REDIS["host"],port=config.REDIS["port"],password=config.REDIS["password"]) else: poolr=redis.ConnectionPool(host=config.REDIS["host"],port=config.REDIS["port"]) r=redis.Redis(connection_pool=poolr) for item in self.expired_records: accountdetail = self.getAccount(item['uid'], item['account_type']) self.mylog('account detail: ' + 'uid:' + accountdetail['uid'] + ", account_type:" + accountdetail['account_type']+ ", balance:" + accountdetail['balance']) self.mylog( 'account virtual detail: ' + 'id:' + item['id'] + ", uid:" + item['uid']+ ", account_type:" + item['account_type']+ ", balance:" + item['balance']+ ", status:" + item['status']+ ", taskdetailid:" + item['taskdetailid'] + ", expire_time:" + item['expire_time']) if float(accountdetail['balance'])<float(item['balance']): new_balance = 0 real_fee = float(accountdetail['balance']) else: new_balance = float(accountdetail['balance']) - float(item['balance']) real_fee = float(item['balance']) #(1)更改虚拟帐呢余额 update_account_detail_sql = 'update advertiser_account_detail set balance = "' + str(new_balance) + '" where uid="' + str(accountdetail['uid']) + '" and account_type="' +accountdetail['account_type'] + '"' self.updateRows(update_account_detail_sql) self.mylog('real_fee:'+ str(real_fee) + ', new_balance:' + str(new_balance) ) self.mylog('update_account_detail_sql:' + update_account_detail_sql) #(2)更改帐户虚拟金过期状态 update_virtual_detail_sql = 'update advertiser_account_virtual_detail set status = "expired", operator="py_script",update_time="' + str(curtime) + '" where id="' + str(item['id'] + '"') self.updateRows(update_virtual_detail_sql) self.mylog('update_virtual_detail_sql:' + update_virtual_detail_sql) #(3)添加帐户流水 account_log_value_str = "'" + item['uid'] + "', '" + item['account_type'] + "', 'recovery', '" + str(real_fee) + "', '虚拟金过期回收', '" + str(curtime)+ "'" add_account_log_sql = 'insert into advertiser_account_log(uid, account_type, operate_type, trade_balance, description, create_time) value(' +account_log_value_str+ ')' self.updateRows('set names utf8;')#处理编码问题 self.insertRows(add_account_log_sql) self.mylog('add_account_log_sql:' + add_account_log_sql) #(4)删除帐户余额缓存 total_balance_rediskey = REDIS.REDIS_CACHEKEY['ACCOUNT_TOTAL_BALANCE'] + '_'+ item['uid'] r.delete(total_balance_rediskey) self.mylog('delete redis cache:' + total_balance_rediskey + "\n") self.mylog('end RecoveryExpiredVirtualMoney.')
class ThirdReportRecordStore(object): def __init__(self): # 数据库参数 self.host = third_party_setting.MYSQL_REQUST['host'] self.user = third_party_setting.MYSQL_REQUST['user'] self.passwd = third_party_setting.MYSQL_REQUST['passwd'] self.port = third_party_setting.MYSQL_REQUST['port'] self.db = third_party_setting.MYSQL_REQUST['db'] self.table = third_party_setting.MYSQL_REQUST['table'] # 文件存放路径 self.sqlpath = os.path.join(os.getcwd(), third_party_setting.SQLFILEPATH) self.fieldlist = third_party_setting.FIELD_LIST # 不用logging日志是因为上个目录已经使用logging了,非继承关系被覆盖 def mylog(self, msg): print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + msg self.logfileHandle.write( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + msg + '\n') # 重连db def trysql(self, sql): try: self.mylog("select sql:" + sql) self.clickdb.queryNotCatch(sql) except MySQLdb.Error as m: self.mylog('masterdb reconnecting now') self.clickdb = MySQL(self.host, self.user, self.passwd, port=self.port, db=self.db) self.clickdb.query(sql) return self.clickdb.fetchAll() def saveDataToFile(self, streamlist): try: MIN = time.strftime("%Y%m%d%H%M", time.localtime()) cf = ConfigParser.ConfigParser() confname = 'third_report_record.conf' cf.read(confname) curtable = 'record_id' reocrd_id = cf.getint("third_report_data", curtable) reocrd_id = int(reocrd_id) #按年_月_日_时_分创建文件,一分钟一个文件 self.filename = self.sqlpath + "sql_content_" + MIN + ".sql" file_object = codecs.open(self.filename, 'a', 'utf-8') for stream in streamlist: print stream sqlcontent = '' valueStr = "" # 增加id valueStr += str(reocrd_id) + '\t' reocrd_id += 1 for field_key in self.fieldlist: if stream.has_key(field_key): valueStr += str(stream[field_key]) + '\t' else: valueStr += "" + '\t' sqlcontent += valueStr.strip('\t') + "\n" print sqlcontent if sqlcontent != "": file_object.write(sqlcontent) file_object.close() cf.set("third_report_data", curtable, reocrd_id) cf.write(open(confname, "w")) except Exception, e: self.mylog("step1 save data to file failed! error:" + str(e))