From 78201ad08b6e8fb929ff295db436ed9836bbe83e Mon Sep 17 00:00:00 2001 From: Lukas Nagel Date: Tue, 24 Sep 2024 20:14:14 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20Metadata=20retrieval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Metadata collection #3 --- OpenMediaServer.Test/APIs/OMDbAPIShould.cs | 31 ++++++++++++ .../OpenMediaServer.Test.csproj | 2 + OpenMediaServer.Test/UnitTest1.cs | 10 ---- OpenMediaServer/APIs/OMDbAPI.cs | 50 +++++++++++++++++++ OpenMediaServer/DTOs/OMDbModel.cs | 38 ++++++++++++++ OpenMediaServer/Extensions/ModelExtensions.cs | 46 +++++++++++++++++ .../Interfaces/APIs/IMetadataAPI.cs | 9 ++++ OpenMediaServer/Models/InventoryItem.cs | 3 +- OpenMediaServer/Models/MetadataModel.cs | 38 ++++++++++++++ OpenMediaServer/Program.cs | 5 ++ OpenMediaServer/Services/InventoryService.cs | 12 +++-- OpenMediaServer/appsettings.json | 5 +- 12 files changed, 234 insertions(+), 15 deletions(-) create mode 100644 OpenMediaServer.Test/APIs/OMDbAPIShould.cs delete mode 100644 OpenMediaServer.Test/UnitTest1.cs create mode 100644 OpenMediaServer/APIs/OMDbAPI.cs create mode 100644 OpenMediaServer/DTOs/OMDbModel.cs create mode 100644 OpenMediaServer/Extensions/ModelExtensions.cs create mode 100644 OpenMediaServer/Interfaces/APIs/IMetadataAPI.cs create mode 100644 OpenMediaServer/Models/MetadataModel.cs diff --git a/OpenMediaServer.Test/APIs/OMDbAPIShould.cs b/OpenMediaServer.Test/APIs/OMDbAPIShould.cs new file mode 100644 index 0000000..1b83d75 --- /dev/null +++ b/OpenMediaServer.Test/APIs/OMDbAPIShould.cs @@ -0,0 +1,31 @@ +using System; +using Microsoft.Extensions.Logging; +using Moq; +using OpenMediaServer.APIs; +using Shouldly; + +namespace OpenMediaServer.Test.APIs; + +public class OMDbAPIShould +{ + public OMDbAPI Api { get; set; } + + public OMDbAPIShould() + { + var logger = new Mock>(); + Api = new OMDbAPI(logger: logger.Object, httpClient: new HttpClient()); + } + +#if DEBUG + + [Fact] + public async Task GetMetadata() + { + var model = await Api.GetMetadata("Hunger Games", ""); + + model.ShouldNotBeNull(); + } + +# endif + +} diff --git a/OpenMediaServer.Test/OpenMediaServer.Test.csproj b/OpenMediaServer.Test/OpenMediaServer.Test.csproj index 56fe012..f38826b 100644 --- a/OpenMediaServer.Test/OpenMediaServer.Test.csproj +++ b/OpenMediaServer.Test/OpenMediaServer.Test.csproj @@ -11,6 +11,8 @@ + + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/OpenMediaServer.Test/UnitTest1.cs b/OpenMediaServer.Test/UnitTest1.cs deleted file mode 100644 index a9cb666..0000000 --- a/OpenMediaServer.Test/UnitTest1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace OpenMediaServer.Test; - -public class UnitTest1 -{ - [Fact] - public void Test1() - { - - } -} \ No newline at end of file diff --git a/OpenMediaServer/APIs/OMDbAPI.cs b/OpenMediaServer/APIs/OMDbAPI.cs new file mode 100644 index 0000000..cdf668c --- /dev/null +++ b/OpenMediaServer/APIs/OMDbAPI.cs @@ -0,0 +1,50 @@ +using System; +using OpenMediaServer.DTOs; +using OpenMediaServer.Extensions; +using OpenMediaServer.Interfaces.APIs; +using OpenMediaServer.Models; + +namespace OpenMediaServer.APIs; + +public class OMDbAPI : IMetadataAPI +{ + private readonly ILogger _logger; + private readonly HttpClient _httpClient; + + public OMDbAPI(ILogger logger, HttpClient httpClient) + { + _logger = logger; + _httpClient = httpClient; + } + + public async Task GetMetadata(string name, string? apiKey, bool fullPlot = false) + { + if (string.IsNullOrEmpty(apiKey)) + { + _logger.LogWarning("ApiKey null or empty. Cannot use OMDbAPI."); + return null; + } + + string plot = "short"; + + if (fullPlot) + { + plot = "full"; + } + + var message = await _httpClient.GetAsync($"http://www.omdbapi.com/?apikey={apiKey}&t={name}&plot={plot}"); + + if (message.IsSuccessStatusCode) + { + var model = await message.Content.ReadFromJsonAsync(); + + return model?.ToMetadataItem(); + } + else + { + _logger.LogWarning("OMDb could not retrieve metadata with error: {ErrorCode} and message: {Message}", message.StatusCode, message.ReasonPhrase); + } + + return null; + } +} diff --git a/OpenMediaServer/DTOs/OMDbModel.cs b/OpenMediaServer/DTOs/OMDbModel.cs new file mode 100644 index 0000000..d441e5e --- /dev/null +++ b/OpenMediaServer/DTOs/OMDbModel.cs @@ -0,0 +1,38 @@ +using System; + +namespace OpenMediaServer.DTOs; + +public class OMDbModel +{ + public string Title { get; set; } + public string Year { get; set; } + public string Rated { get; set; } + public string Released { get; set; } + public string Runtime { get; set; } + public string Genre { get; set; } + public string Director { get; set; } + public string Writer { get; set; } + public string Actors { get; set; } + public string Plot { get; set; } + public string Language { get; set; } + public string Country { get; set; } + public string Awards { get; set; } + public string Poster { get; set; } + public List Ratings { get; set; } + public string Metascore { get; set; } + public string ImdbRating { get; set; } + public string ImdbVotes { get; set; } + public string ImdbID { get; set; } + public string Type { get; set; } + public string DVD { get; set; } + public string BoxOffice { get; set; } + public string Production { get; set; } + public string Website { get; set; } + public string Response { get; set; } +} + +public class Rating +{ + public string Source { get; set; } + public string Value { get; set; } +} diff --git a/OpenMediaServer/Extensions/ModelExtensions.cs b/OpenMediaServer/Extensions/ModelExtensions.cs new file mode 100644 index 0000000..ab56fae --- /dev/null +++ b/OpenMediaServer/Extensions/ModelExtensions.cs @@ -0,0 +1,46 @@ +using System; +using OpenMediaServer.DTOs; +using OpenMediaServer.Models; + +namespace OpenMediaServer.Extensions; + +public static class ModelExtensions +{ + public static MetadataModel ToMetadataItem(this OMDbModel model) + { + var metadataItem = new MetadataModel() + { + Title = model.Title, + Year = model.Year, + Rated = model.Rated, + Released = model.Released, + Runtime = model.Runtime, + Genre = model.Genre, + Director = model.Director, + Writer = model.Writer, + Actors = model.Actors, + Plot = model.Plot, + Language = model.Language, + Country = model.Country, + Awards = model.Awards, + Poster = model.Poster, + Ratings = model.Ratings?.ConvertAll(rating => new Models.Rating + { + Source = rating.Source, + Value = rating.Value + }), + Metascore = model.Metascore, + ImdbRating = model.ImdbRating, + ImdbVotes = model.ImdbVotes, + ImdbID = model.ImdbID, + Type = model.Type, + DVD = model.DVD, + BoxOffice = model.BoxOffice, + Production = model.Production, + Website = model.Website, + Response = model.Response + }; + + return metadataItem; + } +} diff --git a/OpenMediaServer/Interfaces/APIs/IMetadataAPI.cs b/OpenMediaServer/Interfaces/APIs/IMetadataAPI.cs new file mode 100644 index 0000000..9398f6c --- /dev/null +++ b/OpenMediaServer/Interfaces/APIs/IMetadataAPI.cs @@ -0,0 +1,9 @@ +using System; +using OpenMediaServer.Models; + +namespace OpenMediaServer.Interfaces.APIs; + +public interface IMetadataAPI +{ + public Task GetMetadata(string name, string? apiKey, bool fullPlot = false); +} diff --git a/OpenMediaServer/Models/InventoryItem.cs b/OpenMediaServer/Models/InventoryItem.cs index ad1d7d4..ac9524a 100644 --- a/OpenMediaServer/Models/InventoryItem.cs +++ b/OpenMediaServer/Models/InventoryItem.cs @@ -5,7 +5,8 @@ namespace OpenMediaServer.Models; public class InventoryItem { public Guid Id { get; set; } = Guid.NewGuid(); - public string Title { get; set; } + public string? Title { get; set; } public string Path { get; set; } public virtual string Category { get; set; } + public MetadataModel? Metadata { get; set; } } diff --git a/OpenMediaServer/Models/MetadataModel.cs b/OpenMediaServer/Models/MetadataModel.cs new file mode 100644 index 0000000..814c133 --- /dev/null +++ b/OpenMediaServer/Models/MetadataModel.cs @@ -0,0 +1,38 @@ +using System; + +namespace OpenMediaServer.Models; + +public class MetadataModel +{ + public string? Title { get; set; } + public string? Year { get; set; } + public string? Rated { get; set; } + public string? Released { get; set; } + public string? Runtime { get; set; } + public string? Genre { get; set; } + public string? Director { get; set; } + public string? Writer { get; set; } + public string? Actors { get; set; } + public string? Plot { get; set; } + public string? Language { get; set; } + public string? Country { get; set; } + public string? Awards { get; set; } + public string? Poster { get; set; } + public List? Ratings { get; set; } + public string? Metascore { get; set; } + public string? ImdbRating { get; set; } + public string? ImdbVotes { get; set; } + public string? ImdbID { get; set; } + public string? Type { get; set; } + public string? DVD { get; set; } + public string? BoxOffice { get; set; } + public string? Production { get; set; } + public string? Website { get; set; } + public string? Response { get; set; } +} + +public class Rating +{ + public string? Source { get; set; } + public string? Value { get; set; } +} \ No newline at end of file diff --git a/OpenMediaServer/Program.cs b/OpenMediaServer/Program.cs index e46a5bd..39e61ec 100644 --- a/OpenMediaServer/Program.cs +++ b/OpenMediaServer/Program.cs @@ -1,5 +1,7 @@ using OpenMediaServer; +using OpenMediaServer.APIs; using OpenMediaServer.Endpoints; +using OpenMediaServer.Interfaces.APIs; using OpenMediaServer.Interfaces.Endpoints; using OpenMediaServer.Interfaces.Repositories; using OpenMediaServer.Interfaces.Services; @@ -15,6 +17,9 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +builder.Services.AddHttpClient(); var app = builder.Build(); diff --git a/OpenMediaServer/Services/InventoryService.cs b/OpenMediaServer/Services/InventoryService.cs index aea9646..37ec6e8 100644 --- a/OpenMediaServer/Services/InventoryService.cs +++ b/OpenMediaServer/Services/InventoryService.cs @@ -1,4 +1,5 @@ using System; +using OpenMediaServer.Interfaces.APIs; using OpenMediaServer.Interfaces.Repositories; using OpenMediaServer.Interfaces.Services; using OpenMediaServer.Models; @@ -9,14 +10,18 @@ public class InventoryService : IInventoryService { private readonly ILogger _logger; private readonly IStorageRepository _storageRepository; + private readonly IMetadataAPI _metadataAPI; + private readonly IConfiguration _configuration; - public InventoryService(ILogger logger, IStorageRepository storageRepository) + public InventoryService(ILogger logger, IStorageRepository storageRepository, IMetadataAPI metadataAPI, IConfiguration configuration) { _logger = logger; _storageRepository = storageRepository; + _metadataAPI = metadataAPI; + _configuration = configuration; } - public void CreateFromPaths(IEnumerable paths) + public async void CreateFromPaths(IEnumerable paths) { foreach (var path in paths) { @@ -36,7 +41,8 @@ public void CreateFromPaths(IEnumerable paths) { var movie = new Movie(); movie.Path = path; - movie.Title = parts[1]; + movie.Title = parts[1].Split(".").FirstOrDefault(); + movie.Metadata = await _metadataAPI.GetMetadata(movie.Title, _configuration.GetValue("OpenMediaServer:OMDbKey")); AddItem(movie); } diff --git a/OpenMediaServer/appsettings.json b/OpenMediaServer/appsettings.json index 10f68b8..243a82c 100644 --- a/OpenMediaServer/appsettings.json +++ b/OpenMediaServer/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "OpenMediaServer": { + "OMDbKey": "2b55c18b" + } }