Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for tvdb v4 migration #110

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Jellyfin.Plugin.Tvdb/Providers/TvdbEpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ private static MetadataResult<Episode> MapEpisodeToResult(EpisodeInfo id, Episod
AirsBeforeSeasonNumber = episode.AirsBeforeSeason,
// Tvdb uses 3 letter code for language (prob ISO 639-2)
// Reverts to OriginalName if no translation is found
Name = episode.Translations.NameTranslations.FirstOrDefault(x => string.Equals(x.Language, TvdbUtils.NormalizeLanguageToTvdb(id.MetadataLanguage), StringComparison.OrdinalIgnoreCase))?.Name ?? episode.Name,
Overview = episode.Translations.OverviewTranslations?.FirstOrDefault(x => string.Equals(x.Language, TvdbUtils.NormalizeLanguageToTvdb(id.MetadataLanguage), StringComparison.OrdinalIgnoreCase))?.Overview ?? episode.Overview
Name = episode.Translations.GetTranslatedNamedOrDefault(id.MetadataLanguage) ?? episode.Name,
Overview = episode.Translations.GetTranslatedOverviewOrDefault(id.MetadataLanguage) ?? episode.Overview
}
};
result.ResetPeople();
Expand Down
20 changes: 12 additions & 8 deletions Jellyfin.Plugin.Tvdb/Providers/TvdbMissingEpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Entities.Libraries;
using Jellyfin.Data.Events;
using MediaBrowser.Controller.BaseItemManager;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Providers;
Expand Down Expand Up @@ -99,7 +97,13 @@ protected virtual void Dispose(bool disposing)

private static bool EpisodeExists(EpisodeBaseRecord episodeRecord, IReadOnlyList<Episode> existingEpisodes)
{
return existingEpisodes.Any(ep => ep.ContainsEpisodeNumber(episodeRecord.Number) && ep.ParentIndexNumber == episodeRecord.Number);
return existingEpisodes.Any(episode => EpisodeEquals(episode, episodeRecord));
}

private static bool EpisodeEquals(Episode episode, EpisodeBaseRecord otherEpisodeRecord)
{
return episode.ContainsEpisodeNumber(otherEpisodeRecord.Number)
&& episode.ParentIndexNumber == otherEpisodeRecord.SeasonNumber;
}

private bool IsEnabledForLibrary(BaseItem item)
Expand Down Expand Up @@ -196,17 +200,17 @@ private async Task HandleSeason(Season season)
var tvdbId = Convert.ToInt32(tvdbIdTxt, CultureInfo.InvariantCulture);
var allEpisodes = await GetAllEpisodes(tvdbId, season.GetPreferredMetadataLanguage()).ConfigureAwait(false);

var seasonEpisodes = allEpisodes.Where(e => e.SeasonNumber == season.IndexNumber).ToList();
var existingEpisodes = season.Children.OfType<Episode>().ToList();

for (var i = 0; i < allEpisodes.Count; i++)
foreach (var episodeRecord in seasonEpisodes)
{
var episode = allEpisodes[i];
if (EpisodeExists(episode, existingEpisodes))
if (EpisodeExists(episodeRecord, existingEpisodes))
{
continue;
}

AddVirtualEpisode(episode, season);
AddVirtualEpisode(episodeRecord, season);
}
}

