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

59
main.go
View File

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

View File

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

View File

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