用Python3在Win10上自动更新Aria2的BT Trackers,提升Aria2的BT下载速度,非常实用。

# PepTrader & PepCoder
# 一切从兴趣开始...
# It all starts with interests...
# I am PepCoder
# This is a Windows version of BT Trackers Updater via Python 3.6.6
# Original work belongs to @zmr, you can find his original work at
# 最初代码源来自于 @zmr,您可以在以下网址找到其最初的作品版本
# https://gitee.com/Zero_gitee/UpdateTrackers
# Trackers的代码源是来自于: https://github.com/XIU2/TrackersListCollection


import bs4
import requests
import logging
import os
import time


# 全局配置:
# url_trackers_best = "https://trackerslist.com/all.txt"  # 这个是普通版的,可以用在其它BT用户端上
url_trackers_best = "https://trackerslist.com/all_aria2.txt"  # 这个是aria2友好版,为aria2专门设计的
aria2_config = "D:\\aria2\\aria2.conf"    # 设定好您的aira2所在位置,因为每个人放aria2的文件夹都不一样。
logFile = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logger.log')

# 配置日志参数
logging.basicConfig(filename=logFile,
                    format="%(asctime)s \n%(message)s",
                    datefmt="%Y-%m-%d %H:%M:%S",
                    filemode='w',
                    level=40)


def log(level, message):
    "写日志"
    logging.log(level, message)
    return


def getTrackers(url):
    "更新Trackers"
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    log(logging.DEBUG, "更新Trackers【开始】{0}\n url = {1}".format(t, url))
    try:
        # 获取最新数据
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                          'AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/67.0.3396.99 Safari/537.36'
        }
        response = requests.get(url, headers=headers).content
        soup = bs4.BeautifulSoup(response, 'html5lib')  # 解析
        all_trackers = soup.find(name='body').get_text()  # 复制
        print(all_trackers)
        log(logging.DEBUG, "更新Trackers【结束】{0}".format(all_trackers))
        return all_trackers
    except BaseException as e:
        log(logging.ERROR, "更新Trackers【异常】{0}".format(e))
        return None


def update_config(all_trackers):
    "更新aria2.conf文件 bt-tracker=all_trackers"
    try:
        btTrackerIndex = -1
        cfgList = list()
        with open(aria2_config, 'r', encoding='utf8') as cfgFile:
            lines = cfgFile.readlines()
            cfgList = list(lines)
            print(cfgList)
            for line in cfgList:
                if line.startswith("bt-tracker="):  # 在.conf里遍历寻找这一行设置
                    btTrackerIndex = cfgList.index(line)
                    break
            if btTrackerIndex >= 0:
                cfgList.pop(btTrackerIndex)
                if btTrackerIndex - 1 >= 0 and cfgList[btTrackerIndex - 1].startswith("#更新于"):
                    cfgList.pop(btTrackerIndex - 1)
                    btTrackerIndex = btTrackerIndex - 1
            else:
                btTrackerIndex = 0

        cfgList.insert(btTrackerIndex, "bt-tracker="+all_trackers+"\n")  # 覆盖写入更新内容
        update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        cfgList.insert(btTrackerIndex, "#更新于"+update_time+"\n")

        with open("D:\\aria2\\aria2.conf", "w", encoding='utf8') as cfgFile:  # 存储
            cfgFile.writelines(cfgList)

        return True
    except BaseException as e:
        log(logging.ERROR, "更新aria2.conf文件【异常】{0}".format(e))
        return False


def reset_config():
    "aria2c --conf-path=D:\aria2\aria2.conf"
    try:
        os.system("aria2c.exe --conf-path=" + aria2_config)
    except BaseException as e:
        log(logging.ERROR, "重新载入配置文件【异常】{0}".format(e))
        return False


# 配置日志输出级别
logger = logging.getLogger()
logger.setLevel(logging.ERROR)

# 开始更新操作
log(logging.DEBUG, "开始更新操作【开始】")
all_trackers = getTrackers(url_trackers_best)
update_config(all_trackers)
reset_config()
log(logging.DEBUG, "开始更新操作【结束】\n")

这是更新后的aria2.conf文件的效果,我上次更新是在2020年2月18日。

### BT/PT Setting
bt-enable-lpd=true
bt-max-peers=999
follow-torrent=true
listen-port=51413
enable-dht=true
enable-dht6=false
enable-peer-exchange=true
bt-seed-unverified=true
rpc-save-upload-metadata=true
bt-hash-check-seed=true
bt-remove-unselected-file
bt-request-peer-speed-limit=512K
bt-tracker-connect-timeout=90
bt-tracker-interval=60
be-tracker-timeout=120
seed-ratio=2
seed-time=720
bt-stop-timeout=10800


#更新于2020-02-18 14:04:37
bt-tracker=http://104.**.198.**:8000/announce,…(此处省略百万字)…,udp://z0y.x:1337/announce


这样就可以提升aria2的bt下载速度啦,Considerably!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值