fix(filters): RED and OPS lossless parsing and filtering (#1373)

* fix(filters): RED and OPS lossless parsing and filtering

* fix(filters): logscore and EP parsing

* fix(filters): tests

* fix(filters): tests

* feat(definitions): RED parse title variable

* feat(indexers): setup indexer to filter tests

* feat(indexers): tests and improve parsing

* feat(indexers): improve tests
This commit is contained in:
ze0s 2024-01-28 22:03:25 +01:00 committed by GitHub
parent 9db5a8b116
commit 5328078b32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 1093 additions and 360 deletions

View file

@ -4,15 +4,8 @@
package domain
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
@ -40,6 +33,7 @@ func TestRelease_Parse(t *testing.T) {
HDR: []string{"DV"},
Group: "FLUX",
//Website: "ATVP",
Type: "series",
},
},
{
@ -59,6 +53,7 @@ func TestRelease_Parse(t *testing.T) {
AudioChannels: "5.1",
HDR: []string{"DV"},
Group: "FLUX",
Type: "series",
},
},
{
@ -81,6 +76,7 @@ func TestRelease_Parse(t *testing.T) {
AudioChannels: "5.1",
HDR: []string{"DV"},
Group: "FLUX",
Type: "series",
},
},
{
@ -103,6 +99,7 @@ func TestRelease_Parse(t *testing.T) {
AudioChannels: "5.1",
HDR: []string{"DV"},
Group: "FLUX",
Type: "series",
},
},
{
@ -125,6 +122,7 @@ func TestRelease_Parse(t *testing.T) {
AudioChannels: "5.1",
HDR: []string{"DV"},
Group: "FLUX",
Type: "series",
},
},
{
@ -149,6 +147,7 @@ func TestRelease_Parse(t *testing.T) {
Group: "FLUX",
Freeleech: true,
Bonus: []string{"Freeleech"},
Type: "series",
},
},
{
@ -163,7 +162,12 @@ func TestRelease_Parse(t *testing.T) {
Title: "Artist",
Group: "Albumname",
Audio: []string{"Cue", "FLAC", "Lossless", "Log100", "Log"},
AudioFormat: "FLAC",
Source: "CD",
Bitrate: "Lossless",
HasLog: true,
LogScore: 100,
HasCue: true,
},
},
{
@ -180,6 +184,8 @@ func TestRelease_Parse(t *testing.T) {
Title: "Various Artists - Music '21",
Source: "Cassette",
Audio: []string{"320", "MP3"},
AudioFormat: "MP3",
Bitrate: "320",
},
},
{
@ -194,7 +200,9 @@ func TestRelease_Parse(t *testing.T) {
Title: "The artist",
Group: "name",
Source: "CD",
Audio: []string{"MP3", "VBR"},
Audio: []string{"MP3", "VBR", "V0 (VBR)"},
AudioFormat: "MP3",
Bitrate: "V0 (VBR)",
},
},
{
@ -209,7 +217,12 @@ func TestRelease_Parse(t *testing.T) {
Title: "Artist",
Group: "Albumname",
Audio: []string{"Cue", "FLAC", "Lossless", "Log100", "Log"},
AudioFormat: "FLAC",
Source: "CD",
Bitrate: "Lossless",
HasLog: true,
LogScore: 100,
HasCue: true,
},
},
{
@ -224,7 +237,32 @@ func TestRelease_Parse(t *testing.T) {
Title: "Artist",
Group: "Albumname",
Audio: []string{"24BIT Lossless", "Cue", "FLAC", "Log100", "Log"},
AudioFormat: "FLAC",
Source: "CD",
Bitrate: "24BIT Lossless",
HasLog: true,
LogScore: 100,
HasCue: true,
},
},
{
name: "parse_music_6",
fields: Release{
TorrentName: "Artist - Albumname",
ReleaseTags: "FLAC / 24bit Lossless / Log / 78% / Cue / CD",
},
want: Release{
TorrentName: "Artist - Albumname",
ReleaseTags: "FLAC / 24bit Lossless / Log / 78% / Cue / CD",
Title: "Artist",
Group: "Albumname",
Audio: []string{"24BIT Lossless", "Cue", "FLAC", "Log78", "Log"},
AudioFormat: "FLAC",
Source: "CD",
Bitrate: "24BIT Lossless",
HasLog: true,
LogScore: 78,
HasCue: true,
},
},
{
@ -244,6 +282,7 @@ func TestRelease_Parse(t *testing.T) {
Year: 2007,
Group: "GROUP1",
Other: []string{"HYBRiD", "REMUX"},
Type: "movie",
},
},
{
@ -264,6 +303,7 @@ func TestRelease_Parse(t *testing.T) {
Group: "GROUP1",
Season: 1,
Language: []string{"ENGLiSH"},
Type: "series",
},
},
}
@ -663,264 +703,6 @@ func TestRelease_ParseString(t *testing.T) {
}
}
var trackerLessTestTorrent = `d7:comment19:This is just a test10:created by12:Johnny Bravo13:creation datei1430648794e8:encoding5:UTF-84:infod6:lengthi1128e4:name12:testfile.bin12:piece lengthi32768e6:pieces20:Õˆë =UŒäiÎ^æ °Eâ?ÇÒe5:nodesl35:udp://tracker.openbittorrent.com:8035:udp://tracker.openbittorrent.com:80ee`
func TestRelease_DownloadTorrentFile(t *testing.T) {
// disable logger
zerolog.SetGlobalLevel(zerolog.Disabled)
mux := http.NewServeMux()
ts := httptest.NewServer(mux)
defer ts.Close()
mux.HandleFunc("/files/valid_torrent_as_html", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")
payload, _ := os.ReadFile("testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
w.Write(payload)
})
mux.HandleFunc("/files/invalid_torrent_as_html", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")
payload := []byte("<html><head></head><body>This is not the torrent you are looking for</body></html>")
w.Write(payload)
})
mux.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")
payload := []byte("<html><head></head><body>This is not the torrent you are looking for</body></html>")
w.Write(payload)
})
mux.HandleFunc("/plaintext", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain")
payload := []byte("This is not a valid torrent file.")
w.Write(payload)
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.RequestURI, "401") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("unauthorized"))
return
}
if strings.Contains(r.RequestURI, "403") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("forbidden"))
return
}
if strings.Contains(r.RequestURI, "404") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("not found"))
return
}
if strings.Contains(r.RequestURI, "405") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("method not allowed"))
return
}
if strings.Contains(r.RequestURI, "file.torrent") {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/x-bittorrent")
payload, _ := os.ReadFile("testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
w.Write(payload)
return
}
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("internal error"))
})
type fields struct {
ID int64
FilterStatus ReleaseFilterStatus
Rejections []string
Indexer string
FilterName string
Protocol ReleaseProtocol
Implementation ReleaseImplementation
Timestamp time.Time
GroupID string
TorrentID string
DownloadURL string
TorrentTmpFile string
TorrentDataRawBytes []byte
TorrentHash string
TorrentName string
Size uint64
Title string
Category string
Categories []string
Season int
Episode int
Year int
Resolution string
Source string
Codec []string
Container string
HDR []string
Audio []string
AudioChannels string
Group string
Region string
Language []string
Proper bool
Repack bool
Website string
Artists string
Type string
LogScore int
Origin string
Tags []string
ReleaseTags string
Freeleech bool
FreeleechPercent int
Bonus []string
Uploader string
PreTime string
Other []string
RawCookie string
AdditionalSizeCheckRequired bool
FilterID int
Filter *Filter
ActionStatus []ReleaseActionStatus
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "401",
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
DownloadURL: fmt.Sprintf("%s/%d", ts.URL, 401),
Protocol: ReleaseProtocolTorrent,
},
wantErr: true,
},
{
name: "403",
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
DownloadURL: fmt.Sprintf("%s/%d", ts.URL, 403),
Protocol: ReleaseProtocolTorrent,
},
wantErr: true,
},
{
name: "500",
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
DownloadURL: fmt.Sprintf("%s/%d", ts.URL, 500),
Protocol: ReleaseProtocolTorrent,
},
wantErr: true,
},
{
name: "ok",
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
DownloadURL: fmt.Sprintf("%s/%s", ts.URL, "file.torrent"),
Protocol: ReleaseProtocolTorrent,
},
wantErr: false,
},
{
name: "valid_torrent_with_text-html_header",
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
DownloadURL: fmt.Sprintf("%s/files/%s", ts.URL, "valid_torrent_as_html"),
Protocol: ReleaseProtocolTorrent,
},
wantErr: false,
},
{
name: "invalid_torrent_with_text-html_header",
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
DownloadURL: fmt.Sprintf("%s/files/%s", ts.URL, "invalid_torrent_as_html"),
Protocol: ReleaseProtocolTorrent,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Release{
ID: tt.fields.ID,
FilterStatus: tt.fields.FilterStatus,
Rejections: tt.fields.Rejections,
Indexer: tt.fields.Indexer,
FilterName: tt.fields.FilterName,
Protocol: tt.fields.Protocol,
Implementation: tt.fields.Implementation,
Timestamp: tt.fields.Timestamp,
GroupID: tt.fields.GroupID,
TorrentID: tt.fields.TorrentID,
DownloadURL: tt.fields.DownloadURL,
TorrentTmpFile: tt.fields.TorrentTmpFile,
TorrentDataRawBytes: tt.fields.TorrentDataRawBytes,
TorrentHash: tt.fields.TorrentHash,
TorrentName: tt.fields.TorrentName,
Size: tt.fields.Size,
Title: tt.fields.Title,
Category: tt.fields.Category,
Categories: tt.fields.Categories,
Season: tt.fields.Season,
Episode: tt.fields.Episode,
Year: tt.fields.Year,
Resolution: tt.fields.Resolution,
Source: tt.fields.Source,
Codec: tt.fields.Codec,
Container: tt.fields.Container,
HDR: tt.fields.HDR,
Audio: tt.fields.Audio,
AudioChannels: tt.fields.AudioChannels,
Group: tt.fields.Group,
Region: tt.fields.Region,
Language: tt.fields.Language,
Proper: tt.fields.Proper,
Repack: tt.fields.Repack,
Website: tt.fields.Website,
Artists: tt.fields.Artists,
Type: tt.fields.Type,
LogScore: tt.fields.LogScore,
Origin: tt.fields.Origin,
Tags: tt.fields.Tags,
ReleaseTags: tt.fields.ReleaseTags,
Freeleech: tt.fields.Freeleech,
FreeleechPercent: tt.fields.FreeleechPercent,
Bonus: tt.fields.Bonus,
Uploader: tt.fields.Uploader,
PreTime: tt.fields.PreTime,
Other: tt.fields.Other,
RawCookie: tt.fields.RawCookie,
AdditionalSizeCheckRequired: tt.fields.AdditionalSizeCheckRequired,
FilterID: tt.fields.FilterID,
Filter: tt.fields.Filter,
ActionStatus: tt.fields.ActionStatus,
}
err := r.DownloadTorrentFile()
if err == nil && tt.wantErr {
fmt.Println("error")
}
})
}
}
func Test_getUniqueTags(t *testing.T) {
type args struct {
target []string