fixes here and there

This commit is contained in:
Ben Sarmiento
2023-11-11 02:34:46 +01:00
parent 147c0bd444
commit cd96c7bd38
14 changed files with 181 additions and 155 deletions

View File

@@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"github.com/debridmediamanager.com/zurg/internal/config"
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
"go.uber.org/zap"
)
@@ -19,9 +20,9 @@ type RealDebrid struct {
client *zurghttp.HTTPClient
}
func NewRealDebrid(accessToken string, log *zap.SugaredLogger) *RealDebrid {
func NewRealDebrid(accessToken string, config config.ConfigInterface, log *zap.SugaredLogger) *RealDebrid {
maxRetries := 10
client := zurghttp.NewHTTPClient(accessToken, maxRetries)
client := zurghttp.NewHTTPClient(accessToken, maxRetries, nil)
return &RealDebrid{
log: log,
client: client,
@@ -52,11 +53,6 @@ func (rd *RealDebrid) UnrestrictCheck(link string) (*UnrestrictResponse, error)
return nil, err
}
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)
}
var response UnrestrictResponse
err = json.Unmarshal(body, &response)
if err != nil {
@@ -101,11 +97,7 @@ func (rd *RealDebrid) GetTorrents(customLimit int) ([]Torrent, int, error) {
}
defer resp.Body.Close()
// 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)
}
// if status code is not 2xx, return erro
var torrents []Torrent
decoder := json.NewDecoder(resp.Body)
@@ -154,11 +146,6 @@ func (rd *RealDebrid) GetTorrentInfo(id string) (*TorrentInfo, error) {
return nil, err
}
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)
}
var response TorrentInfo
err = json.Unmarshal(body, &response)
if err != nil {
@@ -191,11 +178,6 @@ func (rd *RealDebrid) SelectTorrentFiles(id string, files string) error {
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from select files endpoint", resp.Status)
return fmt.Errorf("HTTP error: %s", resp.Status)
}
rd.log.Debugf("Selected files %s for torrent id=%s", len(strings.Split(files, ",")), id)
return nil
}
@@ -218,11 +200,6 @@ func (rd *RealDebrid) DeleteTorrent(id string) error {
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from delete torrent endpoint", resp.Status)
return fmt.Errorf("HTTP error: %s", resp.Status)
}
rd.log.Debugf("Deleted torrent with id=%s", id)
return nil
}
@@ -251,11 +228,6 @@ func (rd *RealDebrid) AddMagnetHash(magnet string) (*MagnetResponse, error) {
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from add magnet endpoint", resp.Status)
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
var response MagnetResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
@@ -285,11 +257,6 @@ func (rd *RealDebrid) GetActiveTorrentCount() (*ActiveTorrentCountResponse, erro
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from active torrents endpoint", resp.Status)
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
var response ActiveTorrentCountResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
@@ -324,11 +291,6 @@ func (rd *RealDebrid) UnrestrictLink(link string) (*UnrestrictResponse, error) {
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 {
@@ -340,6 +302,6 @@ func (rd *RealDebrid) UnrestrictLink(link string) (*UnrestrictResponse, error) {
return nil, fmt.Errorf("can't fetch first byte")
}
rd.log.Debugf("Unrestricted link %s into %s", link, response.Download)
// rd.log.Debugf("Unrestricted link %s into %s", link, response.Download)
return &response, nil
}