Skip to content

Commit

Permalink
Support searching by DoubanID
Browse files Browse the repository at this point in the history
  • Loading branch information
Libitum committed Feb 20, 2020
1 parent c4da482 commit 240751b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
70 changes: 35 additions & 35 deletions Jellyfin.Plugin.Douban.Tests/MovieProviderTest.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
using System;
using System.Net.Http;
using System.Threading;

using MediaBrowser.Controller.Providers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;

using Jellyfin.Plugin.Douban.Tests.Mock;

namespace Jellyfin.Plugin.Douban.Tests
{
public class MovieProviderTest
{
private readonly MovieProvider _doubanProvider;
private readonly IServiceProvider _serviceProvider;

public MovieProviderTest()
{
_serviceProvider = new ServiceCollection().AddLogging(builder => builder.AddConsole())
.BuildServiceProvider();
var loggerFactory = _serviceProvider.GetService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger("test");

var httpClient = new MockHttpClient();
var jsonSerializer = new MockJsonSerializer();
_doubanProvider = new MovieProvider(httpClient, jsonSerializer, logger);
}

[Fact]
using System;
using System.Net.Http;
using System.Threading;

using MediaBrowser.Controller.Providers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;

using Jellyfin.Plugin.Douban.Tests.Mock;

namespace Jellyfin.Plugin.Douban.Tests
{
public class MovieProviderTest
{
private readonly MovieProvider _doubanProvider;
private readonly IServiceProvider _serviceProvider;

public MovieProviderTest()
{
_serviceProvider = new ServiceCollection().AddLogging(builder => builder.AddConsole())
.BuildServiceProvider();
var loggerFactory = _serviceProvider.GetService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger("test");

var httpClient = new MockHttpClient();
var jsonSerializer = new MockJsonSerializer();
_doubanProvider = new MovieProvider(httpClient, jsonSerializer, logger);
}

[Fact]
public void TestGetMetadata()
{
MovieInfo info = new MovieInfo()
Expand Down Expand Up @@ -64,10 +64,10 @@ public void TestGetMetadata()
info = new MovieInfo()
{
MetadataLanguage = "zh",
Name = "亮剑"
Name = "三国演义"
};
meta = _doubanProvider.GetMetadata(info, CancellationToken.None).Result;
Assert.False(meta.HasMetadata);
}
}
}
}
}
}
8 changes: 4 additions & 4 deletions Jellyfin.Plugin.Douban/BaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Jellyfin.Plugin.Douban
{
public abstract class BaseProvider
{
protected const string ProviderID = "DoubanID";
internal const string ProviderID = "DoubanID";

protected IHttpClient _httpClient;
protected IJsonSerializer _jsonSerializer;
Expand Down Expand Up @@ -111,7 +111,7 @@ protected async Task<MetadataResult<T>> GetMetaFromDouban<T>(string sid,
var data = await GetDoubanSubject(sid, cancellationToken);
if (!String.IsNullOrEmpty(type) && data.Subtype != type)
{
_logger.LogInformation("Douban: Sid {1}'s type is {2}, " +
_logger.LogInformation("Douban: Sid {1}'s type is {2}, " +
"but require {3}", sid, data.Subtype, type);
return result;
}
Expand All @@ -135,7 +135,7 @@ protected async Task<MetadataResult<T>> GetMetaFromDouban<T>(string sid,
internal async Task<Response.Subject> GetDoubanSubject(string sid,
CancellationToken cancellationToken)
{
_logger.LogInformation("Douban: Trying to get douban subject by " +
_logger.LogInformation("Douban: Trying to get douban subject by " +
"sid: {0}", sid);
cancellationToken.ThrowIfCancellationRequested();

Expand All @@ -145,7 +145,7 @@ protected async Task<MetadataResult<T>> GetMetaFromDouban<T>(string sid,
}

String apikey = _config.ApiKey;
var url = String.Format("http://api.douban.com/v2/movie/subject" +
var url = String.Format("http://api.douban.com/v2/movie/subject" +
"/{0}?apikey={1}", sid, apikey);
var options = new HttpRequestOptions
{
Expand Down
19 changes: 17 additions & 2 deletions Jellyfin.Plugin.Douban/MovieProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,23 @@ public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(
return results;
}

var sidList = await SearchSidByName(info.Name, cancellationToken).
ConfigureAwait(false);
IEnumerable<string> sidList;

string doubanId = info.GetProviderId(ProviderID);
_logger.LogInformation("douban id is {0}", doubanId);
if (!string.IsNullOrEmpty(doubanId))
{
sidList = new List<string>
{
doubanId
};
}
else
{
sidList = await SearchSidByName(info.Name, cancellationToken).
ConfigureAwait(false);
}

foreach (String sid in sidList)
{
var subject = await GetDoubanSubject(sid, cancellationToken).
Expand Down
36 changes: 26 additions & 10 deletions Jellyfin.Plugin.Douban/TVProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info,
_logger.LogInformation($"Douban:GetMetadata name: {info.Name}");


// Only handle it when launguage is "zh"
// Only handle it when language is "zh"
if (info.MetadataLanguage != "zh")
{
_logger.LogInformation("DoubanProvider: the required " +
"launguage is not zh, so just bypass DoubanProvider");
_logger.LogInformation("DoubanProvider: the required " +
"language is not zh, so just bypass DoubanProvider");
return new MetadataResult<Series>();
}

Expand Down Expand Up @@ -77,6 +77,7 @@ public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info,
if (result.HasMetadata)
{
info.SetProviderId(ProviderID, sid);
result.QueriedById = true;
}

return result;
Expand All @@ -89,16 +90,31 @@ public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(

var results = new List<RemoteSearchResult>();

// Only handle it when launguage is "zh"
// Only handle it when language is "zh"
if (info.MetadataLanguage != "zh")
{
_logger.LogInformation("DoubanProvider: the required " +
"launguage is not zh, so just bypass DoubanProvider");
_logger.LogInformation("DoubanProvider: the required " +
"language is not zh, so just bypass DoubanProvider");
return results;
}

var sidList = await SearchSidByName(info.Name, cancellationToken).
ConfigureAwait(false);
IEnumerable<string> sidList;

string doubanId = info.GetProviderId(ProviderID);
_logger.LogInformation("douban id is {0}", doubanId);
if (!string.IsNullOrEmpty(doubanId))
{
sidList = new List<string>
{
doubanId
};
}
else
{
sidList = await SearchSidByName(info.Name, cancellationToken).
ConfigureAwait(false);
}

foreach (String sid in sidList)
{
var subject = await GetDoubanSubject(sid, cancellationToken).
Expand Down Expand Up @@ -190,12 +206,12 @@ public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info,

if (!info.IndexNumber.HasValue)
{
_logger.LogInformation("No episode num found, please check " +
_logger.LogInformation("No episode num found, please check " +
"the format of file name");
return result;
}
// Start to get information from douban
var url = String.Format("https://movie.douban.com/subject/{0}" +
var url = String.Format("https://movie.douban.com/subject/{0}" +
"/episode/{1}/", sid, info.IndexNumber);
var options = new HttpRequestOptions
{
Expand Down

0 comments on commit 240751b

Please sign in to comment.