Fix null exception

This commit is contained in:
cxfksword 2022-11-12 11:46:53 +08:00
parent a49dcc71dc
commit e9061d8774
6 changed files with 96 additions and 20 deletions

View File

@ -0,0 +1,52 @@
using Jellyfin.Plugin.MetaShark.Api;
using Jellyfin.Plugin.MetaShark.Core;
using Jellyfin.Plugin.MetaShark.Providers;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Jellyfin.Plugin.MetaShark.Test
{
[TestClass]
public class MovieProviderTest
{
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
builder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "hh:mm:ss ";
}));
[TestMethod]
public void TestGetMetadata()
{
var info = new MovieInfo() { Name = "南极料理人" };
var doubanApi = new DoubanApi(loggerFactory);
var tmdbApi = new TmdbApi(loggerFactory);
var omdbApi = new OmdbApi(loggerFactory);
var httpClientFactory = new DefaultHttpClientFactory();
var libraryManagerStub = new Mock<ILibraryManager>();
Task.Run(async () =>
{
var provider = new MovieProvider(httpClientFactory, loggerFactory, libraryManagerStub.Object, doubanApi, tmdbApi, omdbApi);
var result = await provider.GetMetadata(info, CancellationToken.None);
Assert.IsNotNull(result);
var str = result.ToJson();
Console.WriteLine(result.ToJson());
}).GetAwaiter().GetResult();
}
}
}

View File

@ -676,7 +676,7 @@ namespace Jellyfin.Plugin.MetaShark.Api
private bool IsEnable() private bool IsEnable()
{ {
return Plugin.Instance?.Configuration.EnableTmdb ?? false; return Plugin.Instance?.Configuration.EnableTmdb ?? true;
} }
} }

View File

