Rewrite handle trackers function. Respect trackers tier

Expect for different types in resumeItem trackers.
New recursion function for dig interface for strings in helpers

https://github.com/rumanzo/bt2qbt/issues/36
This commit is contained in:
rumanzo 2022-04-09 00:09:33 +03:00
parent ef8fd5825a
commit b66b5f8e75
7 changed files with 110 additions and 41 deletions

View File

@ -46,7 +46,7 @@ func main() {
resumeItems := map[string]*utorrentStructs.ResumeItem{} resumeItems := map[string]*utorrentStructs.ResumeItem{}
err = bencode.DecodeBytes(b, &resumeItems) err = bencode.DecodeBytes(b, &resumeItems)
if err != nil { if err != nil {
log.Println("Can't process convert resume.dat") log.Printf("Can't convert resume.dat. Err: %v\n", err)
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
os.Exit(1) os.Exit(1)
} }

View File

@ -29,7 +29,7 @@ func (transfer *TransferStructure) HandleStructures() {
transfer.HandleTags() transfer.HandleTags()
transfer.HandleLabels() transfer.HandleLabels()
transfer.GetTrackers(transfer.ResumeItem.Trackers) transfer.HandleTrackers()
transfer.HandlePriority() // important handle priorities before handling pieces transfer.HandlePriority() // important handle priorities before handling pieces
/* /*

View File

@ -6,11 +6,13 @@ import (
"github.com/rumanzo/bt2qbt/internal/options" "github.com/rumanzo/bt2qbt/internal/options"
"github.com/rumanzo/bt2qbt/internal/replace" "github.com/rumanzo/bt2qbt/internal/replace"
"github.com/rumanzo/bt2qbt/pkg/fileHelpers" "github.com/rumanzo/bt2qbt/pkg/fileHelpers"
"github.com/rumanzo/bt2qbt/pkg/helpers"
"github.com/rumanzo/bt2qbt/pkg/qBittorrentStructures" "github.com/rumanzo/bt2qbt/pkg/qBittorrentStructures"
"github.com/rumanzo/bt2qbt/pkg/torrentStructures" "github.com/rumanzo/bt2qbt/pkg/torrentStructures"
"github.com/rumanzo/bt2qbt/pkg/utorrentStructs" "github.com/rumanzo/bt2qbt/pkg/utorrentStructs"
"github.com/zeebo/bencode" "github.com/zeebo/bencode"
"io" "io"
"regexp"
"strings" "strings"
"time" "time"
) )
@ -141,24 +143,30 @@ func (transfer *TransferStructure) HandleLabels() {
} }
} }
// GetTrackers recurstive function for searching trackers in resume item trackers var localTracker = regexp.MustCompile(`(http|udp)://\S+\.local\S*`)
func (transfer *TransferStructure) GetTrackers(trackers interface{}) {
switch strct := trackers.(type) { func (transfer *TransferStructure) HandleTrackers() {
case []string: trackers := helpers.GetStrings(transfer.ResumeItem.Trackers)
for _, str := range strct { trackersMap := map[string][]string{}
for _, substr := range strings.Fields(str) { var index string
transfer.Fastresume.Trackers = append(transfer.Fastresume.Trackers, []string{substr}) for _, tracker := range trackers {
} if localTracker.MatchString(tracker) {
index = "local"
} else {
index = "main"
} }
case string: if _, ok := trackersMap[index]; ok {
for _, substr := range strings.Fields(strct) { trackersMap[index] = append(trackersMap[index], tracker)
transfer.Fastresume.Trackers = append(transfer.Fastresume.Trackers, []string{substr}) } else {
} trackersMap[index] = []string{tracker}
case []interface{}:
for _, st := range strct {
transfer.GetTrackers(st)
} }
} }
if val, ok := trackersMap["main"]; ok {
transfer.Fastresume.Trackers = append(transfer.Fastresume.Trackers, val)
}
if val, ok := trackersMap["local"]; ok {
transfer.Fastresume.Trackers = append(transfer.Fastresume.Trackers, val)
}
} }
func (transfer *TransferStructure) HandlePriority() { func (transfer *TransferStructure) HandlePriority() {

View File

@ -1124,36 +1124,50 @@ func TestTransferStructure_HandlePriority(t *testing.T) {
} }
} }
func TestTransferStructure_GetTrackers(t *testing.T) { func TestTransferStructure_HandleTrackers(t *testing.T) {
transferStructure := TransferStructure{ transferStructure := TransferStructure{
Fastresume: &qBittorrentStructures.QBittorrentFastresume{}, Fastresume: &qBittorrentStructures.QBittorrentFastresume{},
} ResumeItem: &utorrentStructs.ResumeItem{
testTrackers := []interface{}{ Trackers: []interface{}{
"test1", "http://test1.org",
"test2", "udp://test1.org",
[]interface{}{ "http://test1.local",
"test3", "udp://test1.local",
"test4", []interface{}{
[]interface{}{ "http://test2.org:80",
"test5", "udp://test2.org:8080",
"test6", "http://test2.local:80",
"udp://test2.local:8080",
[]interface{}{
"http://test3.org:80/somepath",
"udp://test3.org:8080/somepath",
"http://test3.local:80/somepath",
"udp://test3.local:8080/somepath",
},
},
[]interface{}{
[]interface{}{
"http://test4.org:80/",
"udp://test4.org:8080/",
"http://test4.local:80/",
"udp://test4.local:8080/",
},
},
}, },
}, },
[]interface{}{
[]interface{}{"test7", "test8"},
},
} }
expect := [][]string{ expect := [][]string{
[]string{"test1"}, []string{
[]string{"test2"}, "http://test1.org", "udp://test1.org",
[]string{"test3"}, "http://test2.org:80", "udp://test2.org:8080",
[]string{"test4"}, "http://test3.org:80/somepath", "udp://test3.org:8080/somepath",
[]string{"test5"}, "http://test4.org:80/", "udp://test4.org:8080/"},
[]string{"test6"}, []string{"http://test1.local", "udp://test1.local",
[]string{"test7"}, "http://test2.local:80", "udp://test2.local:8080",
[]string{"test8"}, "http://test3.local:80/somepath", "udp://test3.local:8080/somepath",
"http://test4.local:80/", "udp://test4.local:8080/"},
} }
transferStructure.GetTrackers(testTrackers) transferStructure.HandleTrackers()
if !reflect.DeepEqual(transferStructure.Fastresume.Trackers, expect) { if !reflect.DeepEqual(transferStructure.Fastresume.Trackers, expect) {
t.Fatalf("Unexpected error: opts isn't equal:\n Got: %#v\n Expect %#v\n", transferStructure.Fastresume.Trackers, expect) t.Fatalf("Unexpected error: opts isn't equal:\n Got: %#v\n Expect %#v\n", transferStructure.Fastresume.Trackers, expect)
} }

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"strconv" "strconv"
"strings"
) )
func ASCIIConvert(s string) string { func ASCIIConvert(s string) string {
@ -93,3 +94,20 @@ func CopyFile(src string, dst string) error {
} }
return nil return nil
} }
func GetStrings(trackers interface{}) []string {
ntrackers := []string{}
switch strct := trackers.(type) {
case []string:
for _, str := range strct {
ntrackers = append(ntrackers, strings.Fields(str)...)
}
case string:
ntrackers = append(ntrackers, strings.Fields(strct)...)
case []interface{}:
for _, st := range strct {
ntrackers = append(ntrackers, GetStrings(st)...)
}
}
return ntrackers
}

View File

@ -0,0 +1,29 @@
package helpers
import (
"reflect"
"testing"
)
func TestGetStrings(t *testing.T) {
testTrackers := []interface{}{
"test1",
"test2",
[]interface{}{
"test3",
"test4",
[]interface{}{
"test5",
"test6",
},
},
[]interface{}{
[]interface{}{"test7", "test8"},
},
}
expect := []string{"test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"}
trackers := GetStrings(testTrackers)
if !reflect.DeepEqual(trackers, expect) {
t.Fatalf("Unexpected error: opts isn't equal:\n Got: %#v\n Expect %#v\n", trackers, expect)
}
}

View File

@ -15,7 +15,7 @@ type ResumeItem struct {
Started int64 `bencode:"started"` Started int64 `bencode:"started"`
Targets [][]interface{} `bencode:"targets,omitempty"` Targets [][]interface{} `bencode:"targets,omitempty"`
Time int64 `bencode:"time"` Time int64 `bencode:"time"`
Trackers []string `bencode:"trackers,omitempty"` Trackers interface{} `bencode:"trackers,omitempty"`
UpSpeed int64 `bencode:"upspeed"` UpSpeed int64 `bencode:"upspeed"`
Uploaded int64 `bencode:"uploaded"` Uploaded int64 `bencode:"uploaded"`
} }