|
|
|
|
@ -1,6 +1,5 @@
@@ -1,6 +1,5 @@
|
|
|
|
|
using System; |
|
|
|
|
using System.IO; |
|
|
|
|
using System.Text.RegularExpressions; |
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
using System.Xml.Serialization; |
|
|
|
|
using ErsatzTV.Core.Domain; |
|
|
|
|
@ -13,6 +12,9 @@ namespace ErsatzTV.Core.Metadata
@@ -13,6 +12,9 @@ namespace ErsatzTV.Core.Metadata
|
|
|
|
|
{ |
|
|
|
|
public class LocalMetadataProvider : ILocalMetadataProvider |
|
|
|
|
{ |
|
|
|
|
private static readonly XmlSerializer MovieSerializer = new(typeof(MovieNfo)); |
|
|
|
|
private static readonly XmlSerializer TvShowSerializer = new(typeof(TvShowEpisodeNfo)); |
|
|
|
|
|
|
|
|
|
private readonly IMediaItemRepository _mediaItemRepository; |
|
|
|
|
|
|
|
|
|
public LocalMetadataProvider(IMediaItemRepository mediaItemRepository) => |
|
|
|
|
@ -53,51 +55,65 @@ namespace ErsatzTV.Core.Metadata
@@ -53,51 +55,65 @@ namespace ErsatzTV.Core.Metadata
|
|
|
|
|
return None; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var tvShowSerializer = new XmlSerializer(typeof(TvShowEpisodeNfo)); |
|
|
|
|
var movieSerializer = new XmlSerializer(typeof(MovieNfo)); |
|
|
|
|
|
|
|
|
|
TryAsync<object> tvShowAttempt = TryAsync( |
|
|
|
|
async () => |
|
|
|
|
{ |
|
|
|
|
await using FileStream fileStream = File.Open(nfoFileName, FileMode.Open); |
|
|
|
|
return tvShowSerializer.Deserialize(fileStream); |
|
|
|
|
}); |
|
|
|
|
TryAsync<object> movieAttempt = TryAsync( |
|
|
|
|
async () => |
|
|
|
|
{ |
|
|
|
|
await using FileStream fileStream = File.Open(nfoFileName, FileMode.Open); |
|
|
|
|
return movieSerializer.Deserialize(fileStream); |
|
|
|
|
}); |
|
|
|
|
return await choice(tvShowAttempt, movieAttempt).Match<object, Option<MediaMetadata>>( |
|
|
|
|
result => |
|
|
|
|
{ |
|
|
|
|
switch (result) |
|
|
|
|
if (!(mediaItem.Source is LocalMediaSource localMediaSource)) |
|
|
|
|
{ |
|
|
|
|
return None; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return localMediaSource.MediaType switch |
|
|
|
|
{ |
|
|
|
|
MediaType.Movie => await LoadMovieMetadata(nfoFileName), |
|
|
|
|
MediaType.TvShow => await LoadTvShowMetadata(nfoFileName), |
|
|
|
|
_ => None |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static async Task<Option<MediaMetadata>> LoadTvShowMetadata(string nfoFileName) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
await using FileStream fileStream = File.Open(nfoFileName, FileMode.Open); |
|
|
|
|
Option<TvShowEpisodeNfo> maybeNfo = TvShowSerializer.Deserialize(fileStream) as TvShowEpisodeNfo; |
|
|
|
|
return maybeNfo.Match<Option<MediaMetadata>>( |
|
|
|
|
nfo => new MediaMetadata |
|
|
|
|
{ |
|
|
|
|
case TvShowEpisodeNfo nfo: |
|
|
|
|
return new MediaMetadata |
|
|
|
|
{ |
|
|
|
|
MediaType = MediaType.TvShow, |
|
|
|
|
Title = nfo.ShowTitle, |
|
|
|
|
Subtitle = nfo.Title, |
|
|
|
|
Description = nfo.Outline, |
|
|
|
|
EpisodeNumber = nfo.Episode, |
|
|
|
|
SeasonNumber = nfo.Season, |
|
|
|
|
Aired = GetAired(nfo.Aired) |
|
|
|
|
}; |
|
|
|
|
case MovieNfo nfo: |
|
|
|
|
return new MediaMetadata |
|
|
|
|
{ |
|
|
|
|
MediaType = MediaType.Movie, |
|
|
|
|
Title = nfo.Title, |
|
|
|
|
Description = nfo.Outline, |
|
|
|
|
ContentRating = nfo.ContentRating, |
|
|
|
|
Aired = GetAired(nfo.Premiered) |
|
|
|
|
}; |
|
|
|
|
default: |
|
|
|
|
return None; |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
None); |
|
|
|
|
MediaType = MediaType.TvShow, |
|
|
|
|
Title = nfo.ShowTitle, |
|
|
|
|
Subtitle = nfo.Title, |
|
|
|
|
Description = nfo.Outline, |
|
|
|
|
EpisodeNumber = nfo.Episode, |
|
|
|
|
SeasonNumber = nfo.Season, |
|
|
|
|
Aired = GetAired(nfo.Aired) |
|
|
|
|
}, |
|
|
|
|
None); |
|
|
|
|
} |
|
|
|
|
catch (Exception) |
|
|
|
|
{ |
|
|
|
|
return None; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static async Task<Option<MediaMetadata>> LoadMovieMetadata(string nfoFileName) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
await using FileStream fileStream = File.Open(nfoFileName, FileMode.Open); |
|
|
|
|
Option<MovieNfo> maybeNfo = MovieSerializer.Deserialize(fileStream) as MovieNfo; |
|
|
|
|
return maybeNfo.Match<Option<MediaMetadata>>( |
|
|
|
|
nfo => new MediaMetadata |
|
|
|
|
{ |
|
|
|
|
MediaType = MediaType.Movie, |
|
|
|
|
Title = nfo.Title, |
|
|
|
|
Description = nfo.Outline, |
|
|
|
|
ContentRating = nfo.ContentRating, |
|
|
|
|
Aired = GetAired(nfo.Premiered) |
|
|
|
|
}, |
|
|
|
|
None); |
|
|
|
|
} |
|
|
|
|
catch (Exception) |
|
|
|
|
{ |
|
|
|
|
return None; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static DateTime? GetAired(string aired) |
|
|
|
|
|