Fixing tvdb provider and adding fields=all

This commit is contained in:
Zoe Roux 2021-07-19 17:52:24 +02:00
parent a4635866a7
commit 4c0179ad4d
8 changed files with 72 additions and 26 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Linq.Expressions; using System.Linq.Expressions;
using Kyoo.Models.Attributes;
namespace Kyoo.Models namespace Kyoo.Models
{ {
@ -107,12 +108,12 @@ namespace Kyoo.Models
/// <summary> /// <summary>
/// A reference of the first resource. /// A reference of the first resource.
/// </summary> /// </summary>
public T1 First { get; set; } [SerializeIgnore] public T1 First { get; set; }
/// <summary> /// <summary>
/// A reference to the second resource. /// A reference to the second resource.
/// </summary> /// </summary>
public T2 Second { get; set; } [SerializeIgnore] public T2 Second { get; set; }
/// <summary> /// <summary>

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
using Kyoo.Common.Models.Attributes; using Kyoo.Common.Models.Attributes;
using Kyoo.Controllers; using Kyoo.Controllers;
using Kyoo.Models.Attributes; using Kyoo.Models.Attributes;
@ -157,6 +158,7 @@ namespace Kyoo.Models
/// <remarks>This method will never return anything if the <see cref="ExternalIDs"/> are not loaded.</remarks> /// <remarks>This method will never return anything if the <see cref="ExternalIDs"/> are not loaded.</remarks>
/// <param name="provider">The slug of the provider</param> /// <param name="provider">The slug of the provider</param>
/// <returns>The <see cref="MetadataID{T}.DataID"/> field of the asked provider.</returns> /// <returns>The <see cref="MetadataID{T}.DataID"/> field of the asked provider.</returns>
[CanBeNull]
public string GetID(string provider) public string GetID(string provider)
{ {
return ExternalIDs?.FirstOrDefault(x => x.Second.Slug == provider)?.DataID; return ExternalIDs?.FirstOrDefault(x => x.Second.Slug == provider)?.DataID;

View File

@ -43,10 +43,18 @@ namespace Kyoo.CommonApi
PropertyInfo[] properties = type.GetProperties() PropertyInfo[] properties = type.GetProperties()
.Where(x => x.GetCustomAttribute<LoadableRelationAttribute>() != null) .Where(x => x.GetCustomAttribute<LoadableRelationAttribute>() != null)
.ToArray(); .ToArray();
fields = fields.Select(x => if (fields.Count == 1 && fields.Contains("all"))
{
fields = properties.Select(x => x.Name).ToList();
}
else
{
fields = fields
.Select(x =>
{ {
string property = properties string property = properties
.FirstOrDefault(y => string.Equals(x, y.Name, StringComparison.InvariantCultureIgnoreCase)) .FirstOrDefault(y
=> string.Equals(x, y.Name, StringComparison.InvariantCultureIgnoreCase))
?.Name; ?.Name;
if (property != null) if (property != null)
return property; return property;
@ -60,6 +68,7 @@ namespace Kyoo.CommonApi
if (context.Result != null) if (context.Result != null)
return; return;
} }
}
context.HttpContext.Items["fields"] = fields; context.HttpContext.Items["fields"] = fields;
base.OnActionExecuting(context); base.OnActionExecuting(context);
} }

View File

@ -33,7 +33,8 @@ namespace Kyoo.TheTvdb
/// <returns>The parsed <see cref="DateTime"/> or null.</returns> /// <returns>The parsed <see cref="DateTime"/> or null.</returns>
private static DateTime ParseDate(string date) private static DateTime ParseDate(string date)
{ {
DateTime.TryParseExact(date, "yyyy-mm-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsed); DateTime.TryParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture,
DateTimeStyles.None, out DateTime parsed);
return parsed; return parsed;
} }
@ -122,7 +123,8 @@ namespace Kyoo.TheTvdb
} }
} }
}, },
Role = actor.Role Role = actor.Role,
Type = "Actor"
}; };
} }

View File

