Skip to content

Commit

Permalink
Custom Torrent/Magnet download
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanliang committed Dec 6, 2020
1 parent 4cc6fd7 commit f989a9a
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 87 deletions.
115 changes: 108 additions & 7 deletions Aria2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,50 @@ import (
)

var aria2Rpc rpc.Client
var aria2Set Aria2DownloadMode

// TMMessageChan is set Torrent/Magnet download mode
var TMMessageChan = make(chan string, 3)

// TMAllowDownloads is Allow Torrent/Magnet download List
var TMAllowDownloads = make(map[string]int, 0)

func testTMStop() {
for {
gid := <-TMMessageChan
//log.Println(a)
if _, have := TMAllowDownloads[gid]; !have {
dlInfo, err := aria2Rpc.TellStatus(gid)
dropErr(err)
//log.Printf("%+v\n", dlInfo)
if dlInfo.BitTorrent.Info.Name != "" && aria2Set.TMMode != "3" {
aria2Rpc.Pause(gid)
TMSelectMessageChan <- gid
}
} else {
delete(TMAllowDownloads, gid)
}

}

}
func aria2Load() {
var err error
aria2Rpc, err = rpc.New(context.Background(), info.Aria2Server, info.Aria2Key, time.Second*10, &Aria2Notifier{})
dropErr(err)
aria2Set = Aria2DownloadMode{
TMMode: "1", // 记得修改
}
log.Printf(locText("connectTo"), info.Aria2Server)
version, err := aria2Rpc.GetVersion()
dropErr(err)
log.Printf(locText("connectSuccess"), version.Version)
go testTMStop()
}

