惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
美团技术团队
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
WordPress大学
WordPress大学
月光博客
月光博客
宝玉的分享
宝玉的分享
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans
J
Java Code Geeks
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
Vercel News
Vercel News
博客园 - 聂微东

小猪的窝

多手 逛论坛 死期 冲突 确认损失 反思牛磺酸 诚实 中秋将至 电击上瘾 破案 Ghost in the Shell 选刀误砍柴 折腾 超支 吵吵嚷嚷 开学日 月志202608 心急 入手 CUKTECH 10Ultra 充电头 老花 斗气车 撞鬼 眼光 眼神 痛风 微醺 理想四旬 一期一会 出光机油粗测 主题
批量提交BT种子到Aria2下载
· 2025-01-12 · via 小猪的窝

概况

家里的NAS是我主要文件存储系统,Aria2作为日常常用下载工具也一同部署在上面。

该Aria2以docker容器的形式部署,这样可以带来更好的独立性以便于日常维护。由于众所周知的原因,尝试过一段时间通过openwrt对其进行留学分流,但效果不是太理想。总会发生偶发性的偷跑流量情况,且会损失部分下载速度。为避免钱袋受到伤害,最终还是把Aria2设置为直连方式下载。

aria2NG

直连方式也带来一个问题:部分种子在墙外被限制直连,若直接将链接地址发送到Aria2,是无法被正确下载的。每次遇到这种情况,我只能先通过浏览器把种子文件下载下来后,再通过Aria2NG发种子发送到Aria2进行下载,显得比较繁琐。Aria2NG也有一个问题:每次只能上传一个种子。如果有多个种子文件,操作就显得相当机械繁琐而无趣。我想,能不能一步到位直接把下载好的种子文件统一推送到Aria2自行下载呢?搜索了一下,果然有老哥为此建了个项目,该项目是通过xmlrpc的方式管理Aria2,但缺少密钥验证功能。因此我拿该大佬的项目改了一下,以支持密钥验证方式。有需要的朋友可以直接抄下作业。

实现我需求的思路主要是两个部分,第一部分是脚本处理,第二部分是系统快捷键的部署(我的是MacOS,win部分可能需要一些系统快捷键软件支持才能实现)。

脚本

import xmlrpc.client
import xmlrpc
import os
import argparse

def handle(s, btFile, token):
    print('handle bittorrent file: ', str(btFile))
    token = "token:" + token
    ret=s.aria2.addTorrent(token,xmlrpc.client.Binary(open(btFile, mode='rb').read()),[],{'pause':'true'})
    print("add bt: ",str(ret))
    waiting = s.aria2.tellWaiting(token,0, 1000,
                              ["gid", "totalLength", "completedLength", "uploadSpeed", "downloadSpeed", "connections",
                               "numSeeders", "seeder", "status", "errorCode", "verifiedLength",
                               "verifyIntegrityPending", "files", "bittorrent", "infoHash"])
    for w in waiting:
        gid=w['gid']
        if gid!=ret:
            continue
        #print(w['gid'],w['files'])
        # max-selection strategy
        maxLen=0
        maxFPath=''
        maxFIndex='0'
        for f in w['files']:
            #print(f['length'],f['path'])
            if int(f['length'])>maxLen:
                maxLen=int(f['length'])
                maxFPath=f['path']
                maxFIndex=f['index']
        print('max file: ',str(maxLen),maxFIndex,str(maxFPath))
        # max-selection strategy end
        cret=s.aria2.changeOption(token,gid,{'select-file':maxFIndex})# select multiple files example: 'select-file':'5,6,7,8'
        print('select file: ',cret)
        tret=s.aria2.tellStatus(token,gid)
        print('after selection: ', tret['files'][int(maxFIndex)-1])
        uret=s.aria2.unpause(token,gid)
        print('unpause: ',uret)
    print('over: ',str(btFile))



if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.description = 'bt批量导入aria2,并选择文件大小最大的文件进行下载'
    parser.add_argument("server", help="like: http://192.168.3.99:6800", type=str)
    parser.add_argument("token", help="验证密钥", type=str)
    parser.add_argument("dir", help="存放种子目录", type=str)
    args = parser.parse_args()
    s = xmlrpc.client.ServerProxy(args.server+"/rpc")
    flist=os.listdir(args.dir)
    for i in range(0, len(flist)):
        if flist[i].endswith(".torrent"):
            btFile = os.path.join(args.dir, flist[i])
            if os.path.isfile(btFile):
                handle(s,btFile, args.token)
    print("Done")

使用方法

把上述代码保存为bb2a.py,需要使用Python3运行。

帮助(Help):

python3 bb2a.py --help

启动(Start to Add):

python3 bb2a.py <server> <token> <bt-dir>

参数(parameters):

server      如(like): http://192.168.3.99:6800
token       验证密钥:设置在Aria2配置文件的密钥
bt-dir      bt文件的目录(the dir of your bittorrents)

例子(example):

python3 /path/to/bb2a.py http://192.168.1.100:6800 123456 /home/root/bts/

MacOS增加快捷指令

command + 空格,在快捷运行窗口输入“快捷指令”打开系统的快捷指令模块。点击窗口右上角➕新增一个快捷指令。
新增快速指令1.png

按照下图方式创建快捷指令内容
新增快速指令2.png

参考shell内容如下,请按照自己配置情况修改相关参数。sleep 3是暂停3秒。后面的rm命令是顺便删除下载目录中的bt种子文件。

python3 /Users/ccchen/autobt/bb2a.py http://192.168.12.5:6800 123456 /Volumes/FAT12T/down2025/
sleep 3
rm -f  /Volumes/FAT12T/down2025/*.torrent

按窗口右上角的详情按钮,设置快捷指令的允许执行shell权限。
新增快速指令3.png

最后给快捷指令设置快捷键.我设置的快捷键是“Ctrl + Option + W”这个组合键。
新增快速指令4.png

完结

到此,所有配置已完成。只要下载bt种子到对应的目录。完毕后按快捷键“Ctrl + Option + W”,脚本便自动将该目录中所有种子文件发送到aria2进行下载。同时会把下载目录中对应的种子清理掉。