Skip to content

Commit ed6db2c

Browse files
committed
fix: tests
1 parent 7307450 commit ed6db2c

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

internal/discovery/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func getDownloadableEntries(animeListEntry animelist.Entry, entries []nyaa.Entry
144144
func (c *Controller) NyaaSearch(ctx context.Context, entry animelist.Entry) ([]nyaa.Entry, error) {
145145
// Build search query for Nyaa.
146146
// For title we filter for english and original titles.
147-
strippedTitles := utils.Map(entry.Titles, func(title string) string { return parser.TitleStrip(title, true) })
147+
strippedTitles := utils.Map(entry.Titles, func(title string) string { return parser.TitleStrip(title) })
148148
titleQuery := nyaa.QueryOr(strippedTitles)
149149
sourceQuery := nyaa.QueryOr(c.dep.Config.Sources)
150150
qualityQuery := nyaa.QueryOr(c.dep.Config.Qualitites)

internal/discovery/torrent.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ func (c *Controller) TorrentGetLatestEpisodes(ctx context.Context, entry animeli
1818
for i := range entry.Titles {
1919
// we should consider both metadata and titleEng, because your anime list has different titles available,
2020
// some torrent sources will use one, some will use the other, so to avoid duplication we check for both.
21-
metadata := parser.Parse(entry.Titles[i])
22-
metadata.Title = parser.TitleStrip(metadata.Title, true)
21+
metadata := parser.Parse(entry.Titles[i], parser.RemoveDots())
2322
resp, err := c.dep.TorrentClient.List(ctx, &torrentclient.ListTorrentConfig{
2423
Tag: utils.Pointer(metadata.TagBuildSeries()),
2524
})
@@ -60,8 +59,10 @@ func (c *Controller) buildTorrentName(entry animelist.Entry, parsedNyaa parser.P
6059
// It will configure all necessary metadata and send it to your torrent client.
6160
func (c *Controller) TorrentDigestNyaa(ctx context.Context, entry animelist.Entry, parsedNyaa parser.ParsedNyaa) error {
6261
savePath := c.TorrentGetDownloadPath(entry.Titles[0])
63-
parsedNyaa.Meta.Title = parser.TitleStrip(parsedNyaa.Meta.Title, true)
64-
tags := parsedNyaa.Meta.TagsBuildTorrent()
62+
meta := parsedNyaa.Meta.Clone()
63+
meta.Title = parser.TitleStrip(meta.Title)
64+
tags := meta.TagsBuildTorrent()
65+
6566
req := &torrentclient.AddTorrentConfig{
6667
Tags: tags,
6768
URLs: []string{parsedNyaa.Entry.Link},

internal/parser/metadata.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ func (m *Metadata) TagIsNextEpisode(latest string) bool {
3131
}
3232
return true
3333
}
34+
35+
func (m Metadata) Clone() Metadata {
36+
return Metadata{
37+
Source: m.Source,
38+
Title: m.Title,
39+
Episode: m.Episode,
40+
Season: m.Season,
41+
Tags: append([]string{}, m.Tags...),
42+
VerticalResolution: m.VerticalResolution,
43+
IsMultiEpisode: m.IsMultiEpisode,
44+
}
45+
}

internal/parser/title.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,42 @@ func TitleStripSubtitle(title string) string {
1919
return title
2020
}
2121

22+
type titleCleanOptions struct {
23+
removeDots bool
24+
}
25+
26+
type TitleStripOptions interface {
27+
applyTitleStripOptions(*titleCleanOptions)
28+
}
29+
30+
type optRemoveDots struct{}
31+
32+
func (o optRemoveDots) applyTitleStripOptions(opts *titleCleanOptions) {
33+
opts.removeDots = true
34+
}
35+
36+
func RemoveDots() TitleStripOptions {
37+
return optRemoveDots{}
38+
}
39+
2240
// TitleStrip cleans title from sub-titles, tags and season / episode information.
2341
// Example: [Source] Show: another story - S03E02 [1080p].mkv -> Show.
24-
func TitleStrip(title string, cleanSubtitle bool) string {
42+
func TitleStrip(title string, opts ...TitleStripOptions) string {
43+
options := titleCleanOptions{}
44+
for _, opt := range opts {
45+
opt.applyTitleStripOptions(&options)
46+
}
47+
2548
if index := seasonIndexMatch(title); index != -1 {
2649
title = title[:index]
2750
}
2851
if index := episodeIndexMatch(title); index != -1 {
2952
title = title[:index]
3053
}
3154
title = regexp.MustCompile(`\s{2,}`).ReplaceAllString(title, " ")
32-
if cleanSubtitle {
33-
title = TitleStripSubtitle(title)
55+
title = TitleStripSubtitle(title)
56+
if options.removeDots {
57+
title = strings.ReplaceAll(title, ".", " ")
3458
}
3559
title = strings.ReplaceAll(title, "\"", "")
3660
title = removeTags(title)
@@ -45,9 +69,9 @@ func removeTags(title string) string {
4569
}
4670

4771
// Parse will parse a title into a Metadata, extracting stripped title, tags, season and episode information.
48-
func Parse(title string) Metadata {
72+
func Parse(title string, opts ...TitleStripOptions) Metadata {
4973
resp := Metadata{
50-
Title: TitleStrip(title, false),
74+
Title: TitleStrip(title, opts...),
5175
VerticalResolution: qualityMatch(title),
5276
}
5377
if tags := tagsExpr.FindAllStringSubmatch(title, -1); len(tags) > 0 {

internal/parser/title_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestTitleStrip(t *testing.T) {
1919
}
2020
for _, tt := range tests {
2121
t.Run(tt.name, func(t *testing.T) {
22-
if got := TitleStrip(tt.title, false); got != tt.want {
22+
if got := TitleStrip(tt.title); got != tt.want {
2323
t.Errorf("TitleStrip() = %v, want %v", got, tt.want)
2424
}
2525
})
@@ -30,6 +30,7 @@ func TestTitleParse(t *testing.T) {
3030
tests := []struct {
3131
name string
3232
title string
33+
opts []TitleStripOptions
3334
want Metadata
3435
}{
3536
{
@@ -87,6 +88,7 @@ func TestTitleParse(t *testing.T) {
8788
{
8889
name: "all dots",
8990
title: "MASHLE.MAGIC.AND.MUSCLES.S02E19.Mash.Burnedead.and.the.Magical.Maestro.1080p.CR.WEB-DL.AAC2.0.H.264-VARYG.mkv",
91+
opts: []TitleStripOptions{RemoveDots()},
9092
want: Metadata{
9193
Title: "MASHLE MAGIC AND MUSCLES",
9294
Episode: "19",
@@ -98,6 +100,7 @@ func TestTitleParse(t *testing.T) {
98100
{
99101
name: "undead unluck",
100102
title: "Undead.Unluck.S01E20.Anno.Un.1080p.HULU.WEB-DL.AAC2.0.H.264-VARYG.mkv",
103+
opts: []TitleStripOptions{RemoveDots()},
101104
want: Metadata{
102105
Title: "Undead Unluck",
103106
Episode: "20",
@@ -109,7 +112,7 @@ func TestTitleParse(t *testing.T) {
109112
}
110113
for _, tt := range tests {
111114
t.Run(tt.name, func(t *testing.T) {
112-
got := Parse(tt.title)
115+
got := Parse(tt.title, tt.opts...)
113116
require.Equal(t, tt.want, got)
114117
})
115118
}

0 commit comments

Comments
 (0)