- Fix alldebrid bug

- Minor cleanup
- speedgains
This commit is contained in:
Mukhtar Akere
2025-02-19 01:20:05 +01:00
parent 325e6c912c
commit 9a7bff04ef
9 changed files with 27 additions and 60 deletions

View File

@@ -1,49 +0,0 @@
package common
import (
"path/filepath"
"regexp"
"strings"
)
var (
VIDEOMATCH = "(?i)(\\.)(YUV|WMV|WEBM|VOB|VIV|SVI|ROQ|RMVB|RM|OGV|OGG|NSV|MXF|MPG|MPEG|M2V|MP2|MPE|MPV|MP4|M4P|M4V|MOV|QT|MNG|MKV|FLV|DRC|AVI|ASF|AMV|MKA|F4V|3GP|3G2|DIVX|X264|X265)$"
MUSICMATCH = "(?i)(\\.)(?:MP3|WAV|FLAC|AAC|OGG|WMA|AIFF|ALAC|M4A|APE|AC3|DTS|M4P|MID|MIDI|MKA|MP2|MPA|RA|VOC|WV|AMR)$"
)
var SAMPLEMATCH = `(?i)(^|[\\/]|[._-])(sample|trailer|thumb)s?([._-]|$)`
func RegexMatch(regex string, value string) bool {
re := regexp.MustCompile(regex)
return re.MatchString(value)
}
func RemoveInvalidChars(value string) string {
return strings.Map(func(r rune) rune {
if r == filepath.Separator || r == ':' {
return r
}
if filepath.IsAbs(string(r)) {
return r
}
if strings.ContainsRune(filepath.VolumeName("C:"+string(r)), r) {
return r
}
if r < 32 || strings.ContainsRune(`<>:"/\|?*`, r) {
return -1
}
return r
}, value)
}
func RemoveExtension(value string) string {
re := regexp.MustCompile(VIDEOMATCH + "|" + MUSICMATCH)
// Find the last index of the matched extension
loc := re.FindStringIndex(value)
if loc != nil {
return value[:loc[0]]
} else {
return value
}
}

View File

