Optimize douban cookie reload

This commit is contained in:
cxfksword 2023-02-17 12:39:51 +08:00
parent aa4e8dae59
commit d74541e109
2 changed files with 42 additions and 102 deletions

View File

@ -42,7 +42,6 @@ namespace Jellyfin.Plugin.MetaShark.Api
private readonly ILogger<DoubanApi> _logger; private readonly ILogger<DoubanApi> _logger;
private HttpClient httpClient; private HttpClient httpClient;
private CookieContainer _cookieContainer; private CookieContainer _cookieContainer;
private string oldLoadedCookies = string.Empty;
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
private readonly IMemoryCache _memoryCache; private readonly IMemoryCache _memoryCache;
private static readonly object _lock = new object(); private static readonly object _lock = new object();
@ -106,56 +105,57 @@ namespace Jellyfin.Plugin.MetaShark.Api
httpClient.DefaultRequestHeaders.Add("User-Agent", HTTP_USER_AGENT); httpClient.DefaultRequestHeaders.Add("User-Agent", HTTP_USER_AGENT);
httpClient.DefaultRequestHeaders.Add("Origin", "https://movie.douban.com"); httpClient.DefaultRequestHeaders.Add("Origin", "https://movie.douban.com");
httpClient.DefaultRequestHeaders.Add("Referer", "https://movie.douban.com/"); httpClient.DefaultRequestHeaders.Add("Referer", "https://movie.douban.com/");
this.LoadLoadDoubanCookie();
Plugin.Instance!.ConfigurationChanged += (_, _) =>
{
this.LoadLoadDoubanCookie();
};
} }
private void EnsureLoadDoubanCookie() private void LoadLoadDoubanCookie()
{ {
var configCookie = Plugin.Instance?.Configuration.DoubanCookies.Trim() ?? string.Empty; var configCookie = Plugin.Instance?.Configuration.DoubanCookies.Trim() ?? string.Empty;
lock (_lock) lock (_lock)
{ {
if (oldLoadedCookies != configCookie) var uri = new Uri("https://douban.com/");
// 清空旧的cookie
var cookies = _cookieContainer.GetCookies(uri);
foreach (Cookie co in cookies)
{ {
oldLoadedCookies = configCookie; co.Expires = DateTime.Now.Subtract(TimeSpan.FromDays(1));
var uri = new Uri("https://douban.com/");
// 清空旧的cookie
var cookies = _cookieContainer.GetCookies(uri);
foreach (Cookie co in cookies)
{
co.Expires = DateTime.Now.Subtract(TimeSpan.FromDays(1));
}
// 附加新的cookie
if (!string.IsNullOrEmpty(configCookie))
{
var arr = configCookie.Split(';');
foreach (var str in arr)
{
var cookieArr = str.Split('=');
if (cookieArr.Length != 2)
{
continue;
}
var key = cookieArr[0].Trim();
var value = cookieArr[1].Trim();
try
{
_cookieContainer.Add(new Cookie(key, value, "/", ".douban.com"));
}
catch (Exception ex)
{
this._logger.LogError(ex, ex.Message);
}
}
}
} }
// 附加新的cookie
if (!string.IsNullOrEmpty(configCookie))
{
var arr = configCookie.Split(';');
foreach (var str in arr)
{
var cookieArr = str.Split('=');
if (cookieArr.Length != 2)
{
continue;
}
var key = cookieArr[0].Trim();
var value = cookieArr[1].Trim();
try
{
_cookieContainer.Add(new Cookie(key, value, "/", ".douban.com"));
}
catch (Exception ex)
{
this._logger.LogDebug(ex, ex.Message);
}
}
}
} }
} }
@ -168,15 +168,13 @@ namespace Jellyfin.Plugin.MetaShark.Api
} }
var cacheKey = $"search_{keyword}"; var cacheKey = $"search_{keyword}";
var expiredOption = new MemoryCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30) }; var expiredOption = new MemoryCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5) };
List<DoubanSubject> searchResult; List<DoubanSubject> searchResult;
if (_memoryCache.TryGetValue<List<DoubanSubject>>(cacheKey, out searchResult)) if (_memoryCache.TryGetValue<List<DoubanSubject>>(cacheKey, out searchResult))
{ {
return searchResult; return searchResult;
} }
EnsureLoadDoubanCookie();
await LimitRequestFrequently(); await LimitRequestFrequently();
var encodedKeyword = HttpUtility.UrlEncode(keyword); var encodedKeyword = HttpUtility.UrlEncode(keyword);
@ -243,7 +241,6 @@ namespace Jellyfin.Plugin.MetaShark.Api
return list; return list;
} }
EnsureLoadDoubanCookie();
await LimitRequestFrequently(); await LimitRequestFrequently();
try try
@ -307,7 +304,6 @@ namespace Jellyfin.Plugin.MetaShark.Api
return movie; return movie;
} }
EnsureLoadDoubanCookie();
await LimitRequestFrequently(); await LimitRequestFrequently();
var url = $"https://movie.douban.com/subject/{sid}/"; var url = $"https://movie.douban.com/subject/{sid}/";
@ -415,7 +411,6 @@ namespace Jellyfin.Plugin.MetaShark.Api
return celebrities; return celebrities;
} }
EnsureLoadDoubanCookie();
await LimitRequestFrequently(); await LimitRequestFrequently();
var list = new List<DoubanCelebrity>(); var list = new List<DoubanCelebrity>();
@ -491,8 +486,6 @@ namespace Jellyfin.Plugin.MetaShark.Api
return celebrity; return celebrity;
} }
EnsureLoadDoubanCookie();
var url = $"https://movie.douban.com/celebrity/{id}/"; var url = $"https://movie.douban.com/celebrity/{id}/";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false); var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
@ -567,9 +560,6 @@ namespace Jellyfin.Plugin.MetaShark.Api
} }
EnsureLoadDoubanCookie();
keyword = HttpUtility.UrlEncode(keyword); keyword = HttpUtility.UrlEncode(keyword);
var url = $"https://movie.douban.com/celebrities/search?search_text={keyword}"; var url = $"https://movie.douban.com/celebrities/search?search_text={keyword}";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false); var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
@ -621,7 +611,6 @@ namespace Jellyfin.Plugin.MetaShark.Api
return photos; return photos;
} }
EnsureLoadDoubanCookie();
await LimitRequestFrequently(); await LimitRequestFrequently();
try try
@ -682,8 +671,6 @@ namespace Jellyfin.Plugin.MetaShark.Api
public async Task<bool> CheckLoginAsync(CancellationToken cancellationToken) public async Task<bool> CheckLoginAsync(CancellationToken cancellationToken)
{ {
EnsureLoadDoubanCookie();
try try
{ {
var url = "https://www.douban.com/mine/"; var url = "https://www.douban.com/mine/";
@ -720,53 +707,6 @@ namespace Jellyfin.Plugin.MetaShark.Api
{ {
await this._defaultTimeConstraint; await this._defaultTimeConstraint;
} }
// var diff = 0;
// Double interval = 0.0;
// if (IsEnableAvoidRiskControl())
// {
// var configCookie = Plugin.Instance?.Configuration.DoubanCookies.Trim() ?? string.Empty;
// if (string.IsNullOrEmpty(configCookie))
// {
// interval = 3000;
// }
// else
// {
// interval = 6000;
// }
// // // 启用防止封禁
// // this._logger.LogWarning("thread开始等待." + Thread.CurrentThread.ManagedThreadId);
// // await this._limitTimeConstraint;
// // this._logger.LogWarning("thread等待结束." + Thread.CurrentThread.ManagedThreadId);
// }
// else
// {
// interval = 1000;
// // 默认限制
// // await this._defaultTimeConstraint;
// }
// this._logger.LogWarning("thread进入." + Thread.CurrentThread.ManagedThreadId);
// lock (_lock)
// {
// this._logger.LogWarning("thread开始等待." + Thread.CurrentThread.ManagedThreadId);
// lastRequestTime = lastRequestTime.AddMilliseconds(interval);
// diff = (int)(lastRequestTime - DateTime.Now).TotalMilliseconds;
// if (diff <= 0)
// {
// lastRequestTime = DateTime.Now;
// }
// }
// if (diff > 0)
// {
// this._logger.LogInformation("请求太频繁,等待{0}毫秒后继续执行..." + Thread.CurrentThread.ManagedThreadId, diff);
// // Thread.Sleep(diff);
// await Task.Delay(diff);
// }
// this._logger.LogWarning("thread等待结束." + Thread.CurrentThread.ManagedThreadId);
} }
private string GetTitle(string body) private string GetTitle(string body)

View File

@ -37,8 +37,8 @@ namespace Jellyfin.Plugin.MetaShark.Api
_logger = loggerFactory.CreateLogger<TmdbApi>(); _logger = loggerFactory.CreateLogger<TmdbApi>();
_memoryCache = new MemoryCache(new MemoryCacheOptions()); _memoryCache = new MemoryCache(new MemoryCacheOptions());
var config = Plugin.Instance?.Configuration; var config = Plugin.Instance?.Configuration;
var apiKey = config == null || string.IsNullOrEmpty(config.TmdbApiKey) ? DEFAULT_API_KEY : config.TmdbApiKey; var apiKey = string.IsNullOrEmpty(config?.TmdbApiKey) ? DEFAULT_API_KEY : config.TmdbApiKey;
var host = config == null || string.IsNullOrEmpty(config.TmdbHost) ? DEFAULT_API_HOST : config.TmdbHost; var host = string.IsNullOrEmpty(config?.TmdbHost) ? DEFAULT_API_HOST : config.TmdbHost;
_tmDbClient = new TMDbClient(apiKey, true, host); _tmDbClient = new TMDbClient(apiKey, true, host);
_tmDbClient.RequestTimeout = TimeSpan.FromSeconds(10); _tmDbClient.RequestTimeout = TimeSpan.FromSeconds(10);
// Not really interested in NotFoundException // Not really interested in NotFoundException