Create a torrentMap
This commit is contained in:
@@ -26,8 +26,7 @@ type ConfigInterface interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LoadZurgConfig(filename string) (ConfigInterface, error) {
|
func LoadZurgConfig(filename string) (ConfigInterface, error) {
|
||||||
rlog := logutil.NewLogger()
|
log := logutil.NewLogger().Named("config")
|
||||||
log := rlog.Named("config")
|
|
||||||
|
|
||||||
log.Debug("Loading config file ", filename)
|
log.Debug("Loading config file ", filename)
|
||||||
content, err := os.ReadFile(filename)
|
content, err := os.ReadFile(filename)
|
||||||
|
|||||||
@@ -33,8 +33,7 @@ func (z *ZurgConfigV1) GetDirectories() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (z *ZurgConfigV1) GetGroupMap() map[string][]string {
|
func (z *ZurgConfigV1) GetGroupMap() map[string][]string {
|
||||||
rlog := logutil.NewLogger()
|
log := logutil.NewLogger().Named("config")
|
||||||
log := rlog.Named("config")
|
|
||||||
|
|
||||||
var groupMap = make(map[string][]string)
|
var groupMap = make(map[string][]string)
|
||||||
var groupOrderMap = make(map[string]int) // To store GroupOrder for each directory
|
var groupOrderMap = make(map[string]int) // To store GroupOrder for each directory
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
||||||
rlog := logutil.NewLogger()
|
log := logutil.NewLogger().Named("dav")
|
||||||
log := rlog.Named("dav")
|
|
||||||
|
|
||||||
requestPath := path.Clean(r.URL.Path)
|
requestPath := path.Clean(r.URL.Path)
|
||||||
requestPath = strings.Trim(requestPath, "/")
|
requestPath = strings.Trim(requestPath, "/")
|
||||||
|
|||||||
@@ -18,12 +18,12 @@ func createMultiTorrentResponse(basePath string, torrents []torrent.Torrent) (*d
|
|||||||
if item.Progress != 100 {
|
if item.Progress != 100 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, exists := seen[item.Name]; exists {
|
if _, exists := seen[item.AccessKey]; exists {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
seen[item.Name] = true
|
seen[item.AccessKey] = true
|
||||||
|
|
||||||
path := filepath.Join(basePath, item.Name)
|
path := filepath.Join(basePath, item.AccessKey)
|
||||||
responses = append(responses, dav.Directory(path))
|
responses = append(responses, dav.Directory(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (*
|
|||||||
var responses []dav.Response
|
var responses []dav.Response
|
||||||
|
|
||||||
// initial response is the directory itself
|
// initial response is the directory itself
|
||||||
currentPath := filepath.Join(basePath, torrents[0].Name)
|
currentPath := filepath.Join(basePath, torrents[0].AccessKey)
|
||||||
responses = append(responses, dav.Directory(currentPath))
|
responses = append(responses, dav.Directory(currentPath))
|
||||||
|
|
||||||
finalName := make(map[string]bool)
|
finalName := make(map[string]bool)
|
||||||
|
|||||||
@@ -14,8 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
||||||
rlog := logutil.NewLogger()
|
log := logutil.NewLogger().Named("http")
|
||||||
log := rlog.Named("http")
|
|
||||||
|
|
||||||
requestPath := path.Clean(r.URL.Path)
|
requestPath := path.Clean(r.URL.Path)
|
||||||
|
|
||||||
|
|||||||
@@ -18,13 +18,13 @@ func createMultiTorrentResponse(basePath string, torrents []torrent.Torrent) (st
|
|||||||
if item.Progress != 100 {
|
if item.Progress != 100 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, exists := seen[item.Name]; exists {
|
if _, exists := seen[item.AccessKey]; exists {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
seen[item.Name] = true
|
seen[item.AccessKey] = true
|
||||||
|
|
||||||
path := filepath.Join(basePath, url.PathEscape(item.Name))
|
path := filepath.Join(basePath, url.PathEscape(item.AccessKey))
|
||||||
htmlDoc += fmt.Sprintf("<li><a href=\"%s/\">%s</a></li>", path, item.Name)
|
htmlDoc += fmt.Sprintf("<li><a href=\"%s/\">%s</a></li>", path, item.AccessKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
return htmlDoc, nil
|
return htmlDoc, nil
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ import (
|
|||||||
|
|
||||||
// Router creates a WebDAV router
|
// Router creates a WebDAV router
|
||||||
func Router(mux *http.ServeMux, c config.ConfigInterface, t *torrent.TorrentManager, cache *expirable.LRU[string, string]) {
|
func Router(mux *http.ServeMux, c config.ConfigInterface, t *torrent.TorrentManager, cache *expirable.LRU[string, string]) {
|
||||||
rlog := logutil.NewLogger()
|
log := logutil.NewLogger().Named("net")
|
||||||
log := rlog.Named("net")
|
|
||||||
|
|
||||||
mux.HandleFunc("/http/", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/http/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
|
|||||||
@@ -30,8 +30,7 @@ func (se *ScriptExecutor) Execute() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func OnLibraryUpdateHook(config config.ConfigInterface) {
|
func OnLibraryUpdateHook(config config.ConfigInterface) {
|
||||||
rlog := logutil.NewLogger()
|
log := logutil.NewLogger().Named("hooks")
|
||||||
log := rlog.Named("hooks")
|
|
||||||
|
|
||||||
executor := &ScriptExecutor{
|
executor := &ScriptExecutor{
|
||||||
Script: config.GetOnLibraryUpdate(),
|
Script: config.GetOnLibraryUpdate(),
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ import (
|
|||||||
|
|
||||||
type TorrentManager struct {
|
type TorrentManager struct {
|
||||||
requiredVersion string
|
requiredVersion string
|
||||||
|
rd *realdebrid.RealDebrid
|
||||||
torrents []Torrent
|
torrents []Torrent
|
||||||
|
torrentMap map[string]*Torrent
|
||||||
inProgress []string
|
inProgress []string
|
||||||
checksum string
|
checksum string
|
||||||
config config.ConfigInterface
|
config config.ConfigInterface
|
||||||
@@ -35,7 +37,9 @@ type TorrentManager struct {
|
|||||||
// and store them in-memory; it is called only once at startup
|
// and store them in-memory; it is called only once at startup
|
||||||
func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[string, string]) *TorrentManager {
|
func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[string, string]) *TorrentManager {
|
||||||
t := &TorrentManager{
|
t := &TorrentManager{
|
||||||
requiredVersion: fmt.Sprintf("4.11.2023 - retain:%v", config.EnableRetainFolderNameExtension()),
|
requiredVersion: fmt.Sprintf("8.11.2023 - retain:%v", config.EnableRetainFolderNameExtension()),
|
||||||
|
rd: realdebrid.NewRealDebrid(config.GetToken(), logutil.NewLogger().Named("realdebrid")),
|
||||||
|
torrentMap: make(map[string]*Torrent),
|
||||||
config: config,
|
config: config,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
workerPool: make(chan bool, config.GetNumOfWorkers()),
|
workerPool: make(chan bool, config.GetNumOfWorkers()),
|
||||||
@@ -45,32 +49,49 @@ func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[strin
|
|||||||
log: logutil.NewLogger().Named("manager"),
|
log: logutil.NewLogger().Named("manager"),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize torrents for the first time
|
// start with a clean slate
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
t.torrents = t.getFreshListFromAPI()
|
t.cache.Purge()
|
||||||
t.checksum = t.getChecksum()
|
t.torrents = nil
|
||||||
t.mu.Unlock()
|
|
||||||
|
|
||||||
// log.Println("First checksum", t.checksum)
|
newTorrents, _, err := t.rd.GetTorrents(0)
|
||||||
|
if err != nil {
|
||||||
|
t.log.Fatalf("Cannot get torrents: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
torrentsChan := make(chan *Torrent, len(newTorrents))
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
for i := range newTorrents {
|
||||||
for i := range t.torrents {
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(idx int) {
|
go func(idx int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
t.workerPool <- true
|
t.workerPool <- true
|
||||||
t.addMoreInfo(&t.torrents[idx])
|
torrentsChan <- t.getMoreInfo(newTorrents[idx])
|
||||||
<-t.workerPool
|
<-t.workerPool
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
|
wg.Wait()
|
||||||
|
close(torrentsChan)
|
||||||
|
for newTorrent := range torrentsChan {
|
||||||
|
if newTorrent == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t.torrents = append(t.torrents, *newTorrent)
|
||||||
|
if _, exists := t.torrentMap[newTorrent.AccessKey]; exists {
|
||||||
|
t.torrentMap[newTorrent.AccessKey].Files = newTorrent.Files
|
||||||
|
t.torrentMap[newTorrent.AccessKey].Links = newTorrent.Links
|
||||||
|
t.torrentMap[newTorrent.AccessKey].SelectedFiles = newTorrent.SelectedFiles
|
||||||
|
t.torrentMap[newTorrent.AccessKey].ForRepair = newTorrent.ForRepair
|
||||||
|
} else {
|
||||||
|
t.torrentMap[newTorrent.AccessKey] = newTorrent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.checksum = t.getChecksum()
|
||||||
|
t.mu.Unlock()
|
||||||
|
|
||||||
if t.config.EnableRepair() {
|
if t.config.EnableRepair() {
|
||||||
go t.repairAll(&wg)
|
go t.repairAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
t.mapToDirectories()
|
|
||||||
go t.startRefreshJob()
|
go t.startRefreshJob()
|
||||||
|
|
||||||
return t
|
return t
|
||||||
@@ -80,7 +101,7 @@ func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[strin
|
|||||||
func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
|
func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
|
||||||
var torrents []Torrent
|
var torrents []Torrent
|
||||||
for i := range t.torrents {
|
for i := range t.torrents {
|
||||||
for _, dir := range t.directoryMap[t.torrents[i].Name] {
|
for _, dir := range t.directoryMap[t.torrents[i].AccessKey] {
|
||||||
if dir == directory {
|
if dir == directory {
|
||||||
torrents = append(torrents, t.torrents[i])
|
torrents = append(torrents, t.torrents[i])
|
||||||
}
|
}
|
||||||
@@ -100,13 +121,18 @@ func (t *TorrentManager) FindAllTorrentsWithName(directory, torrentName string)
|
|||||||
var matchingTorrents []Torrent
|
var matchingTorrents []Torrent
|
||||||
torrents := t.GetByDirectory(directory)
|
torrents := t.GetByDirectory(directory)
|
||||||
for i := range torrents {
|
for i := range torrents {
|
||||||
if torrents[i].Name == torrentName || strings.HasPrefix(torrents[i].Name, torrentName) {
|
if torrents[i].AccessKey == torrentName || strings.Contains(torrents[i].AccessKey, torrentName) {
|
||||||
matchingTorrents = append(matchingTorrents, torrents[i])
|
matchingTorrents = append(matchingTorrents, torrents[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matchingTorrents
|
return matchingTorrents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// proxy
|
||||||
|
func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.UnrestrictResponse {
|
||||||
|
return t.rd.UnrestrictUntilOk(link)
|
||||||
|
}
|
||||||
|
|
||||||
// findAllDownloadedFilesFromHash finds all files that were with a given hash
|
// findAllDownloadedFilesFromHash finds all files that were with a given hash
|
||||||
func (t *TorrentManager) findAllDownloadedFilesFromHash(hash string) []File {
|
func (t *TorrentManager) findAllDownloadedFilesFromHash(hash string) []File {
|
||||||
var files []File
|
var files []File
|
||||||
@@ -134,7 +160,7 @@ func (t *TorrentManager) getChecksum() string {
|
|||||||
|
|
||||||
// GetTorrents request
|
// GetTorrents request
|
||||||
go func() {
|
go func() {
|
||||||
torrents, totalCount, err := realdebrid.GetTorrents(t.config.GetToken(), 1)
|
torrents, totalCount, err := t.rd.GetTorrents(1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- err
|
errChan <- err
|
||||||
return
|
return
|
||||||
@@ -144,7 +170,7 @@ func (t *TorrentManager) getChecksum() string {
|
|||||||
|
|
||||||
// GetActiveTorrentCount request
|
// GetActiveTorrentCount request
|
||||||
go func() {
|
go func() {
|
||||||
count, err := realdebrid.GetActiveTorrentCount(t.config.GetToken())
|
count, err := t.rd.GetActiveTorrentCount()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- err
|
errChan <- err
|
||||||
return
|
return
|
||||||
@@ -186,92 +212,79 @@ func (t *TorrentManager) startRefreshJob() {
|
|||||||
if checksum == t.checksum {
|
if checksum == t.checksum {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.mu.Lock()
|
||||||
t.cache.Purge()
|
t.cache.Purge()
|
||||||
|
t.torrents = nil
|
||||||
|
|
||||||
newTorrents := t.getFreshListFromAPI()
|
newTorrents, _, err := t.rd.GetTorrents(0)
|
||||||
|
if err != nil {
|
||||||
|
t.log.Errorf("Cannot get torrents: %v\n", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
torrentsChan := make(chan *Torrent)
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
for i := range newTorrents {
|
for i := range newTorrents {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(idx int) {
|
go func(idx int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
t.log.Debug(newTorrents[idx].ID)
|
||||||
t.workerPool <- true
|
t.workerPool <- true
|
||||||
t.addMoreInfo(&newTorrents[idx])
|
torrentsChan <- t.getMoreInfo(newTorrents[idx])
|
||||||
<-t.workerPool
|
<-t.workerPool
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
close(torrentsChan)
|
||||||
// apply side effects
|
for newTorrent := range torrentsChan {
|
||||||
t.mu.Lock()
|
if newTorrent == nil {
|
||||||
t.torrents = newTorrents
|
continue
|
||||||
|
}
|
||||||
|
t.torrents = append(t.torrents, *newTorrent)
|
||||||
|
if _, exists := t.torrentMap[newTorrent.AccessKey]; exists {
|
||||||
|
t.torrentMap[newTorrent.AccessKey].Files = newTorrent.Files
|
||||||
|
t.torrentMap[newTorrent.AccessKey].Links = newTorrent.Links
|
||||||
|
t.torrentMap[newTorrent.AccessKey].SelectedFiles = newTorrent.SelectedFiles
|
||||||
|
t.torrentMap[newTorrent.AccessKey].ForRepair = newTorrent.ForRepair
|
||||||
|
} else {
|
||||||
|
t.torrentMap[newTorrent.AccessKey] = newTorrent
|
||||||
|
}
|
||||||
|
}
|
||||||
t.checksum = t.getChecksum()
|
t.checksum = t.getChecksum()
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
|
|
||||||
// log.Println("Checksum changed", t.checksum)
|
|
||||||
if t.config.EnableRepair() {
|
if t.config.EnableRepair() {
|
||||||
go t.repairAll(&wg)
|
go t.repairAll()
|
||||||
}
|
}
|
||||||
go t.mapToDirectories()
|
|
||||||
go OnLibraryUpdateHook(t.config)
|
go OnLibraryUpdateHook(t.config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getFreshListFromAPI returns all torrents
|
// getMoreInfo updates the selected files for a torrent
|
||||||
func (t *TorrentManager) getFreshListFromAPI() []Torrent {
|
func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
|
||||||
torrents, _, err := realdebrid.GetTorrents(t.config.GetToken(), 0)
|
t.log.Info("Getting more info for", rdTorrent.ID)
|
||||||
if err != nil {
|
|
||||||
t.log.Errorf("Cannot get torrents: %v\n", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert to own internal type without SelectedFiles yet
|
|
||||||
// populate inProgress
|
|
||||||
var torrentsV2 []Torrent
|
|
||||||
t.inProgress = t.inProgress[:0] // reset
|
|
||||||
for _, torrent := range torrents {
|
|
||||||
torrent.Name = strings.TrimSuffix(torrent.Name, "/")
|
|
||||||
torrentV2 := Torrent{
|
|
||||||
Torrent: torrent,
|
|
||||||
SelectedFiles: nil,
|
|
||||||
ForRepair: false,
|
|
||||||
lock: &sync.Mutex{},
|
|
||||||
}
|
|
||||||
torrentsV2 = append(torrentsV2, torrentV2)
|
|
||||||
|
|
||||||
if torrent.Progress != 100 {
|
|
||||||
t.inProgress = append(t.inProgress, torrent.Hash)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
t.log.Infof("Fetched %d torrents", len(torrentsV2))
|
|
||||||
return torrentsV2
|
|
||||||
}
|
|
||||||
|
|
||||||
// addMoreInfo updates the selected files for a torrent
|
|
||||||
func (t *TorrentManager) addMoreInfo(torrent *Torrent) {
|
|
||||||
// file cache
|
// file cache
|
||||||
torrentFromFile := t.readFromFile(torrent.ID)
|
torrentFromFile := t.readFromFile(rdTorrent.ID)
|
||||||
if torrentFromFile != nil {
|
if torrentFromFile != nil {
|
||||||
// see if api data and file data still match
|
// see if api data and file data still match
|
||||||
// then it means data is still usable
|
// then it means data is still usable
|
||||||
if len(torrentFromFile.Links) == len(torrent.Links) {
|
if len(torrentFromFile.Links) == len(rdTorrent.Links) {
|
||||||
torrent.Name = t.getName(torrentFromFile)
|
return torrentFromFile
|
||||||
torrent.ForRepair = torrentFromFile.ForRepair
|
|
||||||
torrent.SelectedFiles = torrentFromFile.SelectedFiles[:]
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// no file data yet as it is still downloading
|
t.log.Debug("Getting info for", rdTorrent.ID)
|
||||||
if torrent.Progress != 100 {
|
info, err := t.rd.GetTorrentInfo(rdTorrent.ID)
|
||||||
return
|
if err != nil {
|
||||||
|
t.log.Errorf("Cannot get info for id=%s: %v\n", rdTorrent.ID, err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// t.log.Println("Getting info for", torrent.ID)
|
torrent := Torrent{
|
||||||
info, err := realdebrid.GetTorrentInfo(t.config.GetToken(), torrent.ID)
|
Version: t.requiredVersion,
|
||||||
if err != nil {
|
Torrent: *info,
|
||||||
t.log.Errorf("Cannot get info: %v\n", err)
|
SelectedFiles: nil,
|
||||||
return
|
ForRepair: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectedFiles is a subset of Files with only the selected ones
|
// SelectedFiles is a subset of Files with only the selected ones
|
||||||
@@ -321,23 +334,25 @@ func (t *TorrentManager) addMoreInfo(torrent *Torrent) {
|
|||||||
selectedFiles[i].Link = link
|
selectedFiles[i].Link = link
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// update file cache
|
|
||||||
torrent.OriginalName = info.OriginalName
|
|
||||||
torrent.Name = t.getName(torrent)
|
|
||||||
if len(selectedFiles) > 0 {
|
|
||||||
// update the torrent with more data!
|
|
||||||
torrent.SelectedFiles = selectedFiles
|
|
||||||
torrent.ForRepair = forRepair
|
torrent.ForRepair = forRepair
|
||||||
t.writeToFile(torrent)
|
torrent.SelectedFiles = selectedFiles
|
||||||
|
torrent.AccessKey = t.getName(info.Name, info.OriginalName)
|
||||||
|
|
||||||
|
// update file cache
|
||||||
|
if len(selectedFiles) > 0 {
|
||||||
|
t.writeToFile(&torrent)
|
||||||
}
|
}
|
||||||
|
t.log.Debugf("Got info for %s %s", torrent.ID, torrent.AccessKey)
|
||||||
|
return &torrent
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TorrentManager) getName(torrent *Torrent) string {
|
func (t *TorrentManager) getName(name, originalName string) string {
|
||||||
// drop the extension from the name
|
// drop the extension from the name
|
||||||
if t.config.EnableRetainFolderNameExtension() && strings.Contains(torrent.Name, torrent.OriginalName) {
|
if t.config.EnableRetainFolderNameExtension() && strings.Contains(name, originalName) {
|
||||||
return torrent.Name
|
return name
|
||||||
} else {
|
} else {
|
||||||
ret := strings.TrimSuffix(torrent.OriginalName, ".mp4")
|
ret := strings.TrimSuffix(originalName, ".mp4")
|
||||||
ret = strings.TrimSuffix(ret, ".mkv")
|
ret = strings.TrimSuffix(ret, ".mkv")
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
@@ -357,7 +372,7 @@ func (t *TorrentManager) mapToDirectories() {
|
|||||||
for i := range t.torrents {
|
for i := range t.torrents {
|
||||||
// don't process torrents that are already mapped if it is not the first run
|
// don't process torrents that are already mapped if it is not the first run
|
||||||
alreadyMappedToGroup := false
|
alreadyMappedToGroup := false
|
||||||
for _, mappedGroup := range t.processedTorrents[t.torrents[i].Name] {
|
for _, mappedGroup := range t.processedTorrents[t.torrents[i].AccessKey] {
|
||||||
if mappedGroup == group {
|
if mappedGroup == group {
|
||||||
alreadyMappedToGroup = true
|
alreadyMappedToGroup = true
|
||||||
}
|
}
|
||||||
@@ -371,10 +386,10 @@ func (t *TorrentManager) mapToDirectories() {
|
|||||||
for _, file := range t.torrents[i].SelectedFiles {
|
for _, file := range t.torrents[i].SelectedFiles {
|
||||||
filenames = append(filenames, file.Path)
|
filenames = append(filenames, file.Path)
|
||||||
}
|
}
|
||||||
if configV1.MeetsConditions(directory, t.torrents[i].ID, t.torrents[i].Name, filenames) {
|
if configV1.MeetsConditions(directory, t.torrents[i].ID, t.torrents[i].AccessKey, filenames) {
|
||||||
found := false
|
found := false
|
||||||
// check if it is already mapped to this directory
|
// check if it is already mapped to this directory
|
||||||
for _, dir := range t.directoryMap[t.torrents[i].Name] {
|
for _, dir := range t.directoryMap[t.torrents[i].AccessKey] {
|
||||||
if dir == directory {
|
if dir == directory {
|
||||||
found = true
|
found = true
|
||||||
break // it is already mapped to this directory
|
break // it is already mapped to this directory
|
||||||
@@ -383,14 +398,14 @@ func (t *TorrentManager) mapToDirectories() {
|
|||||||
if !found {
|
if !found {
|
||||||
counter[directory]++
|
counter[directory]++
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
t.directoryMap[t.torrents[i].Name] = append(t.directoryMap[t.torrents[i].Name], directory)
|
t.directoryMap[t.torrents[i].AccessKey] = append(t.directoryMap[t.torrents[i].AccessKey], directory)
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
break // we found a directory for this torrent, so we can stop looking for more
|
break // we found a directory for this torrent, so we can stop looking for more
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
t.processedTorrents[t.torrents[i].Name] = append(t.processedTorrents[t.torrents[i].Name], group)
|
t.processedTorrents[t.torrents[i].AccessKey] = append(t.processedTorrents[t.torrents[i].AccessKey], group)
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
}
|
}
|
||||||
sum := 0
|
sum := 0
|
||||||
@@ -408,6 +423,33 @@ func (t *TorrentManager) mapToDirectories() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TorrentManager) getDirectories(torrent *Torrent) []string {
|
||||||
|
var ret []string
|
||||||
|
// Map torrents to directories
|
||||||
|
switch t.config.GetVersion() {
|
||||||
|
case "v1":
|
||||||
|
configV1 := t.config.(*config.ZurgConfigV1)
|
||||||
|
groupMap := configV1.GetGroupMap()
|
||||||
|
// for every group, iterate over every torrent
|
||||||
|
// and then sprinkle/distribute the torrents to the directories of the group
|
||||||
|
for _, directories := range groupMap {
|
||||||
|
for _, directory := range directories {
|
||||||
|
var filenames []string
|
||||||
|
for _, file := range torrent.SelectedFiles {
|
||||||
|
filenames = append(filenames, file.Path)
|
||||||
|
}
|
||||||
|
if configV1.MeetsConditions(directory, torrent.ID, torrent.AccessKey, filenames) {
|
||||||
|
ret = append(ret, directory)
|
||||||
|
break // we found a directory for this torrent for this group, so we can stop looking for more
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
t.log.Error("Unknown config version")
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
// getByID returns a torrent by its ID
|
// getByID returns a torrent by its ID
|
||||||
func (t *TorrentManager) getByID(torrentID string) *Torrent {
|
func (t *TorrentManager) getByID(torrentID string) *Torrent {
|
||||||
for i := range t.torrents {
|
for i := range t.torrents {
|
||||||
@@ -463,19 +505,18 @@ func (t *TorrentManager) readFromFile(torrentID string) *Torrent {
|
|||||||
return &torrent
|
return &torrent
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TorrentManager) repairAll(wg *sync.WaitGroup) {
|
func (t *TorrentManager) repairAll() {
|
||||||
wg.Wait()
|
|
||||||
for _, torrent := range t.torrents {
|
for _, torrent := range t.torrents {
|
||||||
if torrent.ForRepair {
|
if torrent.ForRepair {
|
||||||
t.log.Infof("Issues were detected on %s %s; fixing...", torrent.ID, torrent.Name)
|
t.log.Infof("There were less links than was expected on %s %s; fixing...", torrent.ID, torrent.AccessKey)
|
||||||
t.repair(torrent.ID, torrent.SelectedFiles, true)
|
t.repair(torrent.ID, torrent.SelectedFiles, true)
|
||||||
}
|
}
|
||||||
if len(torrent.Links) == 0 && torrent.Progress == 100 {
|
if len(torrent.Links) == 0 && torrent.Progress == 100 {
|
||||||
// If the torrent has no links
|
// If the torrent has no links
|
||||||
// and already processing repair
|
// and already processing repair
|
||||||
// delete it!
|
// delete it!
|
||||||
t.log.Infof("Deleting broken torrent %s %s as it doesn't contain any files", torrent.ID, torrent.Name)
|
t.log.Infof("Deleting broken torrent %s %s as it doesn't contain any files", torrent.ID, torrent.AccessKey)
|
||||||
realdebrid.DeleteTorrent(t.config.GetToken(), torrent.ID)
|
t.rd.DeleteTorrent(torrent.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -518,12 +559,12 @@ func (t *TorrentManager) repair(torrentID string, selectedFiles []File, tryReins
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(missingFiles) == 0 {
|
if len(missingFiles) == 0 {
|
||||||
t.log.Infof("Torrent %s %s is already repaired", torrent.ID, torrent.Name)
|
t.log.Infof("Torrent %s %s is already repaired", torrent.ID, torrent.AccessKey)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// then we repair it!
|
// then we repair it!
|
||||||
t.log.Infof("Repairing torrent %s %s", torrent.ID, torrent.Name)
|
t.log.Infof("Repairing torrent %s %s", torrent.ID, torrent.AccessKey)
|
||||||
// check if we can still add more downloads
|
// check if we can still add more downloads
|
||||||
proceed := t.canCapacityHandle()
|
proceed := t.canCapacityHandle()
|
||||||
if !proceed {
|
if !proceed {
|
||||||
@@ -573,7 +614,7 @@ func (t *TorrentManager) repair(torrentID string, selectedFiles []File, tryReins
|
|||||||
t.log.Info("No other missing files left to reinsert")
|
t.log.Info("No other missing files left to reinsert")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t.log.Infof("Torrent %s %s is unfixable as the only link cached in RD is already broken", torrent.ID, torrent.Name)
|
t.log.Infof("Torrent %s %s is unfixable as the only link cached in RD is already broken", torrent.ID, torrent.AccessKey)
|
||||||
t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", torrent.Hash)
|
t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", torrent.Hash)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -584,7 +625,7 @@ func (t *TorrentManager) repair(torrentID string, selectedFiles []File, tryReins
|
|||||||
func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string, deleteIfFailed bool) bool {
|
func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string, deleteIfFailed bool) bool {
|
||||||
// if missingFiles is not provided, look for missing files
|
// if missingFiles is not provided, look for missing files
|
||||||
if missingFiles == "" {
|
if missingFiles == "" {
|
||||||
t.log.Info("Redownloading whole torrent", torrent.Name)
|
t.log.Info("Redownloading whole torrent", torrent.AccessKey)
|
||||||
var selection string
|
var selection string
|
||||||
for _, file := range torrent.SelectedFiles {
|
for _, file := range torrent.SelectedFiles {
|
||||||
selection += fmt.Sprintf("%d,", file.ID)
|
selection += fmt.Sprintf("%d,", file.ID)
|
||||||
@@ -598,29 +639,29 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// redownload torrent
|
// redownload torrent
|
||||||
resp, err := realdebrid.AddMagnetHash(t.config.GetToken(), torrent.Hash)
|
resp, err := t.rd.AddMagnetHash(torrent.Hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.log.Errorf("Cannot redownload torrent: %v", err)
|
t.log.Errorf("Cannot redownload torrent: %v", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
newTorrentID := resp.ID
|
newTorrentID := resp.ID
|
||||||
err = realdebrid.SelectTorrentFiles(t.config.GetToken(), newTorrentID, missingFiles)
|
err = t.rd.SelectTorrentFiles(newTorrentID, missingFiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.log.Errorf("Cannot start redownloading: %v", err)
|
t.log.Errorf("Cannot start redownloading: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if deleteIfFailed {
|
if deleteIfFailed {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
realdebrid.DeleteTorrent(t.config.GetToken(), newTorrentID)
|
t.rd.DeleteTorrent(newTorrentID)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
// see if the torrent is ready
|
// see if the torrent is ready
|
||||||
info, err := realdebrid.GetTorrentInfo(t.config.GetToken(), newTorrentID)
|
info, err := t.rd.GetTorrentInfo(newTorrentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.log.Errorf("Cannot get info on redownloaded torrent: %v", err)
|
t.log.Errorf("Cannot get info on redownloaded torrent: %v", err)
|
||||||
if deleteIfFailed {
|
if deleteIfFailed {
|
||||||
realdebrid.DeleteTorrent(t.config.GetToken(), newTorrentID)
|
t.rd.DeleteTorrent(newTorrentID)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -628,17 +669,17 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string,
|
|||||||
|
|
||||||
if info.Progress != 100 {
|
if info.Progress != 100 {
|
||||||
t.log.Infof("Torrent is not cached anymore so we have to wait until completion, currently %d%%", info.Progress)
|
t.log.Infof("Torrent is not cached anymore so we have to wait until completion, currently %d%%", info.Progress)
|
||||||
realdebrid.DeleteTorrent(t.config.GetToken(), newTorrentID)
|
t.rd.DeleteTorrent(newTorrentID)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(info.Links) != len(torrent.SelectedFiles) {
|
if len(info.Links) != len(torrent.SelectedFiles) {
|
||||||
t.log.Infof("It didn't fix the issue, only got %d files but we need %d, undoing", len(info.Links), len(torrent.SelectedFiles))
|
t.log.Infof("It didn't fix the issue, only got %d files but we need %d, undoing", len(info.Links), len(torrent.SelectedFiles))
|
||||||
realdebrid.DeleteTorrent(t.config.GetToken(), newTorrentID)
|
t.rd.DeleteTorrent(newTorrentID)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
t.log.Info("Redownload successful, deleting old torrent")
|
t.log.Info("Redownload successful, deleting old torrent")
|
||||||
realdebrid.DeleteTorrent(t.config.GetToken(), torrent.ID)
|
t.rd.DeleteTorrent(torrent.ID)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -652,43 +693,43 @@ func (t *TorrentManager) organizeChaos(info *realdebrid.Torrent, selectedFiles [
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
// Limit concurrency
|
// Limit concurrency
|
||||||
sem := make(chan struct{}, t.config.GetNumOfWorkers())
|
sem := make(chan bool, t.config.GetNumOfWorkers())
|
||||||
|
|
||||||
for _, link := range info.Links {
|
for _, link := range info.Links {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
sem <- struct{}{}
|
sem <- true
|
||||||
go func(lnk string) {
|
go func(lnk string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer func() { <-sem }()
|
defer func() { <-sem }()
|
||||||
|
resp := t.rd.UnrestrictUntilOk(lnk)
|
||||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
|
||||||
return realdebrid.UnrestrictCheck(t.config.GetToken(), lnk)
|
|
||||||
}
|
|
||||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
|
||||||
if resp != nil {
|
|
||||||
resultsChan <- Result{Response: resp}
|
resultsChan <- Result{Response: resp}
|
||||||
}
|
|
||||||
}(link)
|
}(link)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
t.log.Debugf("Checking %d link(s) for problematic torrent id=%s", len(info.Links), info.ID)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(sem)
|
close(sem)
|
||||||
close(resultsChan)
|
close(resultsChan)
|
||||||
|
t.log.Debugf("Closing results channel for torrent id=%s, checking...", info.ID)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
isChaotic := false
|
isChaotic := false
|
||||||
for result := range resultsChan {
|
for result := range resultsChan {
|
||||||
|
if result.Response == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
found := false
|
found := false
|
||||||
for i := range selectedFiles {
|
for i := range selectedFiles {
|
||||||
if strings.HasSuffix(selectedFiles[i].Path, result.Response.Filename) {
|
if strings.Contains(selectedFiles[i].Path, result.Response.Filename) {
|
||||||
|
t.log.Debugf("Found a file that is in the selection for torrent id=%s: %s", info.ID, result.Response.Filename)
|
||||||
selectedFiles[i].Link = result.Response.Link
|
selectedFiles[i].Link = result.Response.Link
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
// "chaos" file, we don't know where it belongs
|
isChaotic = result.Response.Streamable == 0
|
||||||
isChaotic = !isStreamable(result.Response.Filename)
|
t.log.Debugf("Found a file that is not in the selection for torrent id=%s: %s %v", info.ID, result.Response.Filename, result.Response.Streamable)
|
||||||
selectedFiles = append(selectedFiles, File{
|
selectedFiles = append(selectedFiles, File{
|
||||||
File: realdebrid.File{
|
File: realdebrid.File{
|
||||||
Path: result.Response.Filename,
|
Path: result.Response.Filename,
|
||||||
@@ -710,7 +751,7 @@ func (t *TorrentManager) canCapacityHandle() bool {
|
|||||||
const maxDelay = 60 * time.Second
|
const maxDelay = 60 * time.Second
|
||||||
retryCount := 0
|
retryCount := 0
|
||||||
for {
|
for {
|
||||||
count, err := realdebrid.GetActiveTorrentCount(t.config.GetToken())
|
count, err := t.rd.GetActiveTorrentCount()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.log.Errorf("Cannot get active downloads count: %v", err)
|
t.log.Errorf("Cannot get active downloads count: %v", err)
|
||||||
if retryCount >= maxRetries {
|
if retryCount >= maxRetries {
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
package torrent
|
package torrent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Torrent struct {
|
type Torrent struct {
|
||||||
|
AccessKey string
|
||||||
Version string
|
Version string
|
||||||
realdebrid.Torrent
|
realdebrid.Torrent
|
||||||
SelectedFiles []File
|
SelectedFiles []File
|
||||||
ForRepair bool
|
ForRepair bool
|
||||||
lock *sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
|
|||||||
@@ -14,15 +14,13 @@ import (
|
|||||||
intHttp "github.com/debridmediamanager.com/zurg/internal/http"
|
intHttp "github.com/debridmediamanager.com/zurg/internal/http"
|
||||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||||
"github.com/debridmediamanager.com/zurg/pkg/logutil"
|
"github.com/debridmediamanager.com/zurg/pkg/logutil"
|
||||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
|
||||||
"github.com/hashicorp/golang-lru/v2/expirable"
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HandleGetRequest handles a GET request universally for both WebDAV and HTTP
|
// HandleGetRequest handles a GET request universally for both WebDAV and HTTP
|
||||||
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
||||||
rlog := logutil.NewLogger()
|
log := logutil.NewLogger().Named("uniget")
|
||||||
log := rlog.Named("uniget")
|
|
||||||
|
|
||||||
requestPath := path.Clean(r.URL.Path)
|
requestPath := path.Clean(r.URL.Path)
|
||||||
isDav := true
|
isDav := true
|
||||||
@@ -44,7 +42,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if data, exists := cache.Get(requestPath); exists {
|
if data, exists := cache.Get(requestPath); exists {
|
||||||
streamFileToResponse(data, w, r, c, log)
|
streamFileToResponse(data, w, r, t, c, log)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,43 +66,40 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
|||||||
if file.Link == "" {
|
if file.Link == "" {
|
||||||
// This is a dead file, serve an alternate file
|
// This is a dead file, serve an alternate file
|
||||||
log.Errorf("File %s is no longer available", filename)
|
log.Errorf("File %s is no longer available", filename)
|
||||||
streamErrorVideo("https://www.youtube.com/watch?v=bGTqwt6vdcY", w, r, c, log)
|
streamErrorVideo("https://www.youtube.com/watch?v=bGTqwt6vdcY", w, r, t, c, log)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
link := file.Link
|
link := file.Link
|
||||||
|
|
||||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
resp := t.UnrestrictUntilOk(link)
|
||||||
return realdebrid.UnrestrictLink(c.GetToken(), link)
|
|
||||||
}
|
|
||||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
|
||||||
if resp == nil {
|
if resp == nil {
|
||||||
if !file.Unavailable {
|
if !file.Unavailable {
|
||||||
log.Errorf("Cannot unrestrict file %s %s", filename, link)
|
log.Errorf("Cannot unrestrict file %s %s", filename, link)
|
||||||
t.HideTheFile(torrent, file)
|
t.HideTheFile(torrent, file)
|
||||||
}
|
}
|
||||||
streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, c, log)
|
streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log)
|
||||||
return
|
return
|
||||||
} else if resp.Filename != filename {
|
} else if resp.Filename != filename {
|
||||||
actualExt := filepath.Ext(resp.Filename)
|
actualExt := filepath.Ext(resp.Filename)
|
||||||
expectedExt := filepath.Ext(filename)
|
expectedExt := filepath.Ext(filename)
|
||||||
if actualExt != expectedExt && resp.Streamable != 1 {
|
if actualExt != expectedExt && resp.Streamable != 1 {
|
||||||
log.Errorf("File extension mismatch: %s and %s", filename, resp.Filename)
|
log.Errorf("File extension mismatch: %s and %s", filename, resp.Filename)
|
||||||
streamErrorVideo("https://www.youtube.com/watch?v=t9VgOriBHwE", w, r, c, log)
|
streamErrorVideo("https://www.youtube.com/watch?v=t9VgOriBHwE", w, r, t, c, log)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
log.Errorf("Filename mismatch: %s and %s", filename, resp.Filename)
|
log.Errorf("Filename mismatch: %s and %s", filename, resp.Filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cache.Add(requestPath, resp.Download)
|
cache.Add(requestPath, resp.Download)
|
||||||
streamFileToResponse(resp.Download, w, r, c, log)
|
streamFileToResponse(resp.Download, w, r, t, c, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, c config.ConfigInterface, log *zap.SugaredLogger) {
|
func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) {
|
||||||
// Create a new request for the file download.
|
// Create a new request for the file download.
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error creating new request: %v", err)
|
log.Errorf("Error creating new request: %v", err)
|
||||||
streamErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, c, log)
|
streamErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, t, c, log)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,19 +149,16 @@ func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, c
|
|||||||
// Perform the operation with the retry policy.
|
// Perform the operation with the retry policy.
|
||||||
if err := backoff.Retry(operation, retryPolicy); err != nil {
|
if err := backoff.Retry(operation, retryPolicy); err != nil {
|
||||||
log.Errorf("Failed after retries: %v", err)
|
log.Errorf("Failed after retries: %v", err)
|
||||||
streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, c, log)
|
streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, t, c, log)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, c config.ConfigInterface, log *zap.SugaredLogger) {
|
func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) {
|
||||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
resp := t.UnrestrictUntilOk(link)
|
||||||
return realdebrid.UnrestrictLink(c.GetToken(), link)
|
|
||||||
}
|
|
||||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
|
||||||
if resp == nil {
|
if resp == nil {
|
||||||
http.Error(w, "REAL-DEBRID IS DOWN", http.StatusInternalServerError)
|
http.Error(w, "REAL-DEBRID IS DOWN", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
streamFileToResponse(resp.Download, w, r, c, log)
|
streamFileToResponse(resp.Download, w, r, t, c, log)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
||||||
rlog := logutil.NewLogger()
|
log := logutil.NewLogger().Named("unihead")
|
||||||
log := rlog.Named("unihead")
|
|
||||||
|
|
||||||
requestPath := path.Clean(r.URL.Path)
|
requestPath := path.Clean(r.URL.Path)
|
||||||
requestPath = strings.Replace(requestPath, "/http", "", 1)
|
requestPath = strings.Replace(requestPath, "/http", "", 1)
|
||||||
|
|||||||
51
pkg/http/client.go
Normal file
51
pkg/http/client.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HTTPClient struct {
|
||||||
|
Client *http.Client
|
||||||
|
MaxRetries int
|
||||||
|
Backoff func(attempt int) time.Duration
|
||||||
|
CheckRespStatus func(resp *http.Response, err error) bool
|
||||||
|
BearerToken string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||||
|
if r.BearerToken != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+r.BearerToken)
|
||||||
|
}
|
||||||
|
var resp *http.Response
|
||||||
|
var err error
|
||||||
|
for attempt := 0; attempt < r.MaxRetries; attempt++ {
|
||||||
|
resp, err = r.Client.Do(req)
|
||||||
|
if !r.CheckRespStatus(resp, err) {
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
time.Sleep(r.Backoff(attempt))
|
||||||
|
}
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTPClient(token string, maxRetries int, timeout time.Duration) *HTTPClient {
|
||||||
|
return &HTTPClient{
|
||||||
|
BearerToken: token,
|
||||||
|
Client: &http.Client{Timeout: timeout},
|
||||||
|
MaxRetries: maxRetries,
|
||||||
|
Backoff: func(attempt int) time.Duration {
|
||||||
|
return time.Duration(attempt) * time.Second
|
||||||
|
},
|
||||||
|
CheckRespStatus: func(resp *http.Response, err error) bool {
|
||||||
|
if err != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// no need to retry because the status code is 2XX
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,94 +3,77 @@ package realdebrid
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UnrestrictCheck(accessToken, link string) (*UnrestrictResponse, error) {
|
type RealDebrid struct {
|
||||||
|
log *zap.SugaredLogger
|
||||||
|
client *zurghttp.HTTPClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRealDebrid(accessToken string, log *zap.SugaredLogger) *RealDebrid {
|
||||||
|
maxRetries := 10
|
||||||
|
timeout := 10 * time.Second
|
||||||
|
client := zurghttp.NewHTTPClient(accessToken, maxRetries, timeout)
|
||||||
|
log.Debugf("Created an HTTP client with %d max retries and %s timeout", maxRetries, timeout)
|
||||||
|
return &RealDebrid{
|
||||||
|
log: log,
|
||||||
|
client: client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rd *RealDebrid) UnrestrictCheck(link string) (*UnrestrictResponse, error) {
|
||||||
data := url.Values{}
|
data := url.Values{}
|
||||||
data.Set("link", link)
|
data.Set("link", link)
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/check", bytes.NewBufferString(data.Encode()))
|
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/check", bytes.NewBufferString(data.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when creating a unrestrict check request: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
client := &http.Client{}
|
resp, err := rd.client.Do(req)
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when executing the unrestrict check request: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when reading the body of unrestrict check response: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
rd.log.Errorf("Received a %s from unrestrict check endpoint", resp.Status)
|
||||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
var response UnrestrictResponse
|
var response UnrestrictResponse
|
||||||
err = json.Unmarshal(body, &response)
|
err = json.Unmarshal(body, &response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when decoding unrestrict check JSON: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &response, nil
|
rd.log.Info("Link %s is streamable? %v", response.Streamable)
|
||||||
}
|
|
||||||
|
|
||||||
func UnrestrictLink(accessToken, link string) (*UnrestrictResponse, error) {
|
|
||||||
data := url.Values{}
|
|
||||||
data.Set("link", link)
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/link", bytes.NewBufferString(data.Encode()))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
var response UnrestrictResponse
|
|
||||||
err = json.Unmarshal(body, &response)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !canFetchFirstByte(response.Download) {
|
|
||||||
return nil, fmt.Errorf("can't fetch first byte")
|
|
||||||
}
|
|
||||||
return &response, nil
|
return &response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTorrents returns all torrents, paginated
|
// GetTorrents returns all torrents, paginated
|
||||||
// if customLimit is 0, the default limit of 2500 is used
|
// if customLimit is 0, the default limit of 2500 is used
|
||||||
func GetTorrents(accessToken string, customLimit int) ([]Torrent, int, error) {
|
func (rd *RealDebrid) GetTorrents(customLimit int) ([]Torrent, int, error) {
|
||||||
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
|
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
|
||||||
var allTorrents []Torrent
|
var allTorrents []Torrent
|
||||||
page := 1
|
page := 1
|
||||||
@@ -110,19 +93,20 @@ func GetTorrents(accessToken string, customLimit int) ([]Torrent, int, error) {
|
|||||||
|
|
||||||
req, err := http.NewRequest("GET", reqURL, nil)
|
req, err := http.NewRequest("GET", reqURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when creating a get torrents request: %v", err)
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
resp, err := rd.client.Do(req)
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when executing the get torrents request: %v", err)
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
// if status code is not 2xx, return error
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
rd.log.Errorf("Received a %s from get torrents endpoint", resp.Status)
|
||||||
return nil, 0, fmt.Errorf("HTTP error: %s", resp.Status)
|
return nil, 0, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,6 +114,7 @@ func GetTorrents(accessToken string, customLimit int) ([]Torrent, int, error) {
|
|||||||
decoder := json.NewDecoder(resp.Body)
|
decoder := json.NewDecoder(resp.Body)
|
||||||
err = decoder.Decode(&torrents)
|
err = decoder.Decode(&torrents)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when decoding get torrents JSON: %v", err)
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,126 +132,106 @@ func GetTorrents(accessToken string, customLimit int) ([]Torrent, int, error) {
|
|||||||
|
|
||||||
page++
|
page++
|
||||||
}
|
}
|
||||||
|
|
||||||
return allTorrents, totalCount, nil
|
return allTorrents, totalCount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTorrentInfo(accessToken, id string) (*Torrent, error) {
|
func (rd *RealDebrid) GetTorrentInfo(id string) (*Torrent, error) {
|
||||||
url := "https://api.real-debrid.com/rest/1.0/torrents/info/" + id
|
url := "https://api.real-debrid.com/rest/1.0/torrents/info/" + id
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when creating a get info request: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
resp, err := rd.client.Do(req)
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when executing the get info request: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when reading the body of get info response: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
rd.log.Errorf("Received a %s from get info endpoint", resp.Status)
|
||||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
var response Torrent
|
var response Torrent
|
||||||
err = json.Unmarshal(body, &response)
|
err = json.Unmarshal(body, &response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when : %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rd.log.Debugf("Fetched info for torrent id=%s", response.ID)
|
||||||
return &response, nil
|
return &response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectTorrentFiles selects files of a torrent to start it.
|
// SelectTorrentFiles selects files of a torrent to start it.
|
||||||
func SelectTorrentFiles(accessToken string, id string, files string) error {
|
func (rd *RealDebrid) SelectTorrentFiles(id string, files string) error {
|
||||||
// Prepare request data
|
|
||||||
data := url.Values{}
|
data := url.Values{}
|
||||||
data.Set("files", files)
|
data.Set("files", files)
|
||||||
|
|
||||||
// Construct request URL
|
|
||||||
reqURL := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/selectFiles/%s", id)
|
reqURL := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/selectFiles/%s", id)
|
||||||
req, err := http.NewRequest("POST", reqURL, bytes.NewBufferString(data.Encode()))
|
req, err := http.NewRequest("POST", reqURL, bytes.NewBufferString(data.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when creating a select files request: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set request headers
|
|
||||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
// Send the request
|
resp, err := rd.client.Do(req)
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when executing the select files request: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Handle response status codes
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
switch resp.StatusCode {
|
rd.log.Errorf("Received a %s from select files endpoint", resp.Status)
|
||||||
case http.StatusOK, http.StatusNoContent:
|
return fmt.Errorf("HTTP error: %s", resp.Status)
|
||||||
return nil // Success
|
|
||||||
case http.StatusAccepted:
|
|
||||||
return errors.New("action already done")
|
|
||||||
case http.StatusBadRequest:
|
|
||||||
return errors.New("bad request")
|
|
||||||
case http.StatusUnauthorized:
|
|
||||||
return errors.New("bad token (expired or invalid)")
|
|
||||||
case http.StatusForbidden:
|
|
||||||
return errors.New("permission denied (account locked or not premium)")
|
|
||||||
case http.StatusNotFound:
|
|
||||||
return errors.New("wrong parameter (invalid file id(s)) or unknown resource (invalid id)")
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("unexpected HTTP error: %s", resp.Status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rd.log.Debugf("Selected files %s for torrent id=%s", len(strings.Split(files, ",")), id)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteTorrent deletes a torrent from the torrents list.
|
// DeleteTorrent deletes a torrent from the torrents list.
|
||||||
func DeleteTorrent(accessToken string, id string) error {
|
func (rd *RealDebrid) DeleteTorrent(id string) error {
|
||||||
// Construct request URL
|
// Construct request URL
|
||||||
reqURL := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/delete/%s", id)
|
reqURL := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/delete/%s", id)
|
||||||
req, err := http.NewRequest("DELETE", reqURL, nil)
|
req, err := http.NewRequest("DELETE", reqURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when creating a delete torrent request: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set request headers
|
|
||||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
client := &http.Client{}
|
resp, err := rd.client.Do(req)
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when executing the delete torrent request: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Handle response status codes
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
switch resp.StatusCode {
|
rd.log.Errorf("Received a %s from delete torrent endpoint", resp.Status)
|
||||||
case http.StatusNoContent:
|
return fmt.Errorf("HTTP error: %s", resp.Status)
|
||||||
return nil // Success
|
|
||||||
case http.StatusUnauthorized:
|
|
||||||
return errors.New("bad token (expired or invalid)")
|
|
||||||
case http.StatusForbidden:
|
|
||||||
return errors.New("permission denied (account locked)")
|
|
||||||
case http.StatusNotFound:
|
|
||||||
return errors.New("unknown resource")
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("unexpected HTTP error: %s", resp.Status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rd.log.Debugf("Deleted torrent with id=%s", id)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddMagnetHash adds a magnet link to download.
|
// AddMagnetHash adds a magnet link to download.
|
||||||
func AddMagnetHash(accessToken, magnet string) (*MagnetResponse, error) {
|
func (rd *RealDebrid) AddMagnetHash(magnet string) (*MagnetResponse, error) {
|
||||||
// Prepare request data
|
// Prepare request data
|
||||||
data := url.Values{}
|
data := url.Values{}
|
||||||
data.Set("magnet", fmt.Sprintf("magnet:?xt=urn:btih:%s", magnet))
|
data.Set("magnet", fmt.Sprintf("magnet:?xt=urn:btih:%s", magnet))
|
||||||
@@ -275,77 +240,110 @@ func AddMagnetHash(accessToken, magnet string) (*MagnetResponse, error) {
|
|||||||
reqURL := "https://api.real-debrid.com/rest/1.0/torrents/addMagnet"
|
reqURL := "https://api.real-debrid.com/rest/1.0/torrents/addMagnet"
|
||||||
req, err := http.NewRequest("POST", reqURL, bytes.NewBufferString(data.Encode()))
|
req, err := http.NewRequest("POST", reqURL, bytes.NewBufferString(data.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when creating an add magnet request: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set request headers
|
|
||||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
client := &http.Client{}
|
resp, err := rd.client.Do(req)
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when executing the add magnet request: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Handle response status codes
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
switch resp.StatusCode {
|
rd.log.Errorf("Received a %s from add magnet endpoint", resp.Status)
|
||||||
case http.StatusCreated:
|
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
var response MagnetResponse
|
var response MagnetResponse
|
||||||
err := json.NewDecoder(resp.Body).Decode(&response)
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when decoding add magnet JSON: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rd.log.Debugf("Added magnet %s with id=%s", magnet, response.ID)
|
||||||
return &response, nil
|
return &response, nil
|
||||||
case http.StatusBadRequest:
|
|
||||||
return nil, errors.New("bad request")
|
|
||||||
case http.StatusUnauthorized:
|
|
||||||
return nil, errors.New("bad token (expired or invalid)")
|
|
||||||
case http.StatusForbidden:
|
|
||||||
return nil, errors.New("permission denied (account locked or not premium)")
|
|
||||||
case http.StatusServiceUnavailable:
|
|
||||||
return nil, errors.New("service unavailable")
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unexpected HTTP error: %s", resp.Status)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetActiveTorrentCount gets the number of currently active torrents and the current maximum limit.
|
// GetActiveTorrentCount gets the number of currently active torrents and the current maximum limit.
|
||||||
func GetActiveTorrentCount(accessToken string) (*ActiveTorrentCountResponse, error) {
|
func (rd *RealDebrid) GetActiveTorrentCount() (*ActiveTorrentCountResponse, error) {
|
||||||
// Construct request URL
|
// Construct request URL
|
||||||
reqURL := "https://api.real-debrid.com/rest/1.0/torrents/activeCount"
|
reqURL := "https://api.real-debrid.com/rest/1.0/torrents/activeCount"
|
||||||
req, err := http.NewRequest("GET", reqURL, nil)
|
req, err := http.NewRequest("GET", reqURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when creating a active torrents request: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set request headers
|
|
||||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
client := &http.Client{}
|
resp, err := rd.client.Do(req)
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when executing the active torrents request: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Handle response status codes
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
switch resp.StatusCode {
|
rd.log.Errorf("Received a %s from active torrents endpoint", resp.Status)
|
||||||
case http.StatusOK:
|
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
var response ActiveTorrentCountResponse
|
var response ActiveTorrentCountResponse
|
||||||
err := json.NewDecoder(resp.Body).Decode(&response)
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when decoding active torrents JSON: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &response, nil
|
return &response, nil
|
||||||
case http.StatusUnauthorized:
|
|
||||||
return nil, errors.New("bad token (expired or invalid)")
|
|
||||||
case http.StatusForbidden:
|
|
||||||
return nil, errors.New("permission denied (account locked)")
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unexpected HTTP error: %s", resp.Status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rd *RealDebrid) UnrestrictLink(link string) (*UnrestrictResponse, error) {
|
||||||
|
data := url.Values{}
|
||||||
|
data.Set("link", link)
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/link", bytes.NewBufferString(data.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when creating a unrestrict link request: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when executing the unrestrict link request: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when reading the body of unrestrict link response: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
rd.log.Errorf("Received a %s from unrestrict link endpoint", resp.Status)
|
||||||
|
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response UnrestrictResponse
|
||||||
|
err = json.Unmarshal(body, &response)
|
||||||
|
if err != nil {
|
||||||
|
rd.log.Errorf("Error when decoding unrestrict link JSON: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !canFetchFirstByte(response.Download) {
|
||||||
|
return nil, fmt.Errorf("can't fetch first byte")
|
||||||
|
}
|
||||||
|
|
||||||
|
rd.log.Debugf("Unrestricted link %s into %s", link, response.Download)
|
||||||
|
return &response, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ type Torrent struct {
|
|||||||
Progress int `json:"-"`
|
Progress int `json:"-"`
|
||||||
Added string `json:"added"`
|
Added string `json:"added"`
|
||||||
Bytes int64 `json:"bytes"`
|
Bytes int64 `json:"bytes"`
|
||||||
|
OriginalBytes int64 `json:"original_bytes"`
|
||||||
Links []string `json:"links"`
|
Links []string `json:"links"`
|
||||||
Files []File `json:"files,omitempty"`
|
Files []File `json:"files,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RetryUntilOk[T any](fn func() (T, error)) T {
|
func (rd *RealDebrid) UnrestrictUntilOk(link string) *UnrestrictResponse {
|
||||||
|
unrestrictFn := func() (*UnrestrictResponse, error) {
|
||||||
|
return rd.UnrestrictLink(link)
|
||||||
|
}
|
||||||
|
return retryUntilOk(unrestrictFn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func retryUntilOk[T any](fn func() (T, error)) T {
|
||||||
const initialDelay = 1 * time.Second
|
const initialDelay = 1 * time.Second
|
||||||
const maxDelay = 128 * time.Second
|
const maxDelay = 128 * time.Second
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
Reference in New Issue
Block a user