Merge pull request #1731 from softworkz/MetadataMerging

Prioritize metadata merging by preferred language
This commit is contained in:
Luke 2016-09-22 17:58:18 -04:00 committed by GitHub
commit 2c78e43aad
8 changed files with 99 additions and 18 deletions

View File

@ -13,13 +13,15 @@ namespace MediaBrowser.Controller.Providers
public MetadataResult() public MetadataResult()
{ {
Images = new List<LocalImageInfo>(); Images = new List<LocalImageInfo>();
ResultLanguage = "en";
} }
public List<PersonInfo> People { get; set; } public List<PersonInfo> People { get; set; }
public bool HasMetadata { get; set; } public bool HasMetadata { get; set; }
public T Item { get; set; } public T Item { get; set; }
public string ResultLanguage { get; set; }
public bool QueriedById { get; set; }
public void AddPerson(PersonInfo p) public void AddPerson(PersonInfo p)
{ {
if (People == null) if (People == null)

View File

@ -651,6 +651,8 @@ namespace MediaBrowser.Providers.Manager
{ {
var refreshResult = new RefreshResult(); var refreshResult = new RefreshResult();
var results = new List<MetadataResult<TItemType>>();
foreach (var provider in providers) foreach (var provider in providers)
{ {
var providerName = provider.GetType().Name; var providerName = provider.GetType().Name;
@ -667,7 +669,7 @@ namespace MediaBrowser.Providers.Manager
if (result.HasMetadata) if (result.HasMetadata)
{ {
MergeData(result, temp, new List<MetadataFields>(), false, false); results.Add(result);
refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataDownload; refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataDownload;
} }
@ -688,6 +690,38 @@ namespace MediaBrowser.Providers.Manager
} }
} }
var orderedResults = new List<MetadataResult<TItemType>>();
if (string.IsNullOrEmpty(id.MetadataLanguage))
{
orderedResults.AddRange(results);
}
else
{
// prioritize results with matching ResultLanguage
foreach (var result in results)
{
if (!string.IsNullOrEmpty(result.ResultLanguage) && result.ResultLanguage == id.MetadataLanguage && result.QueriedById)
{
orderedResults.Add(result);
}
}
// add all other results
foreach (var result in results)
{
if (!orderedResults.Contains(result))
{
orderedResults.Add(result);
}
}
}
foreach (var result in results)
{
MergeData(result, temp, new List<MetadataFields>(), false, false);
}
return refreshResult; return refreshResult;
} }

View File

@ -212,13 +212,15 @@ namespace MediaBrowser.Providers.Omdb
{ {
var result = new MetadataResult<Series> var result = new MetadataResult<Series>
{ {
Item = new Series() Item = new Series(),
QueriedById = true
}; };
var imdbId = info.GetProviderId(MetadataProviders.Imdb); var imdbId = info.GetProviderId(MetadataProviders.Imdb);
if (string.IsNullOrWhiteSpace(imdbId)) if (string.IsNullOrWhiteSpace(imdbId))
{ {
imdbId = await GetSeriesImdbId(info, cancellationToken).ConfigureAwait(false); imdbId = await GetSeriesImdbId(info, cancellationToken).ConfigureAwait(false);
result.QueriedById = false;
} }
if (!string.IsNullOrEmpty(imdbId)) if (!string.IsNullOrEmpty(imdbId))
@ -251,13 +253,15 @@ namespace MediaBrowser.Providers.Omdb
{ {
var result = new MetadataResult<T> var result = new MetadataResult<T>
{ {
Item = new T() Item = new T(),
QueriedById = true
}; };
var imdbId = info.GetProviderId(MetadataProviders.Imdb); var imdbId = info.GetProviderId(MetadataProviders.Imdb);
if (string.IsNullOrWhiteSpace(imdbId)) if (string.IsNullOrWhiteSpace(imdbId))
{ {
imdbId = await GetMovieImdbId(info, cancellationToken).ConfigureAwait(false); imdbId = await GetMovieImdbId(info, cancellationToken).ConfigureAwait(false);
result.QueriedById = false;
} }
if (!string.IsNullOrEmpty(imdbId)) if (!string.IsNullOrEmpty(imdbId))

View File

@ -43,7 +43,8 @@ namespace MediaBrowser.Providers.TV
{ {
var result = new MetadataResult<Episode>() var result = new MetadataResult<Episode>()
{ {
Item = new Episode() Item = new Episode(),
QueriedById = true
}; };
// Allowing this will dramatically increase scan times // Allowing this will dramatically increase scan times

View File

@ -91,6 +91,13 @@ namespace MediaBrowser.Providers.TV
var response = await GetEpisodeInfo(seriesTmdbId, seasonNumber.Value, episodeNumber.Value, info.MetadataLanguage, cancellationToken).ConfigureAwait(false); var response = await GetEpisodeInfo(seriesTmdbId, seasonNumber.Value, episodeNumber.Value, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
result.HasMetadata = true; result.HasMetadata = true;
result.QueriedById = true;
if (!string.IsNullOrEmpty(response.overview))
{
// if overview is non-empty, we can assume that localized data was returned
result.ResultLanguage = info.MetadataLanguage;
}
var item = new Episode(); var item = new Episode();
result.Item = item; result.Item = item;

View File

@ -119,6 +119,7 @@ namespace MediaBrowser.Providers.TV
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken) public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
{ {
var result = new MetadataResult<Series>(); var result = new MetadataResult<Series>();
result.QueriedById = true;
var tmdbId = info.GetProviderId(MetadataProviders.Tmdb); var tmdbId = info.GetProviderId(MetadataProviders.Tmdb);
@ -154,6 +155,7 @@ namespace MediaBrowser.Providers.TV
if (string.IsNullOrEmpty(tmdbId)) if (string.IsNullOrEmpty(tmdbId))
{ {
result.QueriedById = false;
var searchResults = await new MovieDbSearch(_logger, _jsonSerializer, _libraryManager).GetSearchResults(info, cancellationToken).ConfigureAwait(false); var searchResults = await new MovieDbSearch(_logger, _jsonSerializer, _libraryManager).GetSearchResults(info, cancellationToken).ConfigureAwait(false);
var searchResult = searchResults.FirstOrDefault(); var searchResult = searchResults.FirstOrDefault();
@ -168,7 +170,7 @@ namespace MediaBrowser.Providers.TV
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
result.Item = await FetchSeriesData(tmdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationToken).ConfigureAwait(false); result = await FetchMovieData(tmdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationToken).ConfigureAwait(false);
result.HasMetadata = result.Item != null; result.HasMetadata = result.Item != null;
} }
@ -176,7 +178,7 @@ namespace MediaBrowser.Providers.TV
return result; return result;
} }
private async Task<Series> FetchSeriesData(string tmdbId, string language, string preferredCountryCode, CancellationToken cancellationToken) private async Task<MetadataResult<Series>> FetchMovieData(string tmdbId, string language, string preferredCountryCode, CancellationToken cancellationToken)
{ {
string dataFilePath = null; string dataFilePath = null;
RootObject seriesInfo = null; RootObject seriesInfo = null;
@ -199,11 +201,13 @@ namespace MediaBrowser.Providers.TV
await EnsureSeriesInfo(tmdbId, language, cancellationToken).ConfigureAwait(false); await EnsureSeriesInfo(tmdbId, language, cancellationToken).ConfigureAwait(false);
var item = new Series(); var result = new MetadataResult<Series>();
result.Item = new Series();
result.ResultLanguage = seriesInfo.ResultLanguage;
ProcessMainInfo(item, seriesInfo, preferredCountryCode); ProcessMainInfo(result.Item, seriesInfo, preferredCountryCode);
return item; return result;
} }
private void ProcessMainInfo(Series series, RootObject seriesInfo, string preferredCountryCode) private void ProcessMainInfo(Series series, RootObject seriesInfo, string preferredCountryCode)
@ -354,6 +358,11 @@ namespace MediaBrowser.Providers.TV
}).ConfigureAwait(false)) }).ConfigureAwait(false))
{ {
mainResult = _jsonSerializer.DeserializeFromStream<RootObject>(json); mainResult = _jsonSerializer.DeserializeFromStream<RootObject>(json);
if (!string.IsNullOrEmpty(language))
{
mainResult.ResultLanguage = language;
}
} }
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@ -385,6 +394,7 @@ namespace MediaBrowser.Providers.TV
var englishResult = _jsonSerializer.DeserializeFromStream<RootObject>(json); var englishResult = _jsonSerializer.DeserializeFromStream<RootObject>(json);
mainResult.overview = englishResult.overview; mainResult.overview = englishResult.overview;
mainResult.ResultLanguage = "en";
} }
} }
@ -627,6 +637,7 @@ namespace MediaBrowser.Providers.TV
public ExternalIds external_ids { get; set; } public ExternalIds external_ids { get; set; }
public Videos videos { get; set; } public Videos videos { get; set; }
public ContentRatings content_ratings { get; set; } public ContentRatings content_ratings { get; set; }
public string ResultLanguage { get; set; }
} }
public int Order public int Order

View File

@ -108,6 +108,7 @@ namespace MediaBrowser.Providers.TV
public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo searchInfo, CancellationToken cancellationToken) public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo searchInfo, CancellationToken cancellationToken)
{ {
var result = new MetadataResult<Episode>(); var result = new MetadataResult<Episode>();
result.QueriedById = true;
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds) && if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds) &&
(searchInfo.IndexNumber.HasValue || searchInfo.PremiereDate.HasValue)) (searchInfo.IndexNumber.HasValue || searchInfo.PremiereDate.HasValue))
@ -713,6 +714,17 @@ namespace MediaBrowser.Providers.TV
} }
} }
break;
}
case "Language":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
result.ResultLanguage = val;
}
break; break;
} }

View File

@ -93,9 +93,11 @@ namespace MediaBrowser.Providers.TV
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo itemId, CancellationToken cancellationToken) public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo itemId, CancellationToken cancellationToken)
{ {
var result = new MetadataResult<Series>(); var result = new MetadataResult<Series>();
result.QueriedById = true;
if (!IsValidSeries(itemId.ProviderIds)) if (!IsValidSeries(itemId.ProviderIds))
{ {
result.QueriedById = false;
await Identify(itemId).ConfigureAwait(false); await Identify(itemId).ConfigureAwait(false);
} }
@ -159,7 +161,7 @@ namespace MediaBrowser.Providers.TV
var seriesXmlPath = GetSeriesXmlPath(seriesProviderIds, metadataLanguage); var seriesXmlPath = GetSeriesXmlPath(seriesProviderIds, metadataLanguage);
var actorsXmlPath = Path.Combine(seriesDataPath, "actors.xml"); var actorsXmlPath = Path.Combine(seriesDataPath, "actors.xml");
FetchSeriesInfo(series, seriesXmlPath, cancellationToken); FetchSeriesInfo(result, seriesXmlPath, cancellationToken);
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@ -607,7 +609,7 @@ namespace MediaBrowser.Providers.TV
return name.Trim(); return name.Trim();
} }
private void FetchSeriesInfo(Series item, string seriesXmlPath, CancellationToken cancellationToken) private void FetchSeriesInfo(MetadataResult<Series> result, string seriesXmlPath, CancellationToken cancellationToken)
{ {
var settings = new XmlReaderSettings var settings = new XmlReaderSettings
{ {
@ -639,7 +641,7 @@ namespace MediaBrowser.Providers.TV
{ {
using (var subtree = reader.ReadSubtree()) using (var subtree = reader.ReadSubtree())
{ {
FetchDataFromSeriesNode(item, subtree, cancellationToken); FetchDataFromSeriesNode(result, subtree, cancellationToken);
} }
break; break;
} }
@ -667,9 +669,9 @@ namespace MediaBrowser.Providers.TV
} }
} }
if (item.Status.HasValue && item.Status.Value == SeriesStatus.Ended && episiodeAirDates.Count > 0) if (result.Item.Status.HasValue && result.Item.Status.Value == SeriesStatus.Ended && episiodeAirDates.Count > 0)
{ {
item.EndDate = episiodeAirDates.Max(); result.Item.EndDate = episiodeAirDates.Max();
} }
} }
@ -861,8 +863,10 @@ namespace MediaBrowser.Providers.TV
} }
} }
private void FetchDataFromSeriesNode(Series item, XmlReader reader, CancellationToken cancellationToken) private void FetchDataFromSeriesNode(MetadataResult<Series> result, XmlReader reader, CancellationToken cancellationToken)
{ {
Series item = result.Item;
reader.MoveToContent(); reader.MoveToContent();
// Loop through each element // Loop through each element
@ -886,6 +890,12 @@ namespace MediaBrowser.Providers.TV
break; break;
} }
case "Language":
{
result.ResultLanguage = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
break;
}
case "Airs_DayOfWeek": case "Airs_DayOfWeek":
{ {
var val = reader.ReadElementContentAsString(); var val = reader.ReadElementContentAsString();