Expand Down Expand Up @@ -289,7 +293,7 @@ private void OnLibraryManagerItemRemoved(object? sender, ItemChangeEventArgs ite
EpisodeBaseRecord? episodeRecord = null;
if (episodeRecords.Count > 0)
{
episodeRecord = episodeRecords[0];
episodeRecord = episodeRecords.FirstOrDefault(e => EpisodeEquals(episode, e));
}

AddVirtualEpisode(episodeRecord, episode.Season);
Expand Down
6 changes: 3 additions & 3 deletions Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ await _tvdbClientManager
.ConfigureAwait(false);
var resultData = result;

if (resultData == null || resultData.Count == 0)
if (resultData is null || resultData.Count == 0 || resultData[0] is null || resultData[0].Series is null)
{
_logger.LogWarning("TvdbSearch: No series found for id: {0}", id);
return null;
Expand Down Expand Up @@ -467,8 +467,8 @@ private static void MapSeriesToResult(MetadataResult<Series> result, SeriesExten
series.SetProviderId(TvdbPlugin.ProviderId, tvdbSeries.Id.ToString(CultureInfo.InvariantCulture));
// Tvdb uses 3 letter code for language (prob ISO 639-2)
// Reverts to OriginalName if no translation is found
series.Name = tvdbSeries.Translations.NameTranslations.FirstOrDefault(x => string.Equals(x.Language, TvdbUtils.NormalizeLanguageToTvdb(info.MetadataLanguage), StringComparison.OrdinalIgnoreCase))?.Name ?? tvdbSeries.Name;
series.Overview = tvdbSeries.Translations.OverviewTranslations.FirstOrDefault(x => string.Equals(x.Language, TvdbUtils.NormalizeLanguageToTvdb(info.MetadataLanguage), StringComparison.OrdinalIgnoreCase))?.Overview ?? tvdbSeries.Overview;
series.Name = tvdbSeries.Translations.GetTranslatedNamedOrDefault(info.MetadataLanguage) ?? tvdbSeries.Name;
series.Overview = tvdbSeries.Translations.GetTranslatedOverviewOrDefault(info.MetadataLanguage) ?? tvdbSeries.Overview;
series.OriginalTitle = tvdbSeries.Name;
result.ResultLanguage = info.MetadataLanguage;
series.AirDays = TvdbUtils.GetAirDays(tvdbSeries.AirsDays).ToArray();
Expand Down
39 changes: 39 additions & 0 deletions Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;

using Jellyfin.Extensions;

using Tvdb.Sdk;

namespace Jellyfin.Plugin.Tvdb;

/// <summary>
/// Extension Methods for Tvdb SDK.
/// </summary>
public static class TvdbSdkExtensions
{
/// <summary>
/// Get the translated Name, or <see langword="null"/>.
/// </summary>
/// <param name="translations">Available translations.</param>
/// <param name="language">Requested language.</param>
/// <returns>Translated Name, or <see langword="null"/>.</returns>
public static string? GetTranslatedNamedOrDefault(this TranslationExtended? translations, string? language)
{
return translations?
.NameTranslations
.FirstOrDefault(translation => TvdbUtils.MatchLanguage(language, translation.Language))?.Name;
}

/// <summary>
/// Get the translated Overview, or <see langword="null"/>.
/// </summary>
/// <param name="translations">Available translations.</param>
/// <param name="language">Requested language.</param>
/// <returns>Translated Overview, or <see langword="null"/>.</returns>
public static string? GetTranslatedOverviewOrDefault(this TranslationExtended? translations, string? language)
{
return translations?
.OverviewTranslations
.FirstOrDefault(translation => TvdbUtils.MatchLanguage(language, translation.Language))?.Overview;
}
}
21 changes: 12 additions & 9 deletions Jellyfin.Plugin.Tvdb/TvdbUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;

using MediaBrowser.Model.Entities;
using Tvdb.Sdk;

Expand Down Expand Up @@ -39,31 +41,32 @@ public static ImageType GetImageTypeFromKeyType(string? keyType)
}

/// <summary>
/// Normalize language to tvdb format.
/// Try to find a match for input language.
/// </summary>
/// <param name="language">Language.</param>
/// <param name="language">input Language.</param>
/// <param name="tvdbLanguage">TVDB data language.</param>
/// <returns>Normalized language.</returns>
public static string? NormalizeLanguageToTvdb(string? language)
public static bool MatchLanguage(string? language, string tvdbLanguage)
{
if (string.IsNullOrWhiteSpace(language))
{
return null;
return false;
}

// Unique case for zh-TW
if (string.Equals(language, "zh-TW", StringComparison.OrdinalIgnoreCase))
{
return "zhtw";
language = "zhtw";
}

// Unique case for pt-BR
if (string.Equals(language, "pt-br", StringComparison.OrdinalIgnoreCase))
{
return "pt";
language = "pt";
}

// to (ISO 639-2)
return TvdbCultureInfo.GetCultureInfo(language)?.ThreeLetterISOLanguageName;
// try to find a match (ISO 639-2)
return TvdbCultureInfo.GetCultureInfo(language)?.ThreeLetterISOLanguageNames?.Contains(tvdbLanguage, StringComparer.OrdinalIgnoreCase) ?? false;
}

/// <summary>
Expand Down