Skip to content

Commit

Permalink
Move to IExternalUrlProvider (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
scampower3 authored Oct 28, 2024
1 parent ec984e9 commit d8baf85
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class TvdbEpisodeExternalId : IExternalId
public ExternalIdMediaType? Type => ExternalIdMediaType.Episode;

/// <inheritdoc />
public string UrlFormatString => TvdbUtils.TvdbBaseUrl + "?tab=episode&id={0}";
public string? UrlFormatString => null;

/// <inheritdoc />
public bool Supports(IHasProviderIds item) => item is Episode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Jellyfin.Plugin.Tvdb.Providers.ExternalId
public class TvdbMovieSlugExternalId : IExternalId
{
/// <inheritdoc />
public string ProviderName => TvdbPlugin.ProviderName;
public string ProviderName => TvdbPlugin.ProviderName + " Slug";

/// <inheritdoc />
public string Key => TvdbPlugin.SlugProviderId;
Expand All @@ -18,7 +18,7 @@ public class TvdbMovieSlugExternalId : IExternalId
public ExternalIdMediaType? Type => ExternalIdMediaType.Movie;

/// <inheritdoc />
public string? UrlFormatString => TvdbUtils.TvdbBaseUrl + "movies/{0}";
public string? UrlFormatString => null;

/// <inheritdoc />
public bool Supports(IHasProviderIds item) => item is Movie;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class TvdbPersonExternalId : IExternalId
public ExternalIdMediaType? Type => ExternalIdMediaType.Person;

/// <inheritdoc />
public string? UrlFormatString => TvdbUtils.TvdbBaseUrl + "people/{0}";
public string? UrlFormatString => null;

/// <inheritdoc />
public bool Supports(IHasProviderIds item) => item is Person;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class TvdbSeasonExternalId : IExternalId
public ExternalIdMediaType? Type => ExternalIdMediaType.Season;

/// <inheritdoc />
public string UrlFormatString => TvdbUtils.TvdbBaseUrl + "dereferrer/season/{0}";
public string? UrlFormatString => null;

/// <inheritdoc />
public bool Supports(IHasProviderIds item) => item is Season;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
namespace Jellyfin.Plugin.Tvdb.Providers.ExternalId
{
/// <inheritdoc />
public class TvdbExternalId : IExternalId
public class TvdbSeriesExternalId : IExternalId
{
/// <inheritdoc />
public string ProviderName => TvdbPlugin.ProviderName;
public string ProviderName => TvdbPlugin.ProviderName + " Numerical";

/// <inheritdoc />
public string Key => TvdbPlugin.ProviderId;

/// <inheritdoc />
public ExternalIdMediaType? Type => null;
public ExternalIdMediaType? Type => ExternalIdMediaType.Series;

/// <inheritdoc />
public string UrlFormatString => TvdbUtils.TvdbBaseUrl + "?tab=series&id={0}";
public string? UrlFormatString => null;

/// <inheritdoc />
public bool Supports(IHasProviderIds item) => item is Series;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;

namespace Jellyfin.Plugin.Tvdb.Providers.ExternalId
{
/// <inheritdoc />
public class TvdbSeriesSlugExternalId : IExternalId
{
/// <inheritdoc />
public string ProviderName => TvdbPlugin.ProviderName + " Slug";

/// <inheritdoc />
public string Key => TvdbPlugin.SlugProviderId;

/// <inheritdoc />
public ExternalIdMediaType? Type => ExternalIdMediaType.Series;

/// <inheritdoc />
public string? UrlFormatString => null;

/// <inheritdoc />
public bool Supports(IHasProviderIds item) => item is Series;
}
}
89 changes: 89 additions & 0 deletions Jellyfin.Plugin.Tvdb/Providers/TvdbExternalUrlProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;

namespace Jellyfin.Plugin.Tvdb.Providers
{
/// <summary>
/// Provides external urls for TheTVDB.
/// </summary>
public class TvdbExternalUrlProvider : IExternalUrlProvider
{
private readonly FrozenSet<string> _supportedOrders = new[] { "official", "regional", "alternate", "altdvd", "dvd", "absolute" }.ToFrozenSet();

/// <inheritdoc/>
public string Name => TvdbPlugin.ProviderName;

/// <inheritdoc/>
public IEnumerable<string> GetExternalUrls(BaseItem item)
{
var externalId = item.GetProviderId(TvdbPlugin.ProviderId);
var slugId = item.GetProviderId(TvdbPlugin.SlugProviderId);

switch (item)
{
case Series:
if (string.IsNullOrEmpty(slugId) && !string.IsNullOrEmpty(externalId))
{
yield return TvdbUtils.TvdbBaseUrl + $"?tab=series&id={externalId}";
}
else if (!string.IsNullOrEmpty(slugId))
{
yield return TvdbUtils.TvdbBaseUrl + $"series/{slugId}";
}

break;
case Season season:
season.Series.ProviderIds.TryGetValue(TvdbPlugin.SlugProviderId, out var seriesSlugId);
var displayOrder = season.Series.DisplayOrder;
if (string.IsNullOrEmpty(displayOrder))
{
displayOrder = "official";
}

if (_supportedOrders.Contains(displayOrder) && !string.IsNullOrEmpty(seriesSlugId))
{
yield return TvdbUtils.TvdbBaseUrl + $"series/{seriesSlugId}/seasons/{displayOrder}/{item.IndexNumber}";
}
else if (string.Equals(displayOrder, "official", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(externalId))
{
// This url format only works for official order
yield return TvdbUtils.TvdbBaseUrl + $"dereferrer/season/{externalId}";
}

break;
case Episode episode:
episode.Series.ProviderIds.TryGetValue(TvdbPlugin.SlugProviderId, out seriesSlugId);
if (!string.IsNullOrEmpty(seriesSlugId) && !string.IsNullOrEmpty(externalId))
{
yield return TvdbUtils.TvdbBaseUrl + $"series/{seriesSlugId}/episodes/{externalId}";
}
else if (!string.IsNullOrEmpty(externalId))
{
yield return TvdbUtils.TvdbBaseUrl + $"?tab=episode&id={externalId}";
}

break;
case Movie:
if (!string.IsNullOrEmpty(slugId))
{
yield return TvdbUtils.TvdbBaseUrl + $"movies/{slugId}";
}

break;
case Person:
if (!string.IsNullOrEmpty(externalId))
{
yield return TvdbUtils.TvdbBaseUrl + $"people/{externalId}";
}

break;
}
}
}
}
4 changes: 3 additions & 1 deletion Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,11 @@ private void MapSeriesToResult(MetadataResult<Series> result, SeriesExtendedReco
result.ResultLanguage = info.MetadataLanguage;
series.AirDays = TvdbUtils.GetAirDays(tvdbSeries.AirsDays).ToArray();
series.AirTime = tvdbSeries.AirsTime;
// series.CommunityRating = (float?)tvdbSeries.SiteRating;
// Attempts to default to USA if not found
series.OfficialRating = tvdbSeries.ContentRatings?.FirstOrDefault(x => string.Equals(x.Country, TvdbCultureInfo.GetCountryInfo(info.MetadataCountryCode)?.ThreeLetterISORegionName, StringComparison.OrdinalIgnoreCase))?.Name ?? tvdbSeries.ContentRatings?.FirstOrDefault(x => string.Equals(x.Country, "usa", StringComparison.OrdinalIgnoreCase))?.Name;

series.SetProviderIdIfHasValue(TvdbPlugin.SlugProviderId, tvdbSeries.Slug);

if (tvdbSeries.Lists is not null && tvdbSeries.Lists is JsonElement jsonElement)
{
var collections = jsonElement.Deserialize<List<object>>();
Expand Down

0 comments on commit d8baf85

Please sign in to comment.