import sys from sqlalchemy import * from datetime import datetime, timedelta import traceback from common import * wightAppList = [] def main(db_url, date): print '>>>bill.py', date db = create_engine(db_url).connect() time = date_to_int(date) db.execute(media_stat_table.delete().where(and_(media_stat_table.c.daytime >= time, media_stat_table.c.daytime < time + 24))) for line in sys.stdin: try: parse_line(line, db, date) except: print "error", line #traceback.print_exc() #sys.exit(1) def parse_line(line, db, date): global wightAppList (fee, click, view, download, install, uaid, upid, style, dayhour, dsp, tagid, dspLevel1) = line.rstrip("\n\r").split('\t') fee = int(fee) click = int(click) view = int(view) download = int(download) install = int(install) dayhour = int(dayhour) style = int(style) try: uaid = int(uaid) except: print "error uaid", line return if uaid in blackAppList: return # remove dirty data if len(upid) != 32: return if(str(dayhour).startswith("201")): dt = datetime.strptime(str(dayhour), '%Y%m%d%H') else: dt = datetime.fromtimestamp(dayhour * 3600) daytime = date_to_int(dt) if dt < date or dt >= date + timedelta(days=1): if fee > 0: print 'invalid daytime:', daytime, uaid, upid, style, view, fee return time = date_to_int(datetime.now()) #print 'data:', daytime, uaid, upid, style, view developer_id = -1 category = -2 for r in db.execute('select developer_id, category from media_publisher where id = %s limit 1' % uaid): r = dict(r) developer_id = r['developer_id'] category = r['category'] developer_type = 0 for r in db.execute('select developer_type from media_account where developer_id = %s limit 1' % developer_id): r = dict(r) developer_type = r['developer_type'] channel_ratio = 0.0 fc_ratio = 100.0 if(developer_id == -1): #没有相关的uaid print 'error: uaid not found', uaid, upid,style,view return elif(uaid in wightAppList): #白名单内的app不扣费 channel_ratio = 0.0 fc_ratio = 0.0 elif(developer_id == 88802): #互娱游戏中心 channel_ratio = 15.0 fc_ratio = 88.0 elif(developer_type == 1): #MIUI内部开发者 channel_ratio = 0.0 fc_ratio = 100.0 elif(developer_type == 2): #非MIUI内部开发者 channel_ratio = 15.0 fc_ratio = 80.0 elif(developer_type == 3): #代理开发者 channel_ratio = 20.0 fc_ratio = 90.0 elif(category == 15): #互娱 channel_ratio = 15.0 fc_ratio = 50.0 else: channel_ratio = 20.0 #普通开发者 fc_ratio = 90.0 gain = fee * (1 - channel_ratio / 100) * fc_ratio / 100 data = { 'daytime': daytime, 'developer_id': developer_id, 'publisher_id': uaid, 'placement_id': upid, 'tagid': tagid, 'style': style, 'request': 0, 'adview': view, 'origin_click': click, 'click': click, #filter_click(view, style, click), 'download': download, 'install': install, 'origin_gain': fee, 'gain': gain, 'filter_gain': gain, #filter_fee(view, style, gain), 'mtime': time, 'ctime': time, 'package_name': '', 'dsp': dsp, 'dsplevel1': dspLevel1 } db.execute(media_stat_table.insert(), [data]) if __name__ == '__main__': try: db_url = sys.argv[1] date = datetime.strptime(sys.argv[2], '%Y%m%d') main(db_url, date) except: print "error processing billing data", traceback.format_exc() sys.exit(1)
python 脚本示例
最新推荐文章于 2024-08-21 17:14:07 发布