Fix OMDb "N/A"

This commit is contained in:
David 2020-12-23 14:45:05 +01:00
parent 276b219fbd
commit e2d338412f

View File

@ -296,10 +296,8 @@ namespace MediaBrowser.Providers.Plugins.Omdb
"i={0}&plot=short&tomatoes=true&r=json", "i={0}&plot=short&tomatoes=true&r=json",
imdbParam)); imdbParam));
using var response = await GetOmdbResponse(_httpClientFactory.CreateClient(NamedClient.Default), url, cancellationToken).ConfigureAwait(false);
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
var jsonOptions = JsonDefaults.GetOptions(); var jsonOptions = JsonDefaults.GetOptions();
var rootObject = await JsonSerializer.DeserializeAsync<RootObject>(stream, jsonOptions, cancellationToken).ConfigureAwait(false); var rootObject = await GetDeserializedOmdbResponse<RootObject>(_httpClientFactory.CreateClient(NamedClient.Default), url, jsonOptions, cancellationToken).ConfigureAwait(false);
Directory.CreateDirectory(Path.GetDirectoryName(path)); Directory.CreateDirectory(Path.GetDirectoryName(path));
await using FileStream jsonFileStream = File.Create(path); await using FileStream jsonFileStream = File.Create(path);
await JsonSerializer.SerializeAsync(jsonFileStream, rootObject, jsonOptions, cancellationToken).ConfigureAwait(false); await JsonSerializer.SerializeAsync(jsonFileStream, rootObject, jsonOptions, cancellationToken).ConfigureAwait(false);
@ -336,10 +334,8 @@ namespace MediaBrowser.Providers.Plugins.Omdb
imdbParam, imdbParam,
seasonId)); seasonId));
using var response = await GetOmdbResponse(_httpClientFactory.CreateClient(NamedClient.Default), url, cancellationToken).ConfigureAwait(false);
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
var jsonOptions = JsonDefaults.GetOptions(); var jsonOptions = JsonDefaults.GetOptions();
var rootObject = await JsonSerializer.DeserializeAsync<SeasonRootObject>(stream, jsonOptions, cancellationToken).ConfigureAwait(false); var rootObject = await GetDeserializedOmdbResponse<SeasonRootObject>(_httpClientFactory.CreateClient(NamedClient.Default), url, jsonOptions, cancellationToken).ConfigureAwait(false);
Directory.CreateDirectory(Path.GetDirectoryName(path)); Directory.CreateDirectory(Path.GetDirectoryName(path));
await using FileStream jsonFileStream = File.Create(path); await using FileStream jsonFileStream = File.Create(path);
await JsonSerializer.SerializeAsync(jsonFileStream, rootObject, jsonOptions, cancellationToken).ConfigureAwait(false); await JsonSerializer.SerializeAsync(jsonFileStream, rootObject, jsonOptions, cancellationToken).ConfigureAwait(false);
@ -347,6 +343,16 @@ namespace MediaBrowser.Providers.Plugins.Omdb
return path; return path;
} }
public static async Task<T> GetDeserializedOmdbResponse<T>(HttpClient httpClient, string url, JsonSerializerOptions jsonOptions, CancellationToken cancellationToken)
{
using var response = await GetOmdbResponse(httpClient, url, cancellationToken).ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
// OMDb is sending "N/A" for no empty number fields
content = content.Replace("\"N/A\"", "\"0\"", StringComparison.InvariantCulture);
return JsonSerializer.Deserialize<T>(content, jsonOptions);
}
public static Task<HttpResponseMessage> GetOmdbResponse(HttpClient httpClient, string url, CancellationToken cancellationToken) public static Task<HttpResponseMessage> GetOmdbResponse(HttpClient httpClient, string url, CancellationToken cancellationToken)
{ {
return httpClient.GetAsync(url, cancellationToken); return httpClient.GetAsync(url, cancellationToken);