More pointers

Small fix for pieces
gofmt
This commit is contained in:
Alexey Kostin 2017-12-29 20:26:13 +03:00
parent 220f407bd8
commit 5d4a9bd4ce

104
b2q.go
View File

@ -1,21 +1,19 @@
package main
import (
"github.com/zeebo/bencode"
"io/ioutil"
"os"
//"reflect"
"bufio"
"crypto/sha1"
"encoding/hex"
"io"
"sync"
"log"
"bufio"
//"unicode/utf8"
"strconv"
"time"
"path/filepath"
"fmt"
"github.com/zeebo/bencode"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"sync"
"time"
)
func decodetorrentfile(path string) map[string]interface{} {
@ -30,7 +28,7 @@ func decodetorrentfile(path string) map[string]interface{} {
return torrent
}
func encodetorrentfile(path string, newstructure map[string]interface{}) error {
func encodetorrentfile(path string, newstructure *map[string]interface{}) error {
_, err := os.Stat(path)
if os.IsNotExist(err) {
os.Create(path)
@ -44,24 +42,26 @@ func encodetorrentfile(path string, newstructure map[string]interface{}) error {
defer file.Close()
bufferedWriter := bufio.NewWriter(file)
enc := bencode.NewEncoder(bufferedWriter)
if err := enc.Encode(newstructure); err != nil {
if err := enc.Encode(*newstructure); err != nil {
log.Fatal(err)
}
bufferedWriter.Flush()
return nil
}
func gethash(info interface{}) (hash string) {
torinfo, _ := bencode.EncodeString(info.(map[string]interface{}))
func gethash(info *map[string]interface{}) (hash string) {
torinfo, _ := bencode.EncodeString((*info)["info"].(map[string]interface{}))
h := sha1.New()
io.WriteString(h, torinfo)
hash = hex.EncodeToString(h.Sum(nil))
return
}
func piecesconvert(s []byte ) (newpieces []byte) {
for _, c := range s {
for _, d := range strconv.FormatInt(int64(c), 2) {
func piecesconvert(s *map[string]interface{}) (newpieces []byte) {
for _, c := range []byte((*s)["have"].(string)) {
var binString string
binString = fmt.Sprintf("%s%.8b", binString, c)
for _, d := range binString {
chr, _ := strconv.Atoi(string(d))
newpieces = append(newpieces, byte(chr))
}
@ -69,25 +69,24 @@ func piecesconvert(s []byte ) (newpieces []byte) {
return
}
func prioconvert(src string) (newprio []int){
for _, c := range []byte(src) {
if i := int(c); (i == 0) || (i ==128) { // if not selected
func prioconvert(src *map[string]interface{}) (newprio []int) {
for _, c := range []byte((*src)["prio"].(string)) {
if i := int(c); (i == 0) || (i == 128) { // if not selected
newprio = append(newprio, 0)
} else if (i == 4) || (i ==8) { // if low or normal prio
} else if (i == 4) || (i == 8) { // if low or normal prio
newprio = append(newprio, 1)
} else if (i == 12) { // if high prio
} else if i == 12 { // if high prio
newprio = append(newprio, 6)
}
}
return
}
func lenght (src interface{}) (lenght int64) {
return src.(map[string]interface{})["info"].(map[string]interface{})["length"].(int64)
func lenght(src *map[string]interface{}) (lenght int64) {
return (*src)["info"].(map[string]interface{})["length"].(int64)
}
func fmtime (path string) (mtime int64) {
func fmtime(path string) (mtime int64) {
fi, err := os.Stat(path)
if err != nil {
return 0
@ -96,7 +95,7 @@ func fmtime (path string) (mtime int64) {
return
}
func copyfile (src string, dst string) error {
func copyfile(src string, dst string) error {
originalFile, err := os.Open(src)
if err != nil {
log.Fatal(err)
@ -149,18 +148,30 @@ func logic(key string, value interface{}, bitdir *string, wg *sync.WaitGroup, wi
newstructure["info-hash"] = local["info"]
newstructure["qBt-tags"] = local["labels"]
newstructure["blocks per piece"] = torrentfile["info"].(map[string]interface{})["piece length"].(int64) / local["blocksize"].(int64)
newstructure["pieces"] = piecesconvert([]byte(local["have"].(string)))
newstructure["pieces"] = piecesconvert(&local)
newstructure["seeding_time"] = local["runtime"]
if newstructure["paused"] = 0; local["started"] == 0 { newstructure["paused"] = 1 }
if newstructure["paused"] = 0; local["started"] == 0 {
newstructure["paused"] = 1
}
newstructure["finished_time"] = int(time.Since(time.Unix(local["completed_on"].(int64), 0)).Minutes())
if local["completed_on"] != 0 { newstructure["last_seen_complete"] = int(time.Now().Unix()) }
if local["completed_on"] != 0 {
newstructure["last_seen_complete"] = int(time.Now().Unix())
}
newstructure["total_downloaded"] = local["downloaded"]
newstructure["total_uploaded"] = local["uploaded"]
newstructure["upload_rate_limit"] = local["upspeed"]
if *with_label == true { newstructure["qBt-category"] = local["label"] }
if *with_tags == true { newstructure["qBt-tags"] = local["labels"] }
newstructure["trackers"] = [][]interface{}{local["trackers"].([]interface{})}
newstructure["file_priority"] = prioconvert(local["prio"].(string))
if *with_label == true {
newstructure["qBt-category"] = local["label"]
}
if *with_tags == true {
newstructure["qBt-tags"] = local["labels"]
}
var trackers []interface{}
for _, tracker := range local["trackers"].([]interface{}) {
trackers = append(trackers, tracker)
}
newstructure["trackers"] = trackers
newstructure["file_priority"] = prioconvert(&local)
if files, ok := torrentfile["info"].(map[string]interface{})["files"]; ok {
var filelists []interface{}
for num, file := range files.([]interface{}) {
@ -170,22 +181,25 @@ func logic(key string, value interface{}, bitdir *string, wg *sync.WaitGroup, wi
if n := newstructure["file_priority"].([]int)[num]; n != 0 {
lenght = file.(map[string]interface{})["length"].(int64)
mtime = fmtime(fullpath)
} else { lenght, mtime = 0, 0 }
} else {
lenght, mtime = 0, 0
}
flenmtime := []int64{lenght, mtime}
filelists = append(filelists, flenmtime)
}
newstructure["save_path"] = local["path"].(string)+"\\"
newstructure["file sizes"] = filelists
} else {
newstructure["save_path"] = filepath.Dir(local["path"].(string))+"\\"
newstructure["file sizes"] = [][]int64{{lenght(torrentfile), fmtime(local["path"].(string))}}
newstructure["file sizes"] = [][]int64{{lenght(&torrentfile), fmtime(local["path"].(string))}}
}
newstructure["save_path"] = filepath.Dir(local["path"].(string)) + "\\"
newstructure["qBt-savePath"] = newstructure["save_path"]
newbasename := gethash(torrentfile["info"])
if err := encodetorrentfile(*qbitdir + newbasename + ".fastresume", newstructure); err != nil {
newbasename := gethash(&torrentfile)
if err := encodetorrentfile(*qbitdir+newbasename+".fastresume", &newstructure); err != nil {
fmt.Println(err)
}
if err := copyfile(*bitdir + key, *qbitdir + newbasename + ".torrent"); err != nil {
if err := copyfile(*bitdir+key, *qbitdir+newbasename+".torrent"); err != nil {
fmt.Println(err)
}
}
@ -193,7 +207,7 @@ func logic(key string, value interface{}, bitdir *string, wg *sync.WaitGroup, wi
func main() {
var wg sync.WaitGroup
bitdir := "C:/Users/rumanzo/AppData/Roaming/BitTorrent/"
qbitdir := "F:/rbtemp/"
qbitdir := "C:/Users/rumanzo/AppData/Local/qBittorrent/BT_backup/"
torrent := decodetorrentfile(bitdir + "resume.dat")
var with_label, with_tags bool
with_label, with_tags = true, true
@ -205,3 +219,5 @@ func main() {
}
wg.Wait()
}
//TODO fix pieces.