@ -80,9 +80,15 @@ namespace Jellyfin.Plugin.MetaShark.Providers
{ {
// ParseName is required here. // ParseName is required here.
// Caller provides the filename with extension stripped and NOT the parsed filename // Caller provides the filename with extension stripped and NOT the parsed filename
var searchName = info.Name;
var parsedName = this._libraryManager.ParseName(info.Name); var parsedName = this._libraryManager.ParseName(info.Name);
this.Log($"GuessByDouban of [name]: {info.Name} year: {info.Year} search name: {parsedName.Name}"); if (parsedName != null)
var result = await this._doubanApi.SearchAsync(parsedName.Name, cancellationToken).ConfigureAwait(false); {
searchName = parsedName.Name;
}
this.Log($"GuessByDouban of [name]: {info.Name} year: {info.Year} search name: {searchName}");
var result = await this._doubanApi.SearchAsync(searchName, cancellationToken).ConfigureAwait(false);
var jw = new JaroWinkler(); var jw = new JaroWinkler();
foreach (var item in result) foreach (var item in result)
{ {
@ -96,12 +102,12 @@ namespace Jellyfin.Plugin.MetaShark.Providers
continue; continue;
} }
if (jw.Similarity(parsedName.Name, item.Name) < 0.8) if (jw.Similarity(searchName, item.Name) < 0.8)
{ {
continue; continue;
} }
if (parsedName.Year == null || parsedName.Year == 0) if (parsedName == null || parsedName.Year == null || parsedName.Year == 0)
{ {
this.Log($"GuessByDouban of [name] found Sid: {item.Sid}"); this.Log($"GuessByDouban of [name] found Sid: {item.Sid}");
return item.Sid; return item.Sid;
@ -242,10 +248,18 @@ namespace Jellyfin.Plugin.MetaShark.Providers
if (omdbItem != null) if (omdbItem != null)
{ {
var findResult = await this._tmdbApi.FindByExternalIdAsync(omdbItem.ImdbID, TMDbLib.Objects.Find.FindExternalSource.Imdb, language, cancellationToken).ConfigureAwait(false); var findResult = await this._tmdbApi.FindByExternalIdAsync(omdbItem.ImdbID, TMDbLib.Objects.Find.FindExternalSource.Imdb, language, cancellationToken).ConfigureAwait(false);
if (findResult?.MovieResults != null && findResult.MovieResults.Count > 0)
{
var tmdbId = findResult.MovieResults[0].Id;
this.Log($"Found tmdb [id]: {tmdbId} by imdb id: {imdb}");
return $"{tmdbId}";
}
if (findResult?.TvResults != null && findResult.TvResults.Count > 0) if (findResult?.TvResults != null && findResult.TvResults.Count > 0)
{ {
this.Log($"GetSeriesMetadata found tmdb [id]: {findResult.TvResults[0].Id} by imdb id: {imdb}"); var tmdbId = findResult.TvResults[0].Id;
return $"{findResult.TvResults[0].Id}"; this.Log($"Found tmdb [id]: {tmdbId} by imdb id: {imdb}");
return $"{tmdbId}";
} }
} }

View File

@ -57,7 +57,11 @@ namespace Jellyfin.Plugin.MetaShark.Providers
if (metaSource != MetaSource.Tmdb && !string.IsNullOrEmpty(sid)) if (metaSource != MetaSource.Tmdb && !string.IsNullOrEmpty(sid))
{ {
var primary = await this._doubanApi.GetMovieAsync(sid, cancellationToken); var primary = await this._doubanApi.GetMovieAsync(sid, cancellationToken);
var dropback = await GetBackdrop(sid, cancellationToken); if (primary == null)
{
return Enumerable.Empty<RemoteImageInfo>();
}
var backdropImgs = await GetBackdrop(sid, cancellationToken);
var res = new List<RemoteImageInfo> { var res = new List<RemoteImageInfo> {
new RemoteImageInfo new RemoteImageInfo
@ -67,16 +71,16 @@ namespace Jellyfin.Plugin.MetaShark.Providers
Type = ImageType.Primary Type = ImageType.Primary
} }
}; };
res.AddRange(dropback); res.AddRange(backdropImgs);
return res; return res;
} }
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb).ToInt(); var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
if (tmdbId > 0) if (!string.IsNullOrEmpty(tmdbId))
{ {
var language = item.GetPreferredMetadataLanguage(); var language = item.GetPreferredMetadataLanguage();
var movie = await _tmdbApi var movie = await _tmdbApi
.GetMovieAsync(tmdbId, language, language, cancellationToken) .GetMovieAsync(tmdbId.ToInt(), language, language, cancellationToken)
.ConfigureAwait(false); .ConfigureAwait(false);
if (movie?.Images == null) if (movie?.Images == null)

View File

@ -96,7 +96,7 @@ namespace Jellyfin.Plugin.MetaShark.Providers
var metaSource = info.GetProviderId(Plugin.ProviderId); // 刷新元数据时会有值 var metaSource = info.GetProviderId(Plugin.ProviderId); // 刷新元数据时会有值
if (string.IsNullOrEmpty(sid) && string.IsNullOrEmpty(tmdbId)) if (string.IsNullOrEmpty(sid) && string.IsNullOrEmpty(tmdbId))
{ {
// 刷新元数据匹配搜索 // 自动扫描匹配搜索
sid = await this.GuessByDoubanAsync(info, cancellationToken).ConfigureAwait(false); sid = await this.GuessByDoubanAsync(info, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(sid)) if (string.IsNullOrEmpty(sid))
{ {
@ -132,11 +132,13 @@ namespace Jellyfin.Plugin.MetaShark.Providers
movie.SetProviderId(MetadataProvider.Imdb, subject.Imdb); movie.SetProviderId(MetadataProvider.Imdb, subject.Imdb);
// 通过imdb获取TMDB id // 通过imdb获取TMDB id
var movieResult = await this._tmdbApi.FindByExternalIdAsync(subject.Imdb, FindExternalSource.Imdb, info.MetadataLanguage, cancellationToken).ConfigureAwait(false); if (string.IsNullOrEmpty(tmdbId))
if (movieResult?.MovieResults != null && movieResult.MovieResults.Count > 0)
{ {
this.Log($"GetMovieMetadata of found tmdb [id]: \"{movieResult.MovieResults[0].Id}\""); tmdbId = await this.GetTmdbIdByImdbAsync(subject.Imdb, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
movie.SetProviderId(MetadataProvider.Tmdb, $"{movieResult.MovieResults[0].Id}"); if (!string.IsNullOrEmpty(tmdbId))
{
movie.SetProviderId(MetadataProvider.Tmdb, tmdbId);
}
} }
} }

View File

@ -57,6 +57,10 @@ namespace Jellyfin.Plugin.MetaShark.Providers
if (metaSource != MetaSource.Tmdb && !string.IsNullOrEmpty(sid)) if (metaSource != MetaSource.Tmdb && !string.IsNullOrEmpty(sid))
{ {
var primary = await this._doubanApi.GetMovieAsync(sid, cancellationToken); var primary = await this._doubanApi.GetMovieAsync(sid, cancellationToken);
if (primary == null)
{
return Enumerable.Empty<RemoteImageInfo>();
}
var dropback = await GetBackdrop(sid, cancellationToken); var dropback = await GetBackdrop(sid, cancellationToken);
var res = new List<RemoteImageInfo> { var res = new List<RemoteImageInfo> {
@ -71,12 +75,12 @@ namespace Jellyfin.Plugin.MetaShark.Providers
return res; return res;
} }
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb).ToInt(); var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
if (tmdbId > 0) if (!string.IsNullOrEmpty(tmdbId))
{ {
var language = item.GetPreferredMetadataLanguage(); var language = item.GetPreferredMetadataLanguage();
var movie = await _tmdbApi var movie = await _tmdbApi
.GetSeriesAsync(tmdbId, language, language, cancellationToken) .GetSeriesAsync(tmdbId.ToInt(), language, language, cancellationToken)
.ConfigureAwait(false); .ConfigureAwait(false);
if (movie?.Images == null) if (movie?.Images == null)