pool and sync

This commit is contained in:
Celogeek 2023-08-15 22:04:18 +02:00
parent 8827aca192
commit c3813c0192
Signed by: celogeek
SSH Key Fingerprint: SHA256:DEDfxIK2nUWXbslbRkww3zsauDjhWHlTXar+ak4lDJ4
3 changed files with 66 additions and 42 deletions

77
main.go
View File

@ -2,27 +2,14 @@ package main
import (
"flag"
"fmt"
"log"
"time"
)
type RsyncOptions struct {
Username string
Hostname string
}
func (r *RsyncOptions) Uri(path string) string {
result := fmt.Sprintf("%s:%s", r.Hostname, path)
if r.Username == "" {
return result
}
return fmt.Sprintf("%s@%s", r.Username, result)
}
func main() {
qbitoptions := &QBitTorrentOptions{}
rsyncoptions := &RsyncOptions{}
dest := ""
var poolTime int
flag.StringVar(&qbitoptions.Uri, "qbittorrent-uri", "http://localhost:8080", "URI of qbittorrent")
flag.StringVar(&qbitoptions.Username, "qbittorrent-username", "", "Username of qbittorrent")
flag.StringVar(&qbitoptions.Password, "qbittorrent-password", "", "Password of qbittorrent")
@ -30,7 +17,8 @@ func main() {
flag.StringVar(&qbitoptions.SyncedTag, "qbittorrent-synced-tag", "", "Tag of qbittorrent when copy finished")
flag.StringVar(&rsyncoptions.Hostname, "rsync-hostname", "", "Rsync host")
flag.StringVar(&rsyncoptions.Username, "rsync-username", "", "Rsync username")
flag.StringVar(&dest, "dest", ".", "Destination directory")
flag.StringVar(&rsyncoptions.Destination, "rsync-destination", ".", "Rsync Destination directory")
flag.IntVar(&poolTime, "pool-time", 30, "Number of second to check new files to sync")
flag.Parse()
if qbitoptions.Uri == "" ||
@ -44,33 +32,54 @@ func main() {
log.Fatal("missing rsync parameters")
}
log.Print("[QBit] Authentication")
qcli, err := NewQBittorrentCli(qbitoptions)
if err != nil {
log.Fatal(err)
log.Fatalf("[QBit] Init Error: %v", err)
}
defer qcli.Logout()
torrents, err := qcli.List()
if err != nil {
log.Fatal(err)
}
t := time.NewTicker(time.Second * time.Duration(poolTime))
defer t.Stop()
for _, t := range torrents {
rtask := NewRsync(
rsyncoptions.Uri(t.Path),
dest,
func(p int) {
qcli.SetProgress(t, p)
},
)
if err := rtask.Run(); err != nil {
qcli.ClearTags()
log.Fatal(err)
log.Print("[QBit] Pooling")
for {
torrents, err := qcli.List()
if err != nil {
log.Printf("[QBit] List Error: %v", err)
}
if len(torrents) > 0 {
log.Printf("[QBit] Found %d torrents to sync", len(torrents))
}
qcli.SetDone(t)
for _, t := range torrents {
log.Printf("[Rsync] Synching %s", t.Name)
rtask := NewRsync(
&RsyncOptions{
Username: rsyncoptions.Username,
Hostname: rsyncoptions.Hostname,
Destination: rsyncoptions.Destination,
Path: t.Path,
OnProgress: func(p int) {
err := qcli.SetProgress(t, p)
if err != nil {
log.Printf("[QBit] SetProgress Error: %v", err)
}
},
})
if err := rtask.Run(); err != nil {
qcli.ClearTags()
log.Printf("[Rsync] Error: %v", err)
} else {
qcli.SetDone(t)
log.Printf("[Rsync] Synching %s done", t.Name)
}
}
<-t.C
}
}
func init() {

View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
"log"
"strings"
"github.com/go-resty/resty/v2"
@ -164,7 +163,6 @@ func (c *QBittorrentCli) SetProgress(t *Torrent, p int) error {
}
t.Progress = p
log.Printf("Downloading [%s]: %d%%", t.Name, p)
return nil
}

View File

@ -3,12 +3,29 @@ package main
import (
"bufio"
"bytes"
"fmt"
"os/exec"
"regexp"
"strconv"
"sync"
)
type RsyncOptions struct {
Username string
Hostname string
Path string
Destination string
OnProgress func(p int)
}
func (r *RsyncOptions) Uri() string {
result := fmt.Sprintf("%s:%s", r.Hostname, r.Path)
if r.Username == "" {
return result
}
return fmt.Sprintf("%s@%s", r.Username, result)
}
type Rsync struct {
Source string
Destination string
@ -17,11 +34,11 @@ type Rsync struct {
progress int
}
func NewRsync(source, destination string, onProgress func(p int)) *Rsync {
func NewRsync(options *RsyncOptions) *Rsync {
return &Rsync{
Source: source,
Destination: destination,
OnProgress: onProgress,
Source: options.Uri(),
Destination: options.Destination,
OnProgress: options.OnProgress,
progress: -1,
}
}
@ -68,8 +85,8 @@ func (r *Rsync) Run() error {
progressMatch := regexp.MustCompile(`(\d+)%`)
for scanner.Scan() {
progress := scanner.Text()
if progressMatch.MatchString(progress) {
m := progressMatch.FindStringSubmatch(progress)
m := progressMatch.FindStringSubmatch(progress)
if len(m) == 2 {
if p, err := strconv.Atoi(m[1]); err == nil {
r.OnProgress(p)
}