Skip to content
This repository was archived by the owner on Apr 13, 2024. It is now read-only.

Commit e09771f

Browse files
committed
Improves tvdb filtering
1 parent 29428ac commit e09771f

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace Wasari.Tvdb.Api.Extensions;
4+
5+
internal static partial class StringExtensions
6+
{
7+
[GeneratedRegex("[a-zA-Z0-9 ]+")]
8+
private static partial Regex EpisodeTitleNormalizeRegex();
9+
10+
public static string NormalizeUsingRegex(this string str) => string.Join(string.Empty, EpisodeTitleNormalizeRegex().Matches(str).Select(o => o.Value));
11+
}

Wasari.Tvdb.Api/Services/Remedy.cs renamed to Wasari.Tvdb.Api/Services/TvdbEpisodesService.cs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Wasari.Tvdb.Abstractions;
2+
using Wasari.Tvdb.Api.Extensions;
23

34
namespace Wasari.Tvdb.Api.Services;
45

@@ -14,21 +15,36 @@ public TvdbEpisodesService(ITvdbApi tvdbApi)
1415
public async ValueTask<IResult> GetEpisodes(string query)
1516
{
1617
var searchResult = await TvdbApi.SearchAsync(query);
17-
18-
if(searchResult.Data.Count != 1)
18+
var tvdbSearchResponseSeries = searchResult.Data;
19+
20+
if (tvdbSearchResponseSeries.Count > 1)
21+
{
22+
var normalizedQuery = query.NormalizeUsingRegex();
23+
24+
var matchByAlias = tvdbSearchResponseSeries
25+
.Where(i => i.Aliases != null && i.Aliases.Any(x => string.Equals(x, normalizedQuery, StringComparison.InvariantCultureIgnoreCase)))
26+
.ToArray();
27+
28+
if (matchByAlias.Length == 1)
29+
{
30+
tvdbSearchResponseSeries = matchByAlias;
31+
}
32+
}
33+
34+
if (tvdbSearchResponseSeries.Count != 1)
1935
return Results.BadRequest(new
2036
{
2137
Status = StatusCodes.Status400BadRequest,
2238
Title = "Invalid query",
2339
Detail = "Query must return exactly one result"
2440
});
25-
26-
var series = searchResult.Data.Single();
41+
42+
var series = tvdbSearchResponseSeries.Single();
2743
var seriesWithEpisodes = await TvdbApi.GetSeriesAsync(series.TvdbId);
2844

2945
return Results.Ok(seriesWithEpisodes.Data.Episodes
3046
.Where(i => !string.IsNullOrEmpty(i.Name))
31-
.Select(i => new Episode(i.Name, i.SeasonNumber, i.Number, i.IsMovie switch
47+
.Select(i => new WasariTvdbEpisode(i.Name, i.SeasonNumber, i.Number, i.IsMovie switch
3248
{
3349
0 => false,
3450
1 => true,

Wasari.Tvdb/Models/TvdbSearchResponseSeries.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Wasari.Tvdb.Models;
44

55
public record TvdbSearchResponseSeries(
66
[property: JsonPropertyName("objectID")] string ObjectId,
7-
[property: JsonPropertyName("aliases")] IReadOnlyList<string> Aliases,
7+
[property: JsonPropertyName("aliases")] IReadOnlyList<string>? Aliases,
88
[property: JsonPropertyName("country")] string Country,
99
[property: JsonPropertyName("id")] string Id,
1010
[property: JsonPropertyName("image_url")] string ImageUrl,

0 commit comments

Comments
 (0)