Finish config mapping
This commit is contained in:
@@ -2,10 +2,12 @@ package torrent
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
@@ -16,6 +18,29 @@ type TorrentManager struct {
|
||||
torrents []Torrent
|
||||
workerPool chan bool
|
||||
config config.ConfigInterface
|
||||
checksum string
|
||||
}
|
||||
|
||||
func (handler *TorrentManager) refreshTorrents() {
|
||||
log.Println("Starting periodic refresh")
|
||||
for {
|
||||
<-time.After(15 * time.Second)
|
||||
checksum := handler.getChecksum()
|
||||
if checksum == handler.checksum {
|
||||
log.Println("No changes detected, skipping refresh")
|
||||
continue
|
||||
}
|
||||
handler.checksum = checksum
|
||||
handler.torrents = handler.getAll()
|
||||
for _, torrent := range handler.torrents {
|
||||
go func(id string) {
|
||||
handler.workerPool <- true
|
||||
handler.getInfo(id)
|
||||
<-handler.workerPool
|
||||
time.Sleep(1 * time.Second) // sleep for 1 second to avoid rate limiting
|
||||
}(torrent.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NewTorrentManager creates a new torrent manager
|
||||
@@ -28,27 +53,46 @@ func NewTorrentManager(token string, config config.ConfigInterface) *TorrentMana
|
||||
config: config,
|
||||
}
|
||||
|
||||
// Initialize torrents for the first time
|
||||
handler.torrents = handler.getAll()
|
||||
|
||||
for _, torrent := range handler.torrents {
|
||||
go func(id string) {
|
||||
handler.workerPool <- true
|
||||
handler.getInfo(id)
|
||||
// sleep for 1 second to avoid rate limiting
|
||||
<-handler.workerPool
|
||||
time.Sleep(1 * time.Second) // sleep for 1 second to avoid rate limiting
|
||||
}(torrent.ID)
|
||||
}
|
||||
|
||||
// Start the periodic refresh
|
||||
go handler.refreshTorrents()
|
||||
|
||||
return handler
|
||||
}
|
||||
|
||||
func (t *TorrentManager) getChecksum() string {
|
||||
torrents, totalCount, err := realdebrid.GetTorrents(t.token, 1)
|
||||
if err != nil {
|
||||
log.Printf("Cannot get torrents: %v\n", err.Error())
|
||||
return t.checksum
|
||||
}
|
||||
if len(torrents) == 0 {
|
||||
return t.checksum
|
||||
}
|
||||
return fmt.Sprintf("%d-%s", totalCount, torrents[0].ID)
|
||||
}
|
||||
|
||||
func (t *TorrentManager) getAll() []Torrent {
|
||||
log.Println("Getting all torrents")
|
||||
torrents, err := realdebrid.GetTorrents(t.token)
|
||||
|
||||
torrents, totalCount, err := realdebrid.GetTorrents(t.token, 0)
|
||||
if err != nil {
|
||||
log.Printf("Cannot get torrents: %v\n", err.Error())
|
||||
return nil
|
||||
}
|
||||
t.checksum = fmt.Sprintf("%d-%s", totalCount, torrents[0].ID)
|
||||
|
||||
var torrentsV2 []Torrent
|
||||
for _, torrent := range torrents {
|
||||
torrentV2 := Torrent{
|
||||
@@ -64,7 +108,7 @@ func (t *TorrentManager) getAll() []Torrent {
|
||||
configV1 := t.config.(*config.ZurgConfigV1)
|
||||
groupMap := configV1.GetGroupMap()
|
||||
for group, directories := range groupMap {
|
||||
log.Printf("Processing group %s\n", group)
|
||||
log.Printf("Processing directory group: %s, %v\n", group, directories)
|
||||
for i := range torrents {
|
||||
for _, directory := range directories {
|
||||
if configV1.MeetsConditions(directory, torrentsV2[i].ID, torrentsV2[i].Name) {
|
||||
@@ -92,6 +136,28 @@ func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
|
||||
return torrents
|
||||
}
|
||||
|
||||
func (t *TorrentManager) RefreshInfo(torrentID string) {
|
||||
filePath := fmt.Sprintf("data/%s.bin", torrentID)
|
||||
// Check the last modified time of the .bin file
|
||||
fileInfo, err := os.Stat(filePath)
|
||||
if err == nil {
|
||||
modTime := fileInfo.ModTime()
|
||||
// If the file was modified less than an hour ago, don't refresh
|
||||
if time.Since(modTime) < time.Hour {
|
||||
return
|
||||
}
|
||||
err = os.Remove(filePath)
|
||||
if err != nil && !os.IsNotExist(err) { // File doesn't exist or other error
|
||||
log.Printf("Cannot remove file: %v\n", err.Error())
|
||||
}
|
||||
} else if !os.IsNotExist(err) { // Error other than file not existing
|
||||
log.Printf("Error checking file info: %v\n", err.Error())
|
||||
return
|
||||
}
|
||||
info := t.getInfo(torrentID)
|
||||
log.Println("Refreshed info for", info.Name)
|
||||
}
|
||||
|
||||
func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
||||
torrentFromFile := t.readFromFile(torrentID)
|
||||
if torrentFromFile != nil {
|
||||
@@ -101,6 +167,7 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
||||
}
|
||||
return torrent
|
||||
}
|
||||
log.Println("Getting info for", torrentID)
|
||||
info, err := realdebrid.GetTorrentInfo(t.token, torrentID)
|
||||
if err != nil {
|
||||
log.Printf("Cannot get info: %v\n", err.Error())
|
||||
@@ -117,6 +184,9 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
||||
})
|
||||
}
|
||||
if len(selectedFiles) != len(info.Links) {
|
||||
// TODO: This means some files have expired
|
||||
// we need to re-add the torrent
|
||||
log.Println("Some links has expired for", info.Name)
|
||||
type Result struct {
|
||||
Filename string
|
||||
Link string
|
||||
@@ -136,7 +206,7 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
||||
defer func() { <-sem }() // Release semaphore
|
||||
|
||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
||||
return realdebrid.UnrestrictLink(t.token, lnk)
|
||||
return realdebrid.UnrestrictCheck(t.token, lnk)
|
||||
}
|
||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
||||
if resp != nil {
|
||||
@@ -147,6 +217,7 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(sem)
|
||||
close(resultsChan)
|
||||
}()
|
||||
|
||||
@@ -191,7 +262,7 @@ func (t *TorrentManager) getByID(torrentID string) *Torrent {
|
||||
}
|
||||
|
||||
func (t *TorrentManager) writeToFile(torrentID string, torrent *Torrent) {
|
||||
filePath := "data/" + torrentID + ".bin"
|
||||
filePath := fmt.Sprintf("data/%s.bin", torrentID)
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed creating file: %s", err)
|
||||
@@ -204,7 +275,7 @@ func (t *TorrentManager) writeToFile(torrentID string, torrent *Torrent) {
|
||||
}
|
||||
|
||||
func (t *TorrentManager) readFromFile(torrentID string) *Torrent {
|
||||
filePath := "data/" + torrentID + ".bin"
|
||||
filePath := fmt.Sprintf("data/%s.bin", torrentID)
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user