给用NexusPHP的PT站写的分流/保种员自动发工资脚本

如题.使用说明:
1, 为实现统计本月流量/保种时间(NexusPHP默认的数据表没有留这些数据), 需要在users表里增加 downloaded_lastmonth, uploaded_lastmonth, seedtime_lastmonth 三个字段.
2, 需要获取分流员/保种员信息, 也需指定pipeliner/guarder字段(enum或boolean, 后者需略微改动此代码)
3, 需要python2.x环境, 以及python-MySQLDb库.
4, 需要服务器上有正常的crontab, 每月1日0点执行即可.
5, 此脚本还提供了记录上月实际上传下载(不含优惠信息), 以及下载时间的统计部分, 相应部分默认已注释掉, 可以手动替换启用. 如需显示在userdetails页面上, 只需做一个简单的减法(当前-上月). 实际上传/下载量的统计还需修改announce.php.
6, 工资(魔力/邀请)参考下面的设置修改. 本初始数据来自CMCT-PT的考核标准和奖励细则.
7, 修改数据库连接字符串的相应部分, 即可用.

#!/usr/bin/env python2

import MySQLdb, math, datetime
from MySQLdb.cursors import DictCursor

db = MySQLdb.connect("localhost", "user", "password", "database", charset = "utf8", connect_timeout = 5)
cs = db.cursor(DictCursor)

query = "SELECT * FROM users"
cs.execute(query)
alldata = cs.fetchall()
pays = {}
for user in alldata:
    id = user['id']
    username = user['username']
    downloaded = user["downloaded"]
    uploaded = user["uploaded"]
    #raw_downloaded = user["raw_downloaded"]
    #raw_uploaded = user["raw_uploaded"]
    seedtime = user["seedtime"]
    #leechtime = user["leechtime"]
    
    downloaded_lastmonth = user["downloaded_lastmonth"]
    uploaded_lastmonth = user["uploaded_lastmonth"]
    #raw_downloaded_lastmonth = user["raw_downloaded_lastmonth"]
    #raw_uploaded_lastmonth = user["raw_uploaded_lastmonth"]
    seedtime_lastmonth = user["seedtime_lastmonth"]
    #leechtime_lastmonth = user["leechtime_lastmonth"]
    
    pipeliner = (user['pipeliner'] == 'yes')
    guarder = (user['guarder'] == 'yes')
    
    if pipeliner:
        if not pays.has_key(username):
            pays[username] = {"id": id}
        pays[username]["uploaded_thismonth"] = uploaded - uploaded_lastmonth
    
    if guarder:
        if not pays.has_key(username):
            pays[username] = {"id": id}
        pays[username]["seedtime_thismonth"] = seedtime - seedtime_lastmonth
        
    #query = "UPDATE users SET downloaded_lastmonth = %s, uploaded_lastmonth = %s, raw_downloaded_lastmonth = %s, raw_uploaded_lastmonth = %s, seedtime_lastmonth = %s, leechtime_lastmonth = %s WHERE id = %s"
    #cs.execute(query, (downloaded, uploaded, raw_downloaded, raw_uploaded, seedtime, leechtime, id))
    query = "UPDATE users SET downloaded_lastmonth = %s, uploaded_lastmonth = %s, seedtime_lastmonth = %s, WHERE id = %s"
    cs.execute(query, (downloaded, uploaded, seedtime, id))

for user in pays.keys():
    karma = 0
    invite = 0
    strprt = []
    if pays[user].has_key("uploaded_thismonth"):
        warning = False
        data = pays[user]['uploaded_thismonth']
        
        lst = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB']
        if data > 0:
            i = int(math.floor(math.log(data, 1024)))
        else:
            i = 0
        if i >= len(lst):
            i = len(lst) - 1
        data_formated = ('%.2f' + " " + lst[i]) % (data / math.pow(1024, i))
        strprt += [user + " is pipeliner, uploaded: " + data_formated]
        
        if data < 107374182400: # 100G
            warning = True
        elif data < 322122547200:
            karma += 1000
        elif data < 536870912000:
            karma += 2000
            invite += 1
        elif data < 966367641600:
            karma += 4000
            invite += 1
        elif data < 1610612736000:
            karma += 6000
            invite += 2
        elif data < 2147483648000:
            karma += 8000
            invite += 3
        else:
            karma += 10000
            invite += 4
        
        if warning:
            print "WARNING: Uploaded less than 100GB last month!"
        
    if pays[user].has_key("seedtime_thismonth"):
        warning = False
        seconds_raw = pays[user]['seedtime_thismonth']
        seconds_datetime = datetime.timedelta(seconds=seconds_raw)
        time_print = str(seconds_datetime)
        days_calc = seconds_datetime.days
        
        strprt += [user + " is guarder, seed time: " + time_print]
        
        if days_calc < 100:
            warning = True
        elif days_calc < 300:
            karma += 1000
        elif days_calc < 500:
            karma += 2000
            invite += 1
        elif days_calc < 900:
            karma += 4000
            invite += 1
        elif days_calc < 1500:
            karma += 6000
            invite += 2
        elif days_calc < 2000:
            karma += 8000
            invite += 3
        else:
            karma += 10000
            invite += 4
        
        if warning:
            print "WARNING: Seeded less than 100 days last month!"
    
    strprt += ["Total awarded: %d Karma points and %d invite(s)." % (karma, invite)]
    print "\n".join(strprt)
    
    query = "INSERT INTO messages (sender, receiver, added, msg, subject, saved, location) VALUES(%s, %s, %s, %s, %s, %s, %s)"
    cs.execute(query, (0, pays[user]['id'], datetime.datetime.now(), "Awards: " + "\n".join(strprt), "Awards", "no", 1))
    
    query = "UPDATE users SET last_pm = NOW(), invites = invites + %d, seedbonus = seedbonus + %d WHERE id = " % (invite, karma)
    query += "%s"
    cs.execute(query, (pays[user]['id'], ))
    
    print

db.commit()
cs.close()

8 thoughts on “给用NexusPHP的PT站写的分流/保种员自动发工资脚本”

        1. 那个我在使用说明里解释过了- – 不知道怎么写config好, 反正那部分都集中在一起了, 就天然magicnumber算了….
          此外, 我给了个注释说明那货是100G的….

Leave a Reply

Your email address will not be published. Required fields are marked *

QR Code Business Card