Changelog 0.6.0
This commit is contained in:
@@ -57,7 +57,7 @@ func New(dc config.Debrid) *AllDebrid {
|
||||
}
|
||||
return &AllDebrid{
|
||||
Name: "alldebrid",
|
||||
Host: dc.Host,
|
||||
Host: "http://api.alldebrid.com/v4.1",
|
||||
APIKey: dc.APIKey,
|
||||
DownloadKeys: accounts,
|
||||
DownloadUncached: dc.DownloadUncached,
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/types"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -111,10 +110,6 @@ func New(dc config.Debrid, client types.Client) *Cache {
|
||||
if err != nil {
|
||||
autoExpiresLinksAfter = time.Hour * 24
|
||||
}
|
||||
workers := runtime.NumCPU() * 50
|
||||
if dc.Workers > 0 {
|
||||
workers = dc.Workers
|
||||
}
|
||||
return &Cache{
|
||||
dir: filepath.Join(cfg.Path, "cache", dc.Name), // path to save cache files
|
||||
torrents: xsync.NewMapOf[string, *CachedTorrent](),
|
||||
@@ -122,7 +117,7 @@ func New(dc config.Debrid, client types.Client) *Cache {
|
||||
invalidDownloadLinks: xsync.NewMapOf[string, string](),
|
||||
client: client,
|
||||
logger: logger.New(fmt.Sprintf("%s-webdav", client.GetName())),
|
||||
workers: workers,
|
||||
workers: dc.Workers,
|
||||
downloadLinks: xsync.NewMapOf[string, downloadLinkCache](),
|
||||
torrentRefreshInterval: torrentRefreshInterval,
|
||||
downloadLinksRefreshInterval: downloadLinksRefreshInterval,
|
||||
@@ -211,13 +206,13 @@ func (c *Cache) load() (map[string]*CachedTorrent, error) {
|
||||
filePath := filepath.Join(c.dir, fileName)
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to read file: %s", filePath)
|
||||
c.logger.Error().Err(err).Msgf("Failed to read file: %s", filePath)
|
||||
continue
|
||||
}
|
||||
|
||||
var ct CachedTorrent
|
||||
if err := json.Unmarshal(data, &ct); err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to unmarshal file: %s", filePath)
|
||||
c.logger.Error().Err(err).Msgf("Failed to unmarshal file: %s", filePath)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -271,7 +266,7 @@ func (c *Cache) Sync() error {
|
||||
defer c.logger.Info().Msg("WebDav server sync complete")
|
||||
cachedTorrents, err := c.load()
|
||||
if err != nil {
|
||||
c.logger.Debug().Err(err).Msg("Failed to load cache")
|
||||
c.logger.Error().Err(err).Msg("Failed to load cache")
|
||||
}
|
||||
|
||||
torrents, err := c.client.GetTorrents()
|
||||
@@ -465,7 +460,7 @@ func (c *Cache) SaveTorrents() {
|
||||
func (c *Cache) SaveTorrent(ct *CachedTorrent) {
|
||||
marshaled, err := json.MarshalIndent(ct, "", " ")
|
||||
if err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to marshal torrent: %s", ct.Id)
|
||||
c.logger.Error().Err(err).Msgf("Failed to marshal torrent: %s", ct.Id)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -500,7 +495,7 @@ func (c *Cache) saveTorrent(id string, data []byte) {
|
||||
|
||||
f, err := os.Create(tmpFile)
|
||||
if err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to create file: %s", tmpFile)
|
||||
c.logger.Error().Err(err).Msgf("Failed to create file: %s", tmpFile)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -517,12 +512,12 @@ func (c *Cache) saveTorrent(id string, data []byte) {
|
||||
|
||||
w := bufio.NewWriter(f)
|
||||
if _, err := w.Write(data); err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to write data: %s", tmpFile)
|
||||
c.logger.Error().Err(err).Msgf("Failed to write data: %s", tmpFile)
|
||||
return
|
||||
}
|
||||
|
||||
if err := w.Flush(); err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to flush data: %s", tmpFile)
|
||||
c.logger.Error().Err(err).Msgf("Failed to flush data: %s", tmpFile)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -531,7 +526,7 @@ func (c *Cache) saveTorrent(id string, data []byte) {
|
||||
fileClosed = true
|
||||
|
||||
if err := os.Rename(tmpFile, filePath); err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to rename file: %s", tmpFile)
|
||||
c.logger.Error().Err(err).Msgf("Failed to rename file: %s", tmpFile)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -559,7 +554,7 @@ func (c *Cache) ProcessTorrent(t *types.Torrent, refreshRclone bool) error {
|
||||
c.logger.Debug().Msgf("Torrent %s is still not complete. Triggering a reinsert(disabled)", t.Id)
|
||||
//ct, err := c.reInsertTorrent(t)
|
||||
//if err != nil {
|
||||
// c.logger.Debug().Err(err).Msgf("Failed to reinsert torrent %s", t.Id)
|
||||
// c.logger.Error().Err(err).Msgf("Failed to reinsert torrent %s", t.Id)
|
||||
// return err
|
||||
//}
|
||||
//c.logger.Debug().Msgf("Reinserted torrent %s", ct.Id)
|
||||
@@ -610,9 +605,9 @@ func (c *Cache) GetDownloadLink(torrentId, filename, fileLink string) string {
|
||||
if file.Link == "" {
|
||||
c.logger.Debug().Msgf("File link is empty for %s. Release is probably nerfed", filename)
|
||||
// Try to reinsert the torrent?
|
||||
ct, err := c.reInsertTorrent(ct.Torrent)
|
||||
ct, err := c.reInsertTorrent(ct)
|
||||
if err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to reinsert torrent %s", ct.Name)
|
||||
c.logger.Error().Err(err).Msgf("Failed to reinsert torrent %s", ct.Name)
|
||||
return ""
|
||||
}
|
||||
file = ct.Files[filename]
|
||||
@@ -623,10 +618,10 @@ func (c *Cache) GetDownloadLink(torrentId, filename, fileLink string) string {
|
||||
downloadLink, err := c.client.GetDownloadLink(ct.Torrent, &file)
|
||||
if err != nil {
|
||||
if errors.Is(err, request.HosterUnavailableError) {
|
||||
c.logger.Debug().Err(err).Msgf("Hoster is unavailable. Triggering repair for %s", ct.Name)
|
||||
ct, err := c.reInsertTorrent(ct.Torrent)
|
||||
c.logger.Error().Err(err).Msgf("Hoster is unavailable. Triggering repair for %s", ct.Name)
|
||||
ct, err := c.reInsertTorrent(ct)
|
||||
if err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to reinsert torrent %s", ct.Name)
|
||||
c.logger.Error().Err(err).Msgf("Failed to reinsert torrent %s", ct.Name)
|
||||
return ""
|
||||
}
|
||||
c.logger.Debug().Msgf("Reinserted torrent %s", ct.Name)
|
||||
@@ -634,7 +629,7 @@ func (c *Cache) GetDownloadLink(torrentId, filename, fileLink string) string {
|
||||
// Retry getting the download link
|
||||
downloadLink, err = c.client.GetDownloadLink(ct.Torrent, &file)
|
||||
if err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to get download link for %s", file.Link)
|
||||
c.logger.Error().Err(err).Msgf("Failed to get download link for %s", file.Link)
|
||||
return ""
|
||||
}
|
||||
if downloadLink == nil {
|
||||
@@ -645,9 +640,9 @@ func (c *Cache) GetDownloadLink(torrentId, filename, fileLink string) string {
|
||||
return downloadLink.DownloadLink
|
||||
} else if errors.Is(err, request.TrafficExceededError) {
|
||||
// This is likely a fair usage limit error
|
||||
c.logger.Debug().Err(err).Msgf("Traffic exceeded for %s", ct.Name)
|
||||
c.logger.Error().Err(err).Msgf("Traffic exceeded for %s", ct.Name)
|
||||
} else {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to get download link for %s", file.Link)
|
||||
c.logger.Error().Err(err).Msgf("Failed to get download link for %s", file.Link)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,14 @@ package debrid
|
||||
import (
|
||||
"github.com/sirrobot01/decypharr/internal/config"
|
||||
"github.com/sirrobot01/decypharr/pkg/debrid/types"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Engine struct {
|
||||
Clients map[string]types.Client
|
||||
clientsMu sync.Mutex
|
||||
Caches map[string]*Cache
|
||||
CacheMu sync.Mutex
|
||||
LastUsed string
|
||||
}
|
||||
|
||||
@@ -37,16 +40,9 @@ func NewEngine() *Engine {
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *Engine) Get() types.Client {
|
||||
if d.LastUsed == "" {
|
||||
for _, c := range d.Clients {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return d.Clients[d.LastUsed]
|
||||
}
|
||||
|
||||
func (d *Engine) GetByName(name string) types.Client {
|
||||
func (d *Engine) GetClient(name string) types.Client {
|
||||
d.clientsMu.Lock()
|
||||
defer d.clientsMu.Unlock()
|
||||
return d.Clients[name]
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ func (c *Cache) refreshTorrents() {
|
||||
// Get all torrents from the debrid service
|
||||
debTorrents, err := c.client.GetTorrents()
|
||||
if err != nil {
|
||||
c.logger.Debug().Err(err).Msg("Failed to get torrents")
|
||||
c.logger.Error().Err(err).Msg("Failed to get torrents")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ func (c *Cache) refreshTorrents() {
|
||||
default:
|
||||
}
|
||||
if err := c.ProcessTorrent(t, true); err != nil {
|
||||
c.logger.Debug().Err(err).Msgf("Failed to process new torrent %s", t.Id)
|
||||
c.logger.Error().Err(err).Msgf("Failed to process new torrent %s", t.Id)
|
||||
errChan <- err
|
||||
}
|
||||
}
|
||||
@@ -208,7 +208,7 @@ func (c *Cache) refreshDownloadLinks() {
|
||||
|
||||
downloadLinks, err := c.client.GetDownloads()
|
||||
if err != nil {
|
||||
c.logger.Debug().Err(err).Msg("Failed to get download links")
|
||||
c.logger.Error().Err(err).Msg("Failed to get download links")
|
||||
}
|
||||
for k, v := range downloadLinks {
|
||||
// if link is generated in the last 24 hours, add it to cache
|
||||
@@ -225,6 +225,6 @@ func (c *Cache) refreshDownloadLinks() {
|
||||
}
|
||||
}
|
||||
|
||||
c.logger.Debug().Msgf("Refreshed %d download links", len(downloadLinks))
|
||||
c.logger.Trace().Msgf("Refreshed %d download links", len(downloadLinks))
|
||||
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ func (c *Cache) repairWorker() {
|
||||
case RepairTypeReinsert:
|
||||
c.logger.Debug().Str("torrentId", torrentId).Msg("Reinserting torrent")
|
||||
var err error
|
||||
cachedTorrent, err = c.reInsertTorrent(cachedTorrent.Torrent)
|
||||
cachedTorrent, err = c.reInsertTorrent(cachedTorrent)
|
||||
if err != nil {
|
||||
c.logger.Error().Err(err).Str("torrentId", cachedTorrent.Id).Msg("Failed to reinsert torrent")
|
||||
continue
|
||||
@@ -96,10 +96,11 @@ func (c *Cache) repairWorker() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) reInsertTorrent(torrent *types.Torrent) (*CachedTorrent, error) {
|
||||
func (c *Cache) reInsertTorrent(ct *CachedTorrent) (*CachedTorrent, error) {
|
||||
// Check if Magnet is not empty, if empty, reconstruct the magnet
|
||||
torrent := ct.Torrent
|
||||
if _, ok := c.repairsInProgress.Load(torrent.Id); ok {
|
||||
return nil, fmt.Errorf("repair already in progress for torrent %s", torrent.Id)
|
||||
return ct, fmt.Errorf("repair already in progress for torrent %s", torrent.Id)
|
||||
}
|
||||
|
||||
if torrent.Magnet == nil {
|
||||
@@ -152,7 +153,7 @@ func (c *Cache) reInsertTorrent(torrent *types.Torrent) (*CachedTorrent, error)
|
||||
if err != nil {
|
||||
addedOn = time.Now()
|
||||
}
|
||||
ct := &CachedTorrent{
|
||||
ct = &CachedTorrent{
|
||||
Torrent: torrent,
|
||||
IsComplete: len(torrent.Files) > 0,
|
||||
AddedOn: addedOn,
|
||||
|
||||
@@ -299,7 +299,7 @@ func New(dc config.Debrid) *DebridLink {
|
||||
}
|
||||
return &DebridLink{
|
||||
Name: "debridlink",
|
||||
Host: dc.Host,
|
||||
Host: "https://debrid-link.com/api/v2",
|
||||
APIKey: dc.APIKey,
|
||||
DownloadKeys: accounts,
|
||||
DownloadUncached: dc.DownloadUncached,
|
||||
|
||||
@@ -84,7 +84,7 @@ func New(dc config.Debrid) *RealDebrid {
|
||||
|
||||
return &RealDebrid{
|
||||
Name: "realdebrid",
|
||||
Host: dc.Host,
|
||||
Host: "https://api.real-debrid.com/rest/1.0",
|
||||
APIKey: dc.APIKey,
|
||||
DownloadKeys: accounts,
|
||||
DownloadUncached: dc.DownloadUncached,
|
||||
|
||||
@@ -62,7 +62,7 @@ func New(dc config.Debrid) *Torbox {
|
||||
|
||||
return &Torbox{
|
||||
Name: "torbox",
|
||||
Host: dc.Host,
|
||||
Host: "https://api.torbox.app/v1",
|
||||
APIKey: dc.APIKey,
|
||||
DownloadKeys: accounts,
|
||||
DownloadUncached: dc.DownloadUncached,
|
||||
|
||||
@@ -58,7 +58,7 @@ func (t *Torrent) GetSymlinkFolder(parent string) string {
|
||||
}
|
||||
|
||||
func (t *Torrent) GetMountFolder(rClonePath string) (string, error) {
|
||||
_log := logger.GetDefaultLogger()
|
||||
_log := logger.Default()
|
||||
possiblePaths := []string{
|
||||
t.OriginalFilename,
|
||||
t.Filename,
|
||||
|
||||
Reference in New Issue
Block a user