Skip to content

Commit

Permalink
feat(autodownloader): additional terms filter
Browse files Browse the repository at this point in the history
  • Loading branch information
5rahim committed Oct 27, 2024
1 parent 2bcbe8f commit bba1b90
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 79 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ All notable changes to this project will be documented in this file.

## v2.2.1

- ⚡️ Auto Downloader: Added 'additional terms' filter option
- 🦺 Torrent streaming: Fixed auto-select regression
- 🦺 Fixed auto-scanning regression
- 🦺 Fixed issue with inexistant log directory

## v2.2.0
Expand Down
31 changes: 28 additions & 3 deletions codegen/generated/public_structs.json
Original file line number Diff line number Diff line change
Expand Up @@ -25501,9 +25501,7 @@
"usedStructName": "anilist.AnimeCollection",
"required": false,
"public": false,
"comments": [
" TODO: Rename to animeCollection"
]
"comments": []
},
{
"name": "rawAnimeCollection",
Expand Down Expand Up @@ -33713,6 +33711,15 @@
"required": true,
"public": true,
"comments": []
},
{
"name": "AdditionalTerms",
"jsonName": "additionalTerms",
"goType": "[]string",
"typescriptType": "Array\u003cstring\u003e",
"required": false,
"public": true,
"comments": []
}
],
"comments": []
Expand Down Expand Up @@ -36160,6 +36167,15 @@
"required": false,
"public": false,
"comments": []
},
{
"name": "logsDir",
"jsonName": "logsDir",
"goType": "string",
"typescriptType": "string",
"required": true,
"public": false,
"comments": []
}
],
"comments": []
Expand Down Expand Up @@ -36249,6 +36265,15 @@
"required": false,
"public": true,
"comments": []
},
{
"name": "LogsDir",
"jsonName": "LogsDir",
"goType": "string",
"typescriptType": "string",
"required": true,
"public": true,
"comments": []
}
],
"comments": []
Expand Down
1 change: 1 addition & 0 deletions internal/core/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (a *App) initModulesOnce() {
Enabled: false, // Will be set in InitOrRefreshModules
AutoDownloader: a.AutoDownloader,
MetadataProvider: a.MetadataProvider,
LogsDir: a.Config.Logs.Dir,
})

// This is run in a goroutine
Expand Down
53 changes: 27 additions & 26 deletions internal/extension_repo/goja_torrent_test/my-torrent-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,33 @@ class Provider {
}
}

async fetchTorrents(url: string): Promise<ToshoTorrent[]> {
const furl = `${this.api}${url}`

try {
const response = await fetch(furl)

if (!response.ok) {
throw new Error(`Failed to fetch torrents, ${response.statusText}`)
}

const torrents: ToshoTorrent[] = await response.json()

return torrents.map(t => {
if (t.seeders > 30000) {
t.seeders = 0
}
if (t.leechers > 30000) {
t.leechers = 0
}
return t
})
}
catch (error) {
throw new Error(`Error fetching torrents: ${error}`)
}
}

