Fixed export\import tags

This commit is contained in:
akostin 2018-04-12 17:10:22 +03:00
parent fe127ab7ba
commit 9b8871dfe1

View File

@ -6,6 +6,7 @@ import (
"encoding/hex"
"fmt"
"github.com/fatih/color"
"github.com/go-ini/ini"
"github.com/zeebo/bencode"
"io"
"io/ioutil"
@ -16,21 +17,20 @@ import (
"strconv"
"strings"
"time"
"github.com/go-ini/ini"
)
func ASCIIconvert(s string) (newstring string) {
for _, c := range s {
if c > 127 {
newstring = fmt.Sprintf("%v\\x%x",newstring, c)
newstring = fmt.Sprintf("%v\\x%x", newstring, c)
} else {
newstring = fmt.Sprintf("%v%v",newstring, string(c))
newstring = fmt.Sprintf("%v%v", newstring, string(c))
}
}
return
}
func checknotexists(s string, tags []string ) bool {
func checknotexists(s string, tags []string) bool {
for _, value := range tags {
if value == s {
return false
@ -156,7 +156,7 @@ func copyfile(src string, dst string) error {
return nil
}
func logic(key string, value map[string]interface{}, bitdir *string, with_label *bool, with_tags *bool, qbitdir *string, comChannel chan string, position int, tags chan string) error {
func logic(key string, value map[string]interface{}, bitdir *string, with_label *bool, with_tags *bool, qbitdir *string, comChannel chan string, position int) error {
newstructure := map[string]interface{}{"active_time": 0, "added_time": 0, "announce_to_dht": 0,
"announce_to_lsd": 0, "announce_to_trackers": 0, "auto_managed": 0,
"banned_peers": new(string), "banned_peers6": new(string), "blocks per piece": 0,
@ -329,23 +329,20 @@ func logic(key string, value map[string]interface{}, bitdir *string, with_label
comChannel <- fmt.Sprintf("Can't create qBittorrent torrent file %v", *qbitdir+newbasename+".torrent")
return err
}
if *with_tags == true {
for _, label := range newstructure["qBt-tags"].([]interface{}){
tags <- fmt.Sprintf("%v", ASCIIconvert(label.(string)))
}
}
comChannel <- fmt.Sprintf("Sucessfully imported %v", key)
return nil
}
func main() {
var bitdir, qbitdir string
var bitdir, qbitdir, config string
var with_label, with_tags bool = true, true
var without_label, without_tags bool
gnuflag.StringVar(&bitdir, "source", (os.Getenv("APPDATA") + "\\Bittorrents\\"), "Source directory that contains resume.dat and torrents files")
gnuflag.StringVar(&bitdir, "s", (os.Getenv("APPDATA") + "\\Bittorrent\\"), "Source directory that contains resume.dat and torrents files")
gnuflag.StringVar(&bitdir, "source", (os.Getenv("APPDATA") + "\\uTorrent\\"), "Source directory that contains resume.dat and torrents files")
gnuflag.StringVar(&bitdir, "s", (os.Getenv("APPDATA") + "\\uTorrent\\"), "Source directory that contains resume.dat and torrents files")
gnuflag.StringVar(&qbitdir, "destination", (os.Getenv("LOCALAPPDATA") + "\\qBittorrent\\BT_backup\\"), "Destination directory BT_backup (as default)")
gnuflag.StringVar(&qbitdir, "d", (os.Getenv("LOCALAPPDATA") + "\\qBittorrent\\BT_backup\\"), "Destination directory BT_backup (as default)")
gnuflag.StringVar(&config, "qconfig", (os.Getenv("APPDATA") + "\\qBittorrent\\qBittorrent.ini"), "qBittorrent config files (for write tags)")
gnuflag.StringVar(&config, "c", (os.Getenv("APPDATA") + "\\qBittorrent\\qBittorrent.ini"), "qBittorrent config files (for write tags)")
gnuflag.BoolVar(&without_label, "without-labels", false, "Do not export/import labels")
gnuflag.BoolVar(&without_tags, "without-tags", false, "Do not export/import tags")
gnuflag.Parse(true)
@ -386,20 +383,13 @@ func main() {
time.Sleep(30 * time.Second)
os.Exit(1)
}
//var oldtags []string
var newtags []string
cfg, err := ini.Load(os.Getenv("APPDATA") + "\\qBittorrent\\qBittorrent.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
if with_tags == true {
if _, err := os.Stat(config); os.IsNotExist(err) {
fmt.Println("Can not read qBittorrent config file. Try run and close qBittorrent if you have not done so already, or specify the path explicitly or do not import tags")
time.Sleep(30 * time.Second)
os.Exit(1)
}
}
dtags := cfg.Section("BitTorrent").Key("Session\\Tags").String()
dtags += ", фиииильм"
fmt.Println(dtags)
//fmt.Printf("%+x", "ф")
fmt.Println(ASCIIconvert("testфииилмь11111"))
//os.Exit(0)
color.Green("It will be performed processing from directory %v to directory %v\n", bitdir, qbitdir)
color.HiRed("Check that the qBittorrent is turned off and the directory %v is backed up.\n\n", qbitdir)
fmt.Println("Press Enter to start")
@ -407,28 +397,64 @@ func main() {
fmt.Println("Started")
totaljobs := len(resumefile) - 2
numjob := 1
var oldtags string
var newtags []string
comChannel := make(chan string, totaljobs)
tags := make(chan string, 1000000)
for key, value := range resumefile {
if key != ".fileguard" && key != "rec" {
go logic(key, value.(map[string]interface{}), &bitdir, &with_label, &with_tags, &qbitdir, comChannel, totaljobs, tags)
if with_tags == true {
for _, label := range value.(map[string]interface{})["labels"].([]interface{}) {
if len(label.(string)) > 0 {
if checknotexists(label.(string), newtags) {
newtags = append(newtags, ASCIIconvert(label.(string)))
}
}
}
}
go logic(key, value.(map[string]interface{}), &bitdir, &with_label, &with_tags, &qbitdir, comChannel, totaljobs)
}
}
for message := range comChannel {
fmt.Printf("%v/%v %v \n", numjob, totaljobs, message)
numjob++
if numjob-1 == totaljobs {
close(tags)
break
}
}
for label := range tags{
if checknotexists(label, newtags) {
newtags = append(newtags, label)
if with_tags == true {
cfg, err := ini.Load(config)
ini.PrettyFormat = false
ini.PrettySection = false
if err != nil {
fmt.Println("Can not read qBittorrent config file. Try to specify the path explicitly or do not import tags")
time.Sleep(30 * time.Second)
os.Exit(1)
}
if _, err := cfg.GetSection("BitTorrent"); err != nil {
cfg.NewSection("BitTorrent")
//Dirty hack for section order. Sorry
kv := cfg.Section("Network").KeysHash()
cfg.DeleteSection("Network")
cfg.NewSection("Network")
for key, value := range kv {
cfg.Section("Network").NewKey(key, value)
}
//End of dirty hack
}
if cfg.Section("BitTorrent").HasKey("Session\\Tags") {
oldtags = cfg.Section("BitTorrent").Key("Session\\Tags").String()
for _, tag := range strings.Split(oldtags, ", ") {
if checknotexists(tag, newtags) {
newtags = append(newtags, tag)
}
}
cfg.Section("BitTorrent").Key("Session\\Tags").SetValue(strings.Join(newtags, ", "))
} else {
cfg.Section("BitTorrent").NewKey("Session\\Tags", strings.Join(newtags, ", "))
}
cfg.SaveTo(config)
}
fmt.Println(len(newtags), newtags)
fmt.Println("\nPress Enter to exit")
fmt.Scanln()
}