Migrate API to use enums, update properties

This commit is contained in:
Shadowghost 2022-04-18 17:16:50 +02:00
parent 51a85fd7f6
commit 6569118627
13 changed files with 350 additions and 100 deletions

View File

@ -7,12 +7,6 @@ namespace Trakt.Api.DataContracts.Scrobble
/// </summary>
public class SocialMedia
{
/// <summary>
/// Gets or sets a value indicating whether facebook posting should be enabled.
/// </summary>
[JsonPropertyName("facebook")]
public bool Facebook { get; set; }
/// <summary>
/// Gets or sets a value indicating whether twittwe posting should be enabled.
/// </summary>

View File

@ -1,5 +1,6 @@
using System.Text.Json.Serialization;
using Trakt.Api.DataContracts.BaseModel;
using Trakt.Api.Enums;
namespace Trakt.Api.DataContracts.Scrobble
{
@ -18,7 +19,7 @@ namespace Trakt.Api.DataContracts.Scrobble
/// Gets or sets the action.
/// </summary>
[JsonPropertyName("action")]
public string Action { get; set; }
public TraktAction Action { get; set; }
/// <summary>
/// Gets or sets the progress.

View File

@ -1,5 +1,6 @@
using System.Text.Json.Serialization;
using Trakt.Api.DataContracts.BaseModel;
using Trakt.Api.Enums;
namespace Trakt.Api.DataContracts.Sync.Collection
{
@ -18,19 +19,19 @@ namespace Trakt.Api.DataContracts.Sync.Collection
/// Gets or sets the media type.
/// </summary>
[JsonPropertyName("media_type")]
public string MediaType { get; set; }
public TraktMediaType? MediaType { get; set; }
/// <summary>
/// Gets or sets the resolution.
/// </summary>
[JsonPropertyName("resolution")]
public string Resolution { get; set; }
public TraktResolution? Resolution { get; set; }
/// <summary>
/// Gets or sets the audio.
/// </summary>
[JsonPropertyName("audio")]
public string Audio { get; set; }
public TraktAudio? Audio { get; set; }
/// <summary>
/// Gets or sets the amount of audio channels.
@ -38,6 +39,16 @@ namespace Trakt.Api.DataContracts.Sync.Collection
[JsonPropertyName("audio_channels")]
public string AudioChannels { get; set; }
// public bool 3d { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the episode is 3D.
/// </summary>
[JsonPropertyName("3d")]
public bool Is3D { get; set; } = false;
/// <summary>
/// Gets or sets the HDR type.
/// </summary>
[JsonPropertyName("hdr")]
public TraktHdr? Hdr { get; set; }
}
}

View File

@ -1,5 +1,6 @@
using System.Text.Json.Serialization;
using Trakt.Api.DataContracts.BaseModel;
using Trakt.Api.Enums;
namespace Trakt.Api.DataContracts.Sync.Collection
{
@ -18,19 +19,19 @@ namespace Trakt.Api.DataContracts.Sync.Collection
/// Gets or sets the media type.
/// </summary>
[JsonPropertyName("media_type")]
public string MediaType { get; set; }
public TraktMediaType? MediaType { get; set; }
/// <summary>
/// Gets or sets the resolution.
/// </summary>
[JsonPropertyName("resolution")]
public string Resolution { get; set; }
public TraktResolution? Resolution { get; set; }
/// <summary>
/// Gets or sets the audio.
/// </summary>
[JsonPropertyName("audio")]
public string Audio { get; set; }
public TraktAudio? Audio { get; set; }
/// <summary>
/// Gets or sets the amount of audio channels.
@ -38,6 +39,16 @@ namespace Trakt.Api.DataContracts.Sync.Collection
[JsonPropertyName("audio_channels")]
public string AudioChannels { get; set; }
// public bool 3d { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the movie is 3D.
/// </summary>
[JsonPropertyName("3d")]
public bool Is3D { get; set; } = false;
/// <summary>
/// Gets or sets the HDR type.
/// </summary>
[JsonPropertyName("hdr")]
public TraktHdr? Hdr { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using Trakt.Api.Enums;
namespace Trakt.Api.DataContracts.Users.Collection
{
@ -11,19 +12,19 @@ namespace Trakt.Api.DataContracts.Users.Collection
/// Gets or sets the media type.
/// </summary>
[JsonPropertyName("media_type")]
public string MediaType { get; set; }
public TraktMediaType? MediaType { get; set; }
/// <summary>
/// Gets or sets the resolution.
/// </summary>
[JsonPropertyName("resolution")]
public string Resolution { get; set; }
public TraktResolution? Resolution { get; set; }
/// <summary>
/// Gets or sets the audio.
/// </summary>
[JsonPropertyName("audio")]
public string Audio { get; set; }
public TraktAudio? Audio { get; set; }
/// <summary>
/// Gets or sets the amount of audio channels.
@ -31,6 +32,16 @@ namespace Trakt.Api.DataContracts.Users.Collection
[JsonPropertyName("audio_channels")]
public string AudioChannels { get; set; }
// public bool 3d { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the movie is 3D.
/// </summary>
[JsonPropertyName("3d")]
public bool Is3D { get; set; } = false;
/// <summary>
/// Gets or sets the HDR type.
/// </summary>
[JsonPropertyName("hdr")]
public TraktHdr? Hdr { get; set; }
}
}

View File

@ -0,0 +1,25 @@
#pragma warning disable SA1300
namespace Trakt.Api.Enums
{
/// <summary>
/// Enum TraktHdr.
/// </summary>
public enum TraktAction
{
/// <summary>
/// Check-In action.
/// </summary>
checkin,
/// <summary>
/// Scrbble action.
/// </summary>
scrobble,
/// <summary>
/// Watch action.
/// </summary>
watch
}
}

View File

@ -1,57 +1,27 @@
#pragma warning disable SA1300
#pragma warning disable CA1707
namespace Trakt.Model
namespace Trakt.Api.Enums
{
/// <summary>
/// Enum TraktAudio.
/// </summary>
public enum TraktAudio
{
/// <summary>
/// LPCM audio.
/// </summary>
lpcm,
/// <summary>
/// MP3 audio.
/// </summary>
mp3,
/// <summary>
/// AAC audio.
/// </summary>
aac,
/// <summary>
/// DTS audio.
/// Auro 3D audio.
/// </summary>
dts,
auro_3d,
/// <summary>
/// DTS-HD Master Audio audio.
/// Dolby Atmos audio.
/// </summary>
dts_ma,
/// <summary>
/// FLAC audio.
/// </summary>
flac,
/// <summary>
/// OGG audio.
/// </summary>
ogg,
/// <summary>
/// WMA audio.
/// </summary>
wma,
/// <summary>
/// Dolby ProLogic audio.
/// </summary>
dolby_prologic,
dolby_atmos,
/// <summary>
/// Dolby Digital audio.
@ -63,9 +33,74 @@ namespace Trakt.Model
/// </summary>
dolby_digital_plus,
/// <summary>
/// Dolby Digital Plus Atmos audio.
/// </summary>
dolby_digital_plus_atmos,
/// <summary>
/// Dolby ProLogic audio.
/// </summary>
dolby_prologic,
/// <summary>
/// Dolby TrueHD audio.
/// </summary>
dolby_truehd
dolby_truehd,
/// <summary>
/// DTS audio.
/// </summary>
dts,
/// <summary>
/// DTS-HD Master Audio audio.
/// </summary>
dts_ma,
/// <summary>
/// DTS-HR audio.
/// </summary>
dts_hr,
/// <summary>
/// DTS-X audio.
/// </summary>
dts_x,
/// <summary>
/// FLAC audio.
/// </summary>
flac,
/// <summary>
/// LPCM audio.
/// </summary>
lpcm,
/// <summary>
/// MP2 audio.
/// </summary>
mp2,
/// <summary>
/// MP3 audio.
/// </summary>
mp3,
/// <summary>
/// OGG audio.
/// </summary>
ogg,
/// <summary>
/// Opus audio.
/// </summary>
ogg_opus,
/// <summary>
/// WMA audio.
/// </summary>
wma,
}
}

View File

@ -0,0 +1,31 @@
#pragma warning disable SA1300
#pragma warning disable CA1707
namespace Trakt.Api.Enums
{
/// <summary>
/// Enum TraktHdr.
/// </summary>
public enum TraktHdr
{
/// <summary>
/// Dolby Vision HDR.
/// </summary>
dolby_vision,
/// <summary>
/// HDR10 HDR.
/// </summary>
hdr10,
/// <summary>
/// HDR10+ HDR.
/// </summary>
hdr10_plus,
/// <summary>
/// HLG HDR.
/// </summary>
hlg
}
}

View File

@ -0,0 +1,26 @@
#pragma warning disable SA1300
#pragma warning disable CA1707
namespace Trakt.Api.Enums
{
/// <summary>
/// Enum TraktItemType.
/// </summary>
public enum TraktItemType
{
/// <summary>
/// Episode type.
/// </summary>
episode,
/// <summary>
/// Movie type.
/// </summary>
movie,
/// <summary>
/// Show type.
/// </summary>
show
}
}

View File

@ -0,0 +1,51 @@
#pragma warning disable SA1300
#pragma warning disable CA1707
namespace Trakt.Api.Enums
{
/// <summary>
/// Enum TraktMediaType.
/// </summary>
public enum TraktMediaType
{
/// <summary>
/// Betamax media.
/// </summary>
betamax,
/// <summary>
/// BluRay media.
/// </summary>
bluray,
/// <summary>
/// Digital media.
/// </summary>
digital,
/// <summary>
/// DVD media.
/// </summary>
dvd,
/// <summary>
/// HD-DVD media.
/// </summary>
hddvd,
/// <summary>
/// LaserDisc media.
/// </summary>
lasterdisc,
/// <summary>
/// VCD media.
/// </summary>
vcd,
/// <summary>
/// VHS media.
/// </summary>
vhs
}
}

View File

@ -0,0 +1,31 @@
#pragma warning disable SA1300
#pragma warning disable CA1707
namespace Trakt.Api.Enums
{
/// <summary>
/// Enum TraktResolution.
/// </summary>
public enum TraktResolution
{
/// <summary>
/// HD 720p resolution.
/// </summary>
hd_720p,
/// <summary>
/// HD 1080p resolution.
/// </summary>
hd_1080p,
/// <summary>
/// SD 480p resolution.
/// </summary>
sd_480p,
/// <summary>
/// UHD 4k resolution.
/// </summary>
uhd_4k
}
}

View File

@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
@ -279,6 +278,7 @@ namespace Trakt.Api
Year = m.ProductionYear,
Ids = GetTraktIMDBTMDBIds<Movie, TraktMovieId>(m)
};
if (traktUser.ExportMediaInfo)
{
traktMovieCollected.AudioChannels = audioStream.GetAudioChannels();
@ -288,8 +288,8 @@ namespace Trakt.Api
return traktMovieCollected;
}).ToList();
var url = eventType == EventType.Add ? TraktUris.SyncCollectionAdd : TraktUris.SyncCollectionRemove;
var url = eventType == EventType.Add ? TraktUris.SyncCollectionAdd : TraktUris.SyncCollectionRemove;
var responses = new List<TraktSyncResponse>();
var chunks = moviesPayload.ToChunks(100);
foreach (var chunk in chunks)
@ -356,7 +356,7 @@ namespace Trakt.Api
var showPayload = new List<TraktShowCollected>();
foreach (Episode episode in episodes)
{
var audioStream = episode.GetMediaStreams().FirstOrDefault(x => x.Type == MediaStreamType.Audio);
var audioStream = episode.GetMediaStreams().FirstOrDefault(stream => stream.Type == MediaStreamType.Audio);
if (useProviderIDs && HasAnyProviderTvIds(episode) &&
(!episode.IndexNumber.HasValue || !episode.IndexNumberEnd.HasValue ||
@ -369,10 +369,11 @@ namespace Trakt.Api
};
if (traktUser.ExportMediaInfo)
{
// traktEpisodeCollected.Is3D = episode.Is3D;
traktEpisodeCollected.AudioChannels = audioStream.GetAudioChannels();
traktEpisodeCollected.Audio = audioStream.GetCodecRepresetation();
traktEpisodeCollected.Resolution = episode.GetDefaultVideoStream().GetResolution();
traktEpisodeCollected.Is3D = episode.Is3D;
traktEpisodeCollected.Hdr = episode.GetDefaultVideoStream().GetHdr();
}
episodesPayload.Add(traktEpisodeCollected);
@ -393,8 +394,7 @@ namespace Trakt.Api
showPayload.Add(syncShow);
}
var syncSeason =
syncShow.Seasons.FirstOrDefault(ss => ss.Number == episode.GetSeasonNumber());
var syncSeason = syncShow.Seasons.FirstOrDefault(season => season.Number == episode.GetSeasonNumber());
if (syncSeason == null)
{
syncSeason = new TraktSeasonCollected
@ -422,6 +422,7 @@ namespace Trakt.Api
CollectedAt = episode.DateCreated.ToISO8601(),
Ids = ids
};
if (traktUser.ExportMediaInfo)
{
// traktEpisodeCollected.Is3D = episode.Is3D;
@ -692,6 +693,7 @@ namespace Trakt.Api
var lastPlayedDate = seen
? _userDataManager.GetUserData(new Guid(traktUser.LinkedMbUserId), m).LastPlayedDate
: null;
return new TraktMovieWatched
{
Title = m.Name,
@ -709,8 +711,8 @@ namespace Trakt.Api
{
Movies = chunk.ToList()
};
var url = seen ? TraktUris.SyncWatchedHistoryAdd : TraktUris.SyncWatchedHistoryRemove;
var url = seen ? TraktUris.SyncWatchedHistoryAdd : TraktUris.SyncWatchedHistoryRemove;
var response = await PostToTrakt<TraktSyncResponse>(url, data, traktUser, cancellationToken).ConfigureAwait(false);
if (response != null)
{
@ -759,7 +761,12 @@ namespace Trakt.Api
private async Task<TraktSyncResponse> SendEpisodePlaystateUpdatesInternalAsync(IEnumerable<Episode> episodeChunk, TraktUser traktUser, bool seen, CancellationToken cancellationToken, bool useProviderIDs = true)
{
var data = new TraktSyncWatched { Episodes = new List<TraktEpisodeWatched>(), Shows = new List<TraktShowWatched>() };
var data = new TraktSyncWatched
{
Episodes = new List<TraktEpisodeWatched>(),
Shows = new List<TraktShowWatched>()
};
foreach (var episode in episodeChunk)
{
var lastPlayedDate = seen
@ -788,6 +795,7 @@ namespace Trakt.Api
Ids = GetTraktTvIds<Series, TraktShowId>(episode.Series),
Seasons = new List<TraktSeasonWatched>()
};
data.Shows.Add(syncShow);
}
@ -799,6 +807,7 @@ namespace Trakt.Api
Number = episode.GetSeasonNumber(),
Episodes = new List<TraktEpisodeWatched>()
};
syncShow.Seasons.Add(syncSeason);
}
@ -820,7 +829,7 @@ namespace Trakt.Api
if (useProviderIDs && response.NotFound.Episodes.Count > 0)
{
// Send subset of episodes back to trakt.tv to try without ids
_logger.LogDebug("Resend episodes playstate update, without episode IDs");
_logger.LogDebug("Resend episodes playstate update, without episode ids");
await SendEpisodePlaystateUpdatesInternalAsync(FindNotFoundEpisodes(episodeChunk, response), traktUser, seen, cancellationToken, false).ConfigureAwait(false);
}
@ -832,13 +841,13 @@ namespace Trakt.Api
// Episodes not found. If using ids, try again without them
List<Episode> episodes = new List<Episode>();
// Build a list of unfound episodes with ids
foreach (TraktEpisode traktEpisode in traktSyncResponse.NotFound.Episodes.Where(i => HasAnyProviderTvIds(i.Ids)))
foreach (TraktEpisode traktEpisode in traktSyncResponse.NotFound.Episodes.Where(episode => HasAnyProviderTvIds(episode.Ids)))
{
// Find matching episode in Jellyfin based on provider ids
var notFoundEpisode = episodeChunk.First(e => e.GetProviderId(MetadataProvider.Imdb) == traktEpisode.Ids.Imdb
|| e.GetProviderId(MetadataProvider.Tmdb) == traktEpisode.Ids.Tmdb?.ToString(CultureInfo.InvariantCulture)
|| e.GetProviderId(MetadataProvider.Tvdb) == traktEpisode.Ids.Tvdb?.ToString(CultureInfo.InvariantCulture)
|| e.GetProviderId(MetadataProvider.TvRage) == traktEpisode.Ids.Tvrage?.ToString(CultureInfo.InvariantCulture));
var notFoundEpisode = episodeChunk.FirstOrDefault(episode => episode.GetProviderId(MetadataProvider.Imdb) == traktEpisode.Ids.Imdb
|| episode.GetProviderId(MetadataProvider.Tmdb) == traktEpisode.Ids.Tmdb?.ToString(CultureInfo.InvariantCulture)
|| episode.GetProviderId(MetadataProvider.Tvdb) == traktEpisode.Ids.Tvdb?.ToString(CultureInfo.InvariantCulture)
|| episode.GetProviderId(MetadataProvider.TvRage) == traktEpisode.Ids.Tvrage?.ToString(CultureInfo.InvariantCulture));
if (notFoundEpisode != null)
{

View File

@ -6,8 +6,8 @@ using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model.Entities;
using Trakt.Api.DataContracts.Users.Collection;
using Trakt.Api.Enums;
using Trakt.Helpers;
using Trakt.Model;
namespace Trakt
{
@ -37,17 +37,17 @@ namespace Trakt
/// <param name="metadata">String to convert to int.</param>
/// <returns><see cref="bool"/> indicating if the provided <see cref="TraktMetadata"/> is empty.</returns>
public static bool IsEmpty(this TraktMetadata metadata)
=> string.IsNullOrEmpty(metadata.MediaType)
&& string.IsNullOrEmpty(metadata.Resolution)
&& string.IsNullOrEmpty(metadata.Audio)
=> metadata.MediaType == null
&& metadata.Resolution == null
&& metadata.Audio == null
&& string.IsNullOrEmpty(metadata.AudioChannels);
/// <summary>
/// Gets the trakt.tv codec representation of a <see cref="MediaStream"/>.
/// </summary>
/// <param name="audioStream">The <see cref="MediaStream"/>.</param>
/// <returns>string.</returns>
public static string GetCodecRepresetation(this MediaStream audioStream)
/// <returns>TraktAudio.</returns>
public static TraktAudio? GetCodecRepresetation(this MediaStream audioStream)
{
var audio = audioStream != null && !string.IsNullOrEmpty(audioStream.Codec)
? audioStream.Codec.ToLowerInvariant().Replace(' ', '_')
@ -55,26 +55,26 @@ namespace Trakt
switch (audio)
{
case "truehd":
return TraktAudio.dolby_truehd.ToString();
return TraktAudio.dolby_truehd;
case "dts":
case "dca":
return TraktAudio.dts.ToString();
return TraktAudio.dts;
case "dtshd":
return TraktAudio.dts_ma.ToString();
return TraktAudio.dts_ma;
case "ac3":
return TraktAudio.dolby_digital.ToString();
return TraktAudio.dolby_digital;
case "aac":
return TraktAudio.aac.ToString();
return TraktAudio.aac;
case "mp2":
return TraktAudio.mp3.ToString();
return TraktAudio.mp3;
case "pcm":
return TraktAudio.lpcm.ToString();
return TraktAudio.lpcm;
case "ogg":
return TraktAudio.ogg.ToString();
return TraktAudio.ogg;
case "wma":
return TraktAudio.wma.ToString();
return TraktAudio.wma;
case "flac":
return TraktAudio.flac.ToString();
return TraktAudio.flac;
default:
return null;
}
@ -91,19 +91,23 @@ namespace Trakt
var audioStream = movie.GetMediaStreams().FirstOrDefault(x => x.Type == MediaStreamType.Audio);
var resolution = movie.GetDefaultVideoStream().GetResolution();
var is3D = movie.Is3D;
var hdr = movie.GetDefaultVideoStream().GetHdr();
var audio = GetCodecRepresetation(audioStream);
var audioChannels = audioStream.GetAudioChannels();
if (collectedMovie.Metadata == null || collectedMovie.Metadata.IsEmpty())
{
return !string.IsNullOrEmpty(resolution)
|| !string.IsNullOrEmpty(audio)
return resolution != null
|| audio != null
|| !string.IsNullOrEmpty(audioChannels);
}
return collectedMovie.Metadata.Audio != audio
|| collectedMovie.Metadata.AudioChannels != audioChannels
|| collectedMovie.Metadata.Resolution != resolution;
|| collectedMovie.Metadata.Resolution != resolution
|| collectedMovie.Metadata.Is3D != is3D
|| collectedMovie.Metadata.Hdr != hdr;
}
/// <summary>
@ -111,7 +115,7 @@ namespace Trakt
/// </summary>
/// <param name="videoStream">The <see cref="MediaStream"/>.</param>
/// <returns>string.</returns>
public static string GetResolution(this MediaStream videoStream)
public static TraktResolution? GetResolution(this MediaStream videoStream)
{
if (videoStream == null)
{
@ -125,27 +129,37 @@ namespace Trakt
if (videoStream.Width.Value >= 3800)
{
return "uhd_4k";
return TraktResolution.uhd_4k;
}
if (videoStream.Width.Value >= 1900)
{
return "hd_1080p";
return TraktResolution.hd_1080p;
}
if (videoStream.Width.Value >= 1270)
{
return "hd_720p";
return TraktResolution.hd_720p;
}
if (videoStream.Width.Value >= 700)
{
return "sd_480p";
return TraktResolution.sd_480p;
}
return null;
}
/// <summary>
/// Gets the HDR type of a <see cref="MediaStream"/>.
/// </summary>
/// <param name="videoStream">The <see cref="MediaStream"/>.</param>
/// <returns>string.</returns>
public static TraktHdr? GetHdr(this MediaStream videoStream)
{
return null;
}
/// <summary>
/// Gets the ISO-8620 representation of a <see cref="DateTime"/>.
/// </summary>