async search(opts: AnimeSearchOptions): Promise<AnimeTorrent[]> {
const query = `?q=${encodeURIComponent(opts.query)}&only_tor=1`
console.log(query)
Expand Down Expand Up @@ -80,32 +107,6 @@ class Provider {
return this.fetchTorrents(query)
}

async fetchTorrents(url: string): Promise<ToshoTorrent[]> {
const furl = `${this.api}${url}`

try {
const response = await fetch(furl)

if (!response.ok) {
throw new Error(`Failed to fetch torrents, ${response.statusText}`)
}

const torrents: ToshoTorrent[] = await response.json()

return torrents.map(t => {
if (t.seeders > 30000) {
t.seeders = 0
}
if (t.leechers > 30000) {
t.leechers = 0
}
return t
})
}
catch (error) {
throw new Error(`Error fetching torrents: ${error}`)
}
}

formatCommonQuery(quality: string): string {
if (quality === "") {
Expand Down
2 changes: 2 additions & 0 deletions internal/handlers/auto_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func HandleCreateAutoDownloaderRule(c *RouteCtx) error {
MediaId int `json:"mediaId"`
ReleaseGroups []string `json:"releaseGroups"`
Resolutions []string `json:"resolutions"`
AdditionalTerms []string `json:"additionalTerms"`
ComparisonTitle string `json:"comparisonTitle"`
TitleComparisonType anime.AutoDownloaderRuleTitleComparisonType `json:"titleComparisonType"`
EpisodeType anime.AutoDownloaderRuleEpisodeType `json:"episodeType"`
Expand All @@ -111,6 +112,7 @@ func HandleCreateAutoDownloaderRule(c *RouteCtx) error {
EpisodeType: b.EpisodeType,
EpisodeNumbers: b.EpisodeNumbers,
Destination: b.Destination,
AdditionalTerms: b.AdditionalTerms,
}

if err := db_bridge.InsertAutoDownloaderRule(c.App.Database, rule); err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/library/anime/autodownloader_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ type (
EpisodeType AutoDownloaderRuleEpisodeType `json:"episodeType"`
EpisodeNumbers []int `json:"episodeNumbers,omitempty"`
Destination string `json:"destination"`
AdditionalTerms []string `json:"additionalTerms"`
}
)
34 changes: 34 additions & 0 deletions internal/library/autodownloader/autodownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ func (ad *AutoDownloader) torrentFollowsRule(
return -1, false
}

if ok := ad.isAdditionalTermsMatch(t.Name, rule); !ok {
return -1, false
}

episode, ok := ad.isSeasonAndEpisodeMatch(t.ParsedData, rule, listEntry, localEntry, items)
if !ok {
return -1, false
Expand Down Expand Up @@ -557,6 +561,36 @@ func (ad *AutoDownloader) downloadTorrent(t *NormalizedTorrent, rule *anime.Auto

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

func (ad *AutoDownloader) isAdditionalTermsMatch(torrentName string, rule *anime.AutoDownloaderRule) (ok bool) {
defer util.HandlePanicInModuleThen("autodownloader/isAdditionalTermsMatch", func() {
ok = false
})

if len(rule.AdditionalTerms) == 0 {
return true
}

// Go through each additional term
for _, optionsText := range rule.AdditionalTerms {
// Split the options by comma
options := strings.Split(strings.TrimSpace(optionsText), ",")
// Check if the torrent name contains at least one of the options
foundOption := false
for _, option := range options {
option := strings.TrimSpace(option)
if strings.Contains(strings.ToLower(torrentName), strings.ToLower(option)) {
foundOption = true
}
}
// If the torrent name doesn't contain any of the options, return false
if !foundOption {
return false
}
}

// If all options are found, return true
return true
}
func (ad *AutoDownloader) isReleaseGroupMatch(releaseGroup string, rule *anime.AutoDownloaderRule) (ok bool) {
defer util.HandlePanicInModuleThen("autodownloader/isReleaseGroupMatch", func() {
ok = false
Expand Down
92 changes: 92 additions & 0 deletions internal/library/autodownloader/comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,95 @@ func TestComparison(t *testing.T) {
}

}

func TestComparison2(t *testing.T) {
ad := AutoDownloader{
metadataProvider: metadata.GetMockProvider(t),
settings: &models.AutoDownloaderSettings{
EnableSeasonCheck: true,
},
}
//name1 := "DANDADAN"
//name2 := "Dandadan"
//aniListEntry := &anilist.AnimeListEntry{
// Media: &anilist.BaseAnime{
// Title: &anilist.BaseAnime_Title{
// Romaji: &name1,
// English: &name2,
// },
// Episodes: lo.ToPtr(12),
// Format: lo.ToPtr(anilist.MediaFormatTv),
// },
//}

rule := &anime.AutoDownloaderRule{
MediaId: 166531,
ReleaseGroups: []string{},
Resolutions: []string{"1080p"},
TitleComparisonType: "likely",
EpisodeType: "recent",
EpisodeNumbers: []int{},
Destination: "/data/seanime/library/Dandadan",
ComparisonTitle: "Dandadan",
}

tests := []struct {
torrentName string
succeedAdditionalTermsMatch bool
ruleAdditionalTerms []string
}{
{
torrentName: "[Anime Time] Dandadan - 04 [Dual Audio][1080p][HEVC 10bit x265][AAC][Multi Sub] [Weekly]",
ruleAdditionalTerms: []string{},
succeedAdditionalTermsMatch: true,
},
{
torrentName: "[Anime Time] Dandadan - 04 [Dual Audio][1080p][HEVC 10bit x265][AAC][Multi Sub] [Weekly]",
ruleAdditionalTerms: []string{
"H265,H.265, H 265,x265",
"10bit,10-bit,10 bit",
},
succeedAdditionalTermsMatch: true,
},
{
torrentName: "[Raze] Dandadan - 04 x265 10bit 1080p 143.8561fps.mkv",
ruleAdditionalTerms: []string{
"H265,H.265, H 265,x265",
"10bit,10-bit,10 bit",
},
succeedAdditionalTermsMatch: true,
},
{
torrentName: "[Sokudo] DAN DA DAN | Dandadan - S01E03 [1080p EAC-3 AV1][Dual Audio] (weekly)",
ruleAdditionalTerms: []string{
"H265,H.265, H 265,x265",
"10bit,10-bit,10 bit",
},
succeedAdditionalTermsMatch: false,
},
{
torrentName: "[Raze] Dandadan - 04 x265 10bit 1080p 143.8561fps.mkv",
ruleAdditionalTerms: []string{
"H265,H.265, H 265,x265",
"10bit,10-bit,10 bit",
"AAC",
},
succeedAdditionalTermsMatch: false,
},
}

for _, tt := range tests {
t.Run(tt.torrentName, func(t *testing.T) {

rule.AdditionalTerms = tt.ruleAdditionalTerms

ok := ad.isAdditionalTermsMatch(tt.torrentName, rule)
if tt.succeedAdditionalTermsMatch {
require.True(t, ok)
} else {
require.False(t, ok)
}
})
}

}
14 changes: 14 additions & 0 deletions internal/library/autoscanner/autoscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type (
db *db.Database // Database instance is required to update the local files.
autoDownloader *autodownloader.AutoDownloader // AutoDownloader instance is required to refresh queue.
metadataProvider metadata.Provider
logsDir string
}
NewAutoScannerOptions struct {
Database *db.Database
Expand All @@ -42,6 +43,7 @@ type (
AutoDownloader *autodownloader.AutoDownloader
WaitTime time.Duration
MetadataProvider metadata.Provider
LogsDir string
}
)

Expand All @@ -65,6 +67,7 @@ func New(opts *NewAutoScannerOptions) *AutoScanner {
db: opts.Database,
autoDownloader: opts.AutoDownloader,
metadataProvider: opts.MetadataProvider,
logsDir: opts.LogsDir,
}
}

Expand Down Expand Up @@ -196,6 +199,16 @@ func (as *AutoScanner) scan() {
return
}

// Create a new scan logger
var scanLogger *scanner.ScanLogger
if as.logsDir != "" {
scanLogger, err = scanner.NewScanLogger(as.logsDir)
if err != nil {
as.logger.Error().Err(err).Msg("autoscanner: Failed to create scan logger")
return
}
}

// Create a new scanner
sc := scanner.Scanner{
DirPath: settings.Library.LibraryPath,
Expand All @@ -208,6 +221,7 @@ func (as *AutoScanner) scan() {
SkipLockedFiles: true, // Skip locked files by default.
SkipIgnoredFiles: true,
ScanSummaryLogger: scanSummaryLogger,
ScanLogger: scanLogger,
MetadataProvider: as.metadataProvider,
}

Expand Down
1 change: 0 additions & 1 deletion internal/library/scanner/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func (m *Matcher) MatchLocalFilesWithMedia() error {
// If the best match is above a certain threshold, set the local file's mediaId to the best match's id
// If the best match is below a certain threshold, leave the local file's mediaId to 0
func (m *Matcher) matchLocalFileWithMedia(lf *anime.LocalFile) {

defer util.HandlePanicInModuleThenS("scanner/matcher/matchLocalFileWithMedia", func(stackTrace string) {
lf.MediaId = 0
/*Log*/
Expand Down
4 changes: 3 additions & 1 deletion internal/library/scanner/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ type Scanner struct {

// Scan will scan the directory and return a list of anime.LocalFile.
func (scn *Scanner) Scan() (lfs []*anime.LocalFile, err error) {

defer util.HandlePanicWithError(&err)

scn.WSEventManager.SendEvent(events.EventScanProgress, 0)
scn.WSEventManager.SendEvent(events.EventScanStatus, "Retrieving local files...")

completeAnimeCache := anilist.NewCompleteAnimeCache()

// Create a new Anilist rate limiter
Expand Down
1 change: 1 addition & 0 deletions seanime-web/src/api/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,7 @@ export type Anime_AutoDownloaderRule = {
episodeType: Anime_AutoDownloaderRuleEpisodeType
episodeNumbers?: Array<number>
destination: string
additionalTerms?: Array<string>
}

/**
Expand Down
Loading

0 comments on commit bba1b90

Please sign in to comment.