@ -71,11 +71,21 @@ namespace Kyoo.TheTvdb
}; };
} }
/// <summary>
/// Retrieve metadata about a show.
/// </summary>
/// <param name="show">The base show to retrieve metadata for.</param>
/// <returns>A new show filled with metadata from the tvdb.</returns>
[ItemCanBeNull] [ItemCanBeNull]
private async Task<Show> _GetShow([NotNull] Show show) private async Task<Show> _GetShow([NotNull] Show show)
{ {
if (!int.TryParse(show.GetID(Provider.Slug), out int id)) if (!int.TryParse(show.GetID(Provider.Slug), out int id))
return (await _SearchShow(show.Title)).FirstOrDefault(); {
Show found = (await _SearchShow(show.Title)).FirstOrDefault();
if (found == null)
return null;
return await Get(found);
}
TvDbResponse<Series> series = await _client.Series.GetAsync(id); TvDbResponse<Series> series = await _client.Series.GetAsync(id);
Show ret = series.Data.ToShow(Provider); Show ret = series.Data.ToShow(Provider);
@ -84,6 +94,11 @@ namespace Kyoo.TheTvdb
return ret; return ret;
} }
/// <summary>
/// Retrieve metadata about an episode.
/// </summary>
/// <param name="episode">The base episode to retrieve metadata for.</param>
/// <returns>A new episode filled with metadata from the tvdb.</returns>
[ItemCanBeNull] [ItemCanBeNull]
private async Task<Episode> _GetEpisode([NotNull] Episode episode) private async Task<Episode> _GetEpisode([NotNull] Episode episode)
{ {
@ -106,11 +121,23 @@ namespace Kyoo.TheTvdb
return ArraySegment<T>.Empty; return ArraySegment<T>.Empty;
} }
/// <summary>
/// Search for shows in the tvdb.
/// </summary>
/// <param name="query">The query to ask the tvdb about.</param>
/// <returns>A list of shows that could be found on the tvdb.</returns>
[ItemNotNull] [ItemNotNull]
private async Task<ICollection<Show>> _SearchShow(string query) private async Task<ICollection<Show>> _SearchShow(string query)
{
try
{ {
TvDbResponse<SeriesSearchResult[]> shows = await _client.Search.SearchSeriesByNameAsync(query); TvDbResponse<SeriesSearchResult[]> shows = await _client.Search.SearchSeriesByNameAsync(query);
return shows.Data.Select(x => x.ToShow(Provider)).ToArray(); return shows.Data.Select(x => x.ToShow(Provider)).ToArray();
} }
catch (TvDbServerException)
{
return ArraySegment<Show>.Empty;
}
}
} }
} }

View File

@ -150,7 +150,7 @@ namespace Kyoo.Tasks
string[] subtitles = files string[] subtitles = files
.Where(FileExtensions.IsSubtitle) .Where(FileExtensions.IsSubtitle)
.Where(x => x.Contains("/Extra/")) .Where(x => !x.Contains("/Extra/"))
.Where(x => tracks.All(y => y.Path != x)) .Where(x => tracks.All(y => y.Path != x))
.ToArray(); .ToArray();
percent = 0; percent = 0;

View File

@ -112,14 +112,15 @@ namespace Kyoo.Tasks
if (season != null) if (season != null)
season.Show = show; season.Show = show;
season = await _RegisterAndFill(season); season = await _RegisterAndFill(season);
if (season != null)
season.Title ??= $"Season {season.SeasonNumber}";
progress.Report(60); progress.Report(60);
episode = await MetadataProvider.Get(episode);
progress.Report(70);
episode.Show = show; episode.Show = show;
episode.Season = season; episode.Season = season;
episode = await MetadataProvider.Get(episode);
progress.Report(70);
episode.Tracks = (await Transcoder.ExtractInfos(episode, false)) episode.Tracks = (await Transcoder.ExtractInfos(episode, false))
.Where(x => x.Type != StreamType.Attachment) .Where(x => x.Type != StreamType.Attachment)
.ToArray(); .ToArray();

View File

@ -60,5 +60,9 @@
}, },
"profilePicturePath": "users/", "profilePicturePath": "users/",
"clients": [] "clients": []
},
"tvdb": {
"apiKey": "REDACTED"
} }
} }