func formatTellSomething(info []rpc.StatusInfo, err error) string {
dropErr(err)
res := ""
log.Printf("%+v\n", info)
var statusFlag = map[string]string{"active": locText("active"), "paused": locText("paused"), "complete": locText("complete"), "removed": locText("removed")}
for index, Files := range info {
if Files.BitTorrent.Info.Name != "" {
Expand Down Expand Up @@ -90,23 +119,31 @@ func formatGidAndName(info []rpc.StatusInfo, err error) []map[string]string {
dropErr(err)

m := make([]map[string]string, 0)
log.Printf("%+v\n", info)
//log.Printf("%+v\n", info)
for _, Files := range info {
for _, File := range Files.Files {
// log.Println(Files.BitTorrent.Info.Name, Files.BitTorrent.Info.Name != "")
if Files.BitTorrent.Info.Name != "" {
ms := make(map[string]string)
//paths, fileName := filepath.Split(files)
ms["GID"] = Files.Gid
countSplit := strings.Split(File.Path, "/")
ms["Name"] = countSplit[len(countSplit)-1]
ms["Name"] = Files.BitTorrent.Info.Name
m = append(m, ms)
} else {
for _, File := range Files.Files {
ms := make(map[string]string)
//paths, fileName := filepath.Split(files)
ms["GID"] = Files.Gid
countSplit := strings.Split(File.Path, "/")
ms["Name"] = countSplit[len(countSplit)-1]
m = append(m, ms)
}
}
}
return m
}

func tellName(info rpc.StatusInfo, err error) string {
dropErr(err)
log.Printf("%+v\n", info)
//log.Printf("%+v\n", info)
Name := ""
if info.BitTorrent.Info.Name != "" {
Name = info.BitTorrent.Info.Name
Expand All @@ -122,6 +159,7 @@ func tellName(info rpc.StatusInfo, err error) string {
}
return Name
}

func download(uri string) bool {
uriType := isDownloadType(uri)
if uriType == 0 {
Expand All @@ -139,3 +177,66 @@ func download(uri string) bool {
}
return true
}

func formatTMFiles(gid string) [][]string {
var fileList [][]string
rawList, err := aria2Rpc.GetFiles(gid)
dropErr(err)
// log.Printf("%+v", rawList)
for _, file := range rawList {
fileInfo := make([]string, 0)
fileInfo = append(fileInfo, file.Path)
bytes, err := strconv.ParseFloat(file.Length, 64)
dropErr(err)
fileInfo = append(fileInfo, byte2Readable(bytes))
fileInfo = append(fileInfo, file.Selected)
fileList = append(fileList, fileInfo)
}
return fileList
}

func setTMDownloadFilesAndStart(gid string, FilesList [][]string) {
selectFile := ""
for i, file := range FilesList {
if file[2] == "true" {
selectFile += fmt.Sprint(i+1) + ","
}
}
aria2Rpc.ChangeOption(gid, rpc.Option{
"select-file": selectFile[:len(selectFile)-1],
})
TMAllowDownloads[gid] = 0
aria2Rpc.Unpause(gid)

}
func selectBigestFile(gid string) int {
index := 0
rawList, err := aria2Rpc.GetFiles(gid)
dropErr(err)
for i := 0; i < len(rawList); i++ {
if rawList[i].Length > rawList[index].Length {
index = i
}
}
return index
}
func selectBigFiles(gid string) []int {
indexs := make([]int, 0)
rawList, err := aria2Rpc.GetFiles(gid)
dropErr(err)
totalSize, avgSize := 0, 0.0
for _, file := range rawList {
totalSize += toInt(file.Length)
}
avgSize = float64(totalSize) / float64(len(rawList))
avgSize -= avgSize * 0.2

for i := 0; i < len(rawList); i++ {
dist, err := strconv.ParseFloat(rawList[i].Length, 64)
dropErr(err)
if dist > avgSize {
indexs = append(indexs, i)
}
}
return indexs
}
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- [ ] qbittorrent control
- [ ] Multi download server control at the same time

#### The robot protocol supports
#### The Bot protocol supports
- [x] Telegram Bot
- [ ] DingTalk Bot

Expand All @@ -23,14 +23,15 @@
- [x] Download HTTP/FTP link
- [x] Download Magnet link
- [x] Download the files in the BT file
- [ ] Custom Torrent/Magnet download
- [ ] Do not download files smaller than the specified size
- [x] Custom Torrent/Magnet download
- [x] Download only the largest files
- [x] Do not download files smaller than the specified size
- [ ] Download Torrent/Magnet according to the size of storage space
- [ ] Do not download files that exceed storage space
- [ ] Download the files in Torrent/Magnet several times according to the storage space
- [ ] Upload a file
- [ ] Upload the file to OneDrive when the download is complete
- [ ] Upload a file to Google Drive when the download is complete
- [ ] Upload the file to Google Drive when the download is complete
- [ ] Upload the file to Mega when the download is complete
- [ ] Upload the file to 189Cloud when the download is complete
- [x] Additional features
Expand Down Expand Up @@ -81,7 +82,7 @@
```
#### Corresponding explanations
* aria2-server : Aria2 server address. Websocket connection is used by default. If you want to use websocket to connect to aria2, be sure to set `enable-rpc=true` in `aria2.conf`. If not necessary, please try to **set the local aria2 address**, in order to maximize the use of this program
* aria2-key : The value of `rpc-secret` in aria2.conf
* aria2-key : The value of `rpc-secret` in `aria2.conf`
* bot-key : ID of telegram BOT
* user-id : The ID of the administrator
* max-index:Maximum display quantity of download information, 10 pieces are recommended (to be improved in the future)
Expand All @@ -99,6 +100,6 @@
When you fill in the above language tag in `config.json`, the program will automatically download the language pack

#### About user-id
If you don't know your `user-id`, you can leave this field blank and enter `/myid` after running the robot, and the robot will return your `user-id`
If you don't know your `user-id`, you can leave this field blank and enter `/myid` after running the Bot, and the Bot will return your `user-id`


7 changes: 4 additions & 3 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
- [x] 下载 HTTP/FTP 链接
- [x] 下载 Magnet 链接
- [x] 下载 BT 文件内的文件
- [ ] 自定义的 Torrent/Magnet 下载
- [ ] 不下载小于指定大小的文件
- [x] 智能 Torrent/Magnet 下载方式
- [x] 只下载最大的文件
- [x] 不下载小于指定大小的文件
- [ ] 自适应环境存储空间的 Torrent/Magnet 下载
- [ ] 不下载超过存储空间的文件
- [ ] 根据存储空间分块多次下载 Torrent/Magnet 内的文件
Expand Down Expand Up @@ -82,7 +83,7 @@
```
#### 各项对应解释
* aria2-server:aria2服务器地址,默认使用websocket连接。如果要使用websocket连接aria2,请务必设置`aria2.conf`内的`enable-rpc=true`。如果不是必须,请尽量设置本地的aria2地址,以便于最大化的使用本程序
* aria2-key:aria2.conf中rpc-secret的值
* aria2-key:`aria2.conf``rpc-secret`的值
* bot-key:Telegram Bot的标识
* user-id:管理员的ID
* max-index:下载信息最大显示数量,建议10条(以后会改进)
Expand Down
7 changes: 4 additions & 3 deletions README_zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
- [x] 下載 HTTP/FTP 連結
- [x] 下載 Magnet 連結
- [x] 下載 BT 文件內的文件
- [ ] 自訂的 Torrent/Magnet 下載
- [ ] 不下載小於指定大小的檔
- [x] 智慧 Torrent/Magnet 下載方式
- [x] 只下載最大的文件
- [x] 不下載小於指定大小的檔
- [ ] 自我調整環境存儲空間的 Torrent/Magnet 下載
- [ ] 不下載超過存儲空間的檔
- [ ] 根據存儲空間分塊多次下載 Torrent/Magnet 內的檔
Expand Down Expand Up @@ -80,7 +81,7 @@
```
#### 各項對應解釋
* aria2-server:aria2伺服器位址,默認使用websocket連接。如果要使用websocket連接aria2,請務必設置`aria2.conf`內的`enable-rpc=true`。如果不是必須,請儘量設置本地的aria2位址,以便於最大化的使用本程式
* aria2-key:aria2.conf中rpc-secret的值
* aria2-key:`aria2.conf``rpc-secret`的值
* bot-key:Telegram Bot的標識
* user-id:管理員的ID
* max-index:下載資訊最大顯示數量,建議10條(以後會改進)
Expand Down
Loading

0 comments on commit f989a9a

Please sign in to comment.