-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metadata collection #3
- Loading branch information
Showing
12 changed files
with
234 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters