-
Notifications
You must be signed in to change notification settings - Fork 3
/
qbtchangetracker.go
149 lines (141 loc) · 4.77 KB
/
qbtchangetracker.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bufio"
"fmt"
"github.com/fatih/color"
goflags "github.com/jessevdk/go-flags"
"github.com/rumanzo/qbtchangetracker/path"
"github.com/rumanzo/qbtchangetracker/tracker"
"log"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
type Flags struct {
QBitDir string `short:"d" long:"directory" description:"Destination directory BT_backup (as default)"`
OldTracker string `short:"o" long:"oldtracker" description:"Old tracker"`
NewTracker string `short:"n" long:"newtracker" description:"New tracker"`
Replaces []string `short:"r" long:"replace" description:"Replace paths.\n Delimiter for from/to is comma - ,\n Example: -r \"D:\\films,/home/user/films\" -r \"D:\\music,/home/user/music\"\n"`
PathSeparator string `long:"sep" description:"Default path separator that will use in all paths. You may need use this flag if you migrating from windows to linux in some cases"`
Version bool `short:"v" long:"version" description:"Show version"`
}
var version, commit, buildImage string
func main() {
var wg sync.WaitGroup
flags := Flags{PathSeparator: string(os.PathSeparator)}
sep := string(os.PathSeparator)
switch OS := runtime.GOOS; OS {
case "windows":
flags.QBitDir = os.Getenv("LOCALAPPDATA") + sep + "qBittorrent" + sep + "BT_backup" + sep
flags.QBitDir = strings.Join([]string{os.Getenv("LOCALAPPDATA"), "qBittorrent", "BT_backup"}, sep)
case "darwin":
usr, err := user.Current()
if err != nil {
panic(err)
}
flags.QBitDir = strings.Join([]string{usr.HomeDir, "Library", "Application Support", "QBittorrent", "BT_backup"}, sep)
case "linux":
usr, err := user.Current()
if err != nil {
panic(err)
}
flags.QBitDir = strings.Join([]string{usr.HomeDir, ".local", "share", "data", "qBittorrent", "BT_backup"}, sep)
}
parser := goflags.NewParser(&flags, goflags.Default)
if _, err := parser.Parse(); err != nil { // https://godoc.org/github.com/jessevdk/go-flags#ErrorType
if flagsErr, ok := err.(*goflags.Error); ok && flagsErr.Type == goflags.ErrHelp {
os.Exit(0)
} else {
log.Println(err)
time.Sleep(30 * time.Second)
os.Exit(1)
}
}
if flags.Version {
fmt.Printf("Version: %v\nCommit: %v\nGolang version: %v\nBuild image: %v\n", version, commit, runtime.Version(), buildImage)
os.Exit(0)
}
sepdefined := parser.FindOptionByLongName("sep")
if flags.QBitDir[len(flags.QBitDir)-1] != os.PathSeparator {
flags.QBitDir += string(os.PathSeparator)
}
if _, err := os.Stat(flags.QBitDir); os.IsNotExist(err) {
log.Println(err)
time.Sleep(30 * time.Second)
os.Exit(1)
}
files, _ := filepath.Glob(flags.QBitDir + "*fastresume")
comChannel := make(chan string, len(files)*2)
errChannel := make(chan error, len(files)*2)
var replacepatterns []path.Replace
if len(flags.Replaces) != 0 || sepdefined.IsSet() == true {
for _, str := range flags.Replaces {
patterns := strings.Split(str, ",")
if len(patterns) < 2 {
continue
}
replacepatterns = append(replacepatterns, path.Replace{From: patterns[0], To: patterns[1]})
}
} else {
if flags.OldTracker == "" || flags.NewTracker == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter old tracker: ")
flags.OldTracker, _ = reader.ReadString('\n')
flags.OldTracker = strings.TrimSuffix(flags.OldTracker, "\n")
fmt.Print("Enter new tracker: ")
flags.NewTracker, _ = reader.ReadString('\n')
flags.NewTracker = strings.TrimSuffix(flags.NewTracker, "\n")
}
}
color.HiRed("Check that the qBittorrent is turned off and the directory %v and is backed up.\n\n",
flags.QBitDir)
fmt.Println("Press Enter to start")
fmt.Scanln()
log.Println("Started")
if len(flags.Replaces) != 0 || sepdefined.IsSet() == true {
for _, frfile := range files {
trfile := strings.TrimSuffix(frfile, filepath.Ext(frfile)) + ".torrent"
if _, err := os.Stat(trfile); os.IsNotExist(err) {
continue
}
go path.PathReplace(&replacepatterns, trfile, frfile, flags.PathSeparator, &wg, comChannel, errChannel)
wg.Add(1)
}
wg.Wait()
}
if flags.OldTracker != "" || flags.NewTracker != "" {
for _, frfile := range files {
trfile := strings.TrimSuffix(frfile, filepath.Ext(frfile)) + ".torrent"
if _, err := os.Stat(trfile); os.IsNotExist(err) {
continue
}
go tracker.ChangeTracker(&flags.OldTracker, &flags.NewTracker, trfile, frfile, &wg, comChannel, errChannel)
wg.Add(1)
}
}
waserrors := false
numjob := 1
go func() {
wg.Wait()
close(comChannel)
close(errChannel)
}()
for message := range comChannel {
fmt.Printf("%-5v %v \n", numjob, message)
numjob++
}
for message := range errChannel {
color.HiRed(fmt.Sprintf("%-5v %v \n", numjob, message))
waserrors = true
numjob++
}
if waserrors {
log.Println("Not all torrents was processed")
}
fmt.Print("Press Enter to exit")
fmt.Scanln()
}