Skip to content

Commit

Permalink
✨ Added Metadata retrieval
Browse files Browse the repository at this point in the history
Metadata collection #3
  • Loading branch information
LNA-DEV committed Sep 24, 2024
1 parent 2ddb72d commit 78201ad
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 15 deletions.
31 changes: 31 additions & 0 deletions OpenMediaServer.Test/APIs/OMDbAPIShould.cs
Original file line number Diff line number Diff line change
@@ -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<ILogger<OMDbAPI>>();
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

}
2 changes: 2 additions & 0 deletions OpenMediaServer.Test/OpenMediaServer.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
10 changes: 0 additions & 10 deletions OpenMediaServer.Test/UnitTest1.cs

This file was deleted.

50 changes: 50 additions & 0 deletions OpenMediaServer/APIs/OMDbAPI.cs
Original file line number Diff line number Diff line change
@@ -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<OMDbAPI> _logger;
private readonly HttpClient _httpClient;

public OMDbAPI(ILogger<OMDbAPI> logger, HttpClient httpClient)
{
_logger = logger;
_httpClient = httpClient;
}

public async Task<MetadataModel?> 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<OMDbModel>();

return model?.ToMetadataItem();
}
else
{
_logger.LogWarning("OMDb could not retrieve metadata with error: {ErrorCode} and message: {Message}", message.StatusCode, message.ReasonPhrase);
}

return null;
}
}
38 changes: 38 additions & 0 deletions OpenMediaServer/DTOs/OMDbModel.cs
Original file line number Diff line number Diff line change
@@ -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<Rating> 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; }
}
46 changes: 46 additions & 0 deletions OpenMediaServer/Extensions/ModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions OpenMediaServer/Interfaces/APIs/IMetadataAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using OpenMediaServer.Models;

namespace OpenMediaServer.Interfaces.APIs;

public interface IMetadataAPI
{
public Task<MetadataModel?> GetMetadata(string name, string? apiKey, bool fullPlot = false);
}
3 changes: 2 additions & 1 deletion OpenMediaServer/Models/InventoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
38 changes: 38 additions & 0 deletions OpenMediaServer/Models/MetadataModel.cs
Original file line number Diff line number Diff line change
@@ -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<Rating>? 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; }
}
5 changes: 5 additions & 0 deletions OpenMediaServer/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +17,9 @@
builder.Services.AddSingleton<IStorageRepository, FileSystemRepository>();
builder.Services.AddSingleton<IInventoryService, InventoryService>();
builder.Services.AddSingleton<IMovieEndpoints, MovieEndpoints>();
builder.Services.AddSingleton<IMetadataAPI, OMDbAPI>();

builder.Services.AddHttpClient<IMetadataAPI, OMDbAPI>();

var app = builder.Build();

Expand Down
12 changes: 9 additions & 3 deletions OpenMediaServer/Services/InventoryService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using OpenMediaServer.Interfaces.APIs;
using OpenMediaServer.Interfaces.Repositories;
using OpenMediaServer.Interfaces.Services;
using OpenMediaServer.Models;
Expand All @@ -9,14 +10,18 @@ public class InventoryService : IInventoryService
{
private readonly ILogger<InventoryService> _logger;
private readonly IStorageRepository _storageRepository;
private readonly IMetadataAPI _metadataAPI;
private readonly IConfiguration _configuration;

public InventoryService(ILogger<InventoryService> logger, IStorageRepository storageRepository)
public InventoryService(ILogger<InventoryService> logger, IStorageRepository storageRepository, IMetadataAPI metadataAPI, IConfiguration configuration)
{
_logger = logger;
_storageRepository = storageRepository;
_metadataAPI = metadataAPI;
_configuration = configuration;
}

public void CreateFromPaths(IEnumerable<string> paths)
public async void CreateFromPaths(IEnumerable<string> paths)
{
foreach (var path in paths)
{
Expand All @@ -36,7 +41,8 @@ public void CreateFromPaths(IEnumerable<string> 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<string>("OpenMediaServer:OMDbKey"));

AddItem(movie);
}
Expand Down
5 changes: 4 additions & 1 deletion OpenMediaServer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"OpenMediaServer": {
"OMDbKey": "2b55c18b"
}
}

0 comments on commit 78201ad

Please sign in to comment.