python 脚本示例


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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值