@@ -118,6 +118,7 @@ func (c *RLHTTPClient) MakeRequest(req *http.Request) ([]byte, error) {
func NewRLHTTPClient(rl *rate.Limiter, headers map[string]string) *RLHTTPClient { func NewRLHTTPClient(rl *rate.Limiter, headers map[string]string) *RLHTTPClient {
tr := &http.Transport{ tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Proxy: http.ProxyFromEnvironment,
} }
c := &RLHTTPClient{ c := &RLHTTPClient{
client: &http.Client{ client: &http.Client{

View File

@@ -47,3 +47,12 @@ func RemoveExtension(value string) string {
return value return value
} }
} }
func IsMediaFile(path string) bool {
mediaPattern := VIDEOMATCH + "|" + MUSICMATCH
return RegexMatch(mediaPattern, path)
}
func IsSampleFile(path string) bool {
return RegexMatch(SAMPLEMATCH, path)
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/sirrobot01/debrid-blackhole/internal/config" "github.com/sirrobot01/debrid-blackhole/internal/config"
"github.com/sirrobot01/debrid-blackhole/internal/logger" "github.com/sirrobot01/debrid-blackhole/internal/logger"
"github.com/sirrobot01/debrid-blackhole/internal/request" "github.com/sirrobot01/debrid-blackhole/internal/request"
"github.com/sirrobot01/debrid-blackhole/internal/utils"
"github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent" "github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent"
"net/http" "net/http"
@@ -106,7 +107,9 @@ func flattenFiles(files []MagnetFile, parentPath string, index *int) []torrent.F
} else { } else {
// This is a file // This is a file
fileName := filepath.Base(f.Name) fileName := filepath.Base(f.Name)
if common.RegexMatch(common.SAMPLEMATCH, fileName) {
// Skip sample files
if utils.IsSampleFile(fileName) {
continue continue
} }
if !cfg.IsAllowedFile(fileName) { if !cfg.IsAllowedFile(fileName) {

View File

@@ -9,6 +9,7 @@ import (
"github.com/sirrobot01/debrid-blackhole/internal/config" "github.com/sirrobot01/debrid-blackhole/internal/config"
"github.com/sirrobot01/debrid-blackhole/internal/logger" "github.com/sirrobot01/debrid-blackhole/internal/logger"
"github.com/sirrobot01/debrid-blackhole/internal/request" "github.com/sirrobot01/debrid-blackhole/internal/request"
"github.com/sirrobot01/debrid-blackhole/internal/utils"
"github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent" "github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent"
"net/http" "net/http"
@@ -124,7 +125,7 @@ func (dl *DebridLink) GetTorrent(id string) (*torrent.Torrent, error) {
if data.Status == 100 { if data.Status == 100 {
status = "downloaded" status = "downloaded"
} }
name := common.RemoveInvalidChars(data.Name) name := utils.RemoveInvalidChars(data.Name)
t.Id = data.ID t.Id = data.ID
t.Name = name t.Name = name
t.Bytes = data.TotalSize t.Bytes = data.TotalSize
@@ -171,7 +172,7 @@ func (dl *DebridLink) SubmitMagnet(t *torrent.Torrent) (*torrent.Torrent, error)
} }
data := *res.Value data := *res.Value
status := "downloading" status := "downloading"
name := common.RemoveInvalidChars(data.Name) name := utils.RemoveInvalidChars(data.Name)
t.Id = data.ID t.Id = data.ID
t.Name = name t.Name = name
t.Bytes = data.TotalSize t.Bytes = data.TotalSize

View File

@@ -171,7 +171,7 @@ func (r *RealDebrid) GetTorrent(id string) (*torrent.Torrent, error) {
if err != nil { if err != nil {
return t, err return t, err
} }
name := common.RemoveInvalidChars(data.OriginalFilename) name := utils.RemoveInvalidChars(data.OriginalFilename)
t.Id = id t.Id = id
t.Name = name t.Name = name
t.Bytes = data.Bytes t.Bytes = data.Bytes
@@ -203,7 +203,7 @@ func (r *RealDebrid) CheckStatus(t *torrent.Torrent, isSymlink bool) (*torrent.T
var data TorrentInfo var data TorrentInfo
err = json.Unmarshal(resp, &data) err = json.Unmarshal(resp, &data)
status := data.Status status := data.Status
name := common.RemoveInvalidChars(data.OriginalFilename) name := utils.RemoveInvalidChars(data.OriginalFilename)
t.Name = name // Important because some magnet changes the name t.Name = name // Important because some magnet changes the name
t.Folder = name t.Folder = name
t.Filename = data.Filename t.Filename = data.Filename

View File

@@ -9,6 +9,7 @@ import (
"github.com/sirrobot01/debrid-blackhole/internal/config" "github.com/sirrobot01/debrid-blackhole/internal/config"
"github.com/sirrobot01/debrid-blackhole/internal/logger" "github.com/sirrobot01/debrid-blackhole/internal/logger"
"github.com/sirrobot01/debrid-blackhole/internal/request" "github.com/sirrobot01/debrid-blackhole/internal/request"
"github.com/sirrobot01/debrid-blackhole/internal/utils"
"github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent" "github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent"
"mime/multipart" "mime/multipart"
@@ -180,7 +181,7 @@ func (tb *Torbox) GetTorrent(id string) (*torrent.Torrent, error) {
cfg := config.GetConfig() cfg := config.GetConfig()
for _, f := range data.Files { for _, f := range data.Files {
fileName := filepath.Base(f.Name) fileName := filepath.Base(f.Name)
if common.RegexMatch(common.SAMPLEMATCH, fileName) { if utils.IsSampleFile(fileName) {
// Skip sample files // Skip sample files
continue continue
} }

View File

@@ -69,7 +69,7 @@ func (t *Torrent) GetMountFolder(rClonePath string) (string, error) {
possiblePaths := []string{ possiblePaths := []string{
t.OriginalFilename, t.OriginalFilename,
t.Filename, t.Filename,
common.RemoveExtension(t.OriginalFilename), utils.RemoveExtension(t.OriginalFilename),
} }
for _, path := range possiblePaths { for _, path := range possiblePaths {

View File

@@ -4,7 +4,7 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/cavaliergopher/grab/v3" "github.com/cavaliergopher/grab/v3"
"github.com/sirrobot01/debrid-blackhole/common" "github.com/sirrobot01/debrid-blackhole/internal/utils"
debrid "github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent" debrid "github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent"
"net/http" "net/http"
"os" "os"
@@ -52,7 +52,8 @@ Loop:
func (q *QBit) ProcessManualFile(torrent *Torrent) (string, error) { func (q *QBit) ProcessManualFile(torrent *Torrent) (string, error) {
debridTorrent := torrent.DebridTorrent debridTorrent := torrent.DebridTorrent
q.logger.Info().Msgf("Downloading %d files...", len(debridTorrent.DownloadLinks)) q.logger.Info().Msgf("Downloading %d files...", len(debridTorrent.DownloadLinks))
torrentPath := common.RemoveInvalidChars(filepath.Join(q.DownloadFolder, debridTorrent.Arr.Name, common.RemoveExtension(debridTorrent.OriginalFilename))) torrentPath := filepath.Join(q.DownloadFolder, debridTorrent.Arr.Name, utils.RemoveExtension(debridTorrent.OriginalFilename))
torrentPath = utils.RemoveInvalidChars(torrentPath)
err := os.MkdirAll(torrentPath, os.ModePerm) err := os.MkdirAll(torrentPath, os.ModePerm)
if err != nil { if err != nil {
// add previous error to the error and return // add previous error to the error and return
@@ -147,9 +148,9 @@ func (q *QBit) ProcessSymlink(torrent *Torrent) (string, error) {
} }
// Check if the torrent path is a file // Check if the torrent path is a file
torrentRclonePath := filepath.Join(rCloneBase, torrentPath) // leave it as is torrentRclonePath := filepath.Join(rCloneBase, torrentPath) // leave it as is
if debridTorrent.Debrid == "alldebrid" && len(files) == 1 { if debridTorrent.Debrid == "alldebrid" && utils.IsMediaFile(torrentPath) {
// Alldebrid hotfix for single file torrents // Alldebrid hotfix for single file torrents
torrentFolder = common.RemoveExtension(torrentFolder) torrentFolder = utils.RemoveExtension(torrentFolder)
torrentRclonePath = rCloneBase // /mnt/rclone/magnets/ // Remove the filename since it's in the root folder torrentRclonePath = rCloneBase // /mnt/rclone/magnets/ // Remove the filename since it's in the root folder
} }
torrentSymlinkPath := filepath.Join(q.DownloadFolder, debridTorrent.Arr.Name, torrentFolder) // /mnt/symlinks/{category}/MyTVShow/ torrentSymlinkPath := filepath.Join(q.DownloadFolder, debridTorrent.Arr.Name, torrentFolder) // /mnt/symlinks/{category}/MyTVShow/