为aria2自动更新trackers

没错我又造轮子了,总觉得市面上的自动更新脚本写的都不够好,那我就自己上吧

之前在网上看到给 aria2 添加 bt-tracker 可以提升速度,经过一番搜索,找到 ngosang 大佬的 Github 仓库地址 https://github.com/ngosang/trackerslist

大佬每日勤勤恳恳的为广大用户免费更新 tracker list ,心里万分感动。但是他的格式不符合aria2的要求,不可能每次更新都手动删除那两个换行符然后改成逗号吧,那也太傻比了。所以花了半个小时写了个脚本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""
@author: pakro
@software: pycharm
@file: aria2_update_trackers.py
@time: 2019/9/4 0004 14:19
@desc: 为当前路径下的aria2配置文件更新trackers
"""
import os
import sys
import time
import getopt
import requests


def usage():
print("-h help\n-p or --path custom path\n-f or --file_name config file name\n-t or --tracker_url tracker url")


def download_tracker(
trackers_best_url="https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt"):
try:
trackers_best = "bt-tracker=" + requests.get(trackers_best_url).text.replace("\n\n", ",")[:-1]
return trackers_best
except Exception as e:
print("网络错误! ", e)
sys.exit(0)


def read_local_file(config_file_name="aria2.conf", trackers_url=None):
if not os.path.exists(config_file_name):
return False
try:
with open(config_file_name, "r", encoding="UTF-8") as f:
data = list([])
for line in f.readlines():
if "bt-tracker=" not in line:
data.append(line)

try:
with open(config_file_name, "w", encoding="UTF-8") as f:
for each in data:
f.write(each)
if trackers_url:
f.write(download_tracker(trackers_url))
else:
f.write(download_tracker())
except Exception as e:
print("读取都没问题, 写入居然出了问题, 请重新试试. 错误信息: ", e)
return False

print("success!")
time.sleep(2)
except Exception as e:
print("似乎读取文件出了问题? 看看是否有其他程序正在占用这个文件. 错误信息: ", e)
return False


def main(argv):
config_file_name = None
trackers_url = None
path = None
try:
opts, args = getopt.getopt(argv, "hf:t:p:", ["file_name=", "tracker_url=", "path="])
except getopt.GetoptError:
print("不知道出了什么问题")
sys.exit(2)

for opt, arg in opts:
if opt in "-h":
usage()
sys.exit(0)
if opt in ("-f", "--file_name"):
config_file_name = arg
if opt in ("-t", "--tracker_url"):
trackers_url = arg
if opt in ("-p", "--path"):
path = arg

try:
if path:
print("输入了自定义路径: ", path)
if path[:-1] != "/" or path[:-1] != "\\":
config_file_name = path + "/" + config_file_name
else:
config_file_name = path + config_file_name
if config_file_name and trackers_url:
read_local_file(config_file_name, trackers_url)
elif config_file_name:
read_local_file(config_file_name)
elif trackers_url:
read_local_file(trackers_url=trackers_url)
else:
read_local_file()
except SystemExit:
print("网络都没有, 怎么运行?")
time.sleep(2)


if __name__ == '__main__':
main(sys.argv[1:])

用法其实很简单

参数 作用
-h 帮助
-p or –path 自定义设置文件路径,默认是当前执行路径
-f or –file_name 自定义配置文件名,默认是 aria2.conf
-t or –tracker_url 自定义tracker地址, 必须是ngosang大佬的格式

用法举例:

1
aria2_update_trackers.py -p /home/linux/aria2 -f aria2-backup.conf

这样就可以挂在 Linux 上做自动化了

项目开源在这里