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()
{
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.
// Caller provides the filename with extension stripped and NOT the parsed filename
var searchName = info.Name;
var parsedName = this._libraryManager.ParseName(info.Name);
this.Log($"GuessByDouban of [name]: {info.Name} year: {info.Year} search name: {parsedName.Name}");
var result = await this._doubanApi.SearchAsync(parsedName.Name, cancellationToken).ConfigureAwait(false);
if (parsedName != null)
{
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();
foreach (var item in result)
{
@ -96,12 +102,12 @@ namespace Jellyfin.Plugin.MetaShark.Providers
continue;
}
if (jw.Similarity(parsedName.Name, item.Name) < 0.8)
if (jw.Similarity(searchName, item.Name) < 0.8)
{
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}");
return item.Sid;
@ -242,10 +248,18 @@ namespace Jellyfin.Plugin.MetaShark.Providers
if (omdbItem != null)
{
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)
{
this.Log($"GetSeriesMetadata found tmdb [id]: {findResult.TvResults[0].Id} by imdb id: {imdb}");
return $"{findResult.TvResults[0].Id}";
var tmdbId = 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))
{
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> {
new RemoteImageInfo
@ -67,16 +71,16 @@ namespace Jellyfin.Plugin.MetaShark.Providers
Type = ImageType.Primary
}
};
res.AddRange(dropback);
res.AddRange(backdropImgs);
return res;
}
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb).ToInt();
if (tmdbId > 0)
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
if (!string.IsNullOrEmpty(tmdbId))
{
var language = item.GetPreferredMetadataLanguage();
var movie = await _tmdbApi
.GetMovieAsync(tmdbId, language, language, cancellationToken)
.GetMovieAsync(tmdbId.ToInt(), language, language, cancellationToken)
.ConfigureAwait(false);
if (movie?.Images == null)

View File

@ -96,7 +96,7 @@ namespace Jellyfin.Plugin.MetaShark.Providers
var metaSource = info.GetProviderId(Plugin.ProviderId); // 刷新元数据时会有值
if (string.IsNullOrEmpty(sid) && string.IsNullOrEmpty(tmdbId))
{
// 刷新元数据匹配搜索
// 自动扫描匹配搜索
sid = await this.GuessByDoubanAsync(info, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(sid))
{
@ -132,11 +132,13 @@ namespace Jellyfin.Plugin.MetaShark.Providers
movie.SetProviderId(MetadataProvider.Imdb, subject.Imdb);
// 通过imdb获取TMDB id
var movieResult = await this._tmdbApi.FindByExternalIdAsync(subject.Imdb, FindExternalSource.Imdb, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
if (movieResult?.MovieResults != null && movieResult.MovieResults.Count > 0)
if (string.IsNullOrEmpty(tmdbId))
{
this.Log($"GetMovieMetadata of found tmdb [id]: \"{movieResult.MovieResults[0].Id}\"");
movie.SetProviderId(MetadataProvider.Tmdb, $"{movieResult.MovieResults[0].Id}");
tmdbId = await this.GetTmdbIdByImdbAsync(subject.Imdb, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
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))
{
var primary = await this._doubanApi.GetMovieAsync(sid, cancellationToken);
if (primary == null)
{
return Enumerable.Empty<RemoteImageInfo>();
}
var dropback = await GetBackdrop(sid, cancellationToken);
var res = new List<RemoteImageInfo> {
@ -71,12 +75,12 @@ namespace Jellyfin.Plugin.MetaShark.Providers
return res;
}
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb).ToInt();
if (tmdbId > 0)
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
if (!string.IsNullOrEmpty(tmdbId))
{
var language = item.GetPreferredMetadataLanguage();
var movie = await _tmdbApi
.GetSeriesAsync(tmdbId, language, language, cancellationToken)
.GetSeriesAsync(tmdbId.ToInt(), language, language, cancellationToken)
.ConfigureAwait(false);
if (movie?.Images == null)