Optimize douban cookie

This commit is contained in:
cxfksword 2022-12-23 21:45:29 +08:00
parent ef4509d3f6
commit 33d921f1a9
2 changed files with 58 additions and 26 deletions

View File

@ -39,6 +39,8 @@ namespace Jellyfin.Plugin.MetaShark.Api
const string HTTP_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.44"; const string HTTP_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.44";
private readonly ILogger<DoubanApi> _logger; private readonly ILogger<DoubanApi> _logger;
private HttpClient httpClient; private HttpClient httpClient;
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();
@ -83,7 +85,7 @@ namespace Jellyfin.Plugin.MetaShark.Api
_memoryCache = new MemoryCache(new MemoryCacheOptions()); _memoryCache = new MemoryCache(new MemoryCacheOptions());
var handler = new HttpClientHandlerEx(); var handler = new HttpClientHandlerEx();
this.SetDoubanCookie(handler.CookieContainer); this._cookieContainer = handler.CookieContainer;
httpClient = new HttpClient(handler, true); httpClient = new HttpClient(handler, true);
httpClient.Timeout = TimeSpan.FromSeconds(10); httpClient.Timeout = TimeSpan.FromSeconds(10);
httpClient.DefaultRequestHeaders.Add("user-agent", HTTP_USER_AGENT); httpClient.DefaultRequestHeaders.Add("user-agent", HTTP_USER_AGENT);
@ -93,34 +95,52 @@ namespace Jellyfin.Plugin.MetaShark.Api
private void SetDoubanCookie(CookieContainer cookieContainer) private void EnsureLoadDoubanCookie()
{ {
var configCookie = Plugin.Instance?.Configuration.DoubanCookies.Trim() ?? string.Empty; var configCookie = Plugin.Instance?.Configuration.DoubanCookies.Trim() ?? string.Empty;
if (string.IsNullOrEmpty(configCookie))
{
return;
}
var uri = new Uri("https://douban.com/"); lock (_lock)
var arr = configCookie.Split(';');
foreach (var str in arr)
{ {
var cookieArr = str.Split('='); if (oldLoadedCookies != configCookie)
if (cookieArr.Length != 2)
{ {
continue; oldLoadedCookies = configCookie;
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);
}
}
}
} }
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);
}
} }
} }
@ -141,6 +161,8 @@ namespace Jellyfin.Plugin.MetaShark.Api
} }
EnsureLoadDoubanCookie();
keyword = HttpUtility.UrlEncode(keyword); keyword = HttpUtility.UrlEncode(keyword);
var url = $"https://www.douban.com/search?cat=1002&q={keyword}"; var url = $"https://www.douban.com/search?cat=1002&q={keyword}";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false); var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
@ -206,6 +228,8 @@ namespace Jellyfin.Plugin.MetaShark.Api
return movie; return movie;
} }
EnsureLoadDoubanCookie();
var url = $"https://movie.douban.com/subject/{sid}/"; var url = $"https://movie.douban.com/subject/{sid}/";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false); var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
@ -314,6 +338,8 @@ namespace Jellyfin.Plugin.MetaShark.Api
return celebrities; return celebrities;
} }
EnsureLoadDoubanCookie();
var list = new List<DoubanCelebrity>(); var list = new List<DoubanCelebrity>();
var url = $"https://movie.douban.com/subject/{sid}/celebrities"; var url = $"https://movie.douban.com/subject/{sid}/celebrities";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false); var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
@ -381,6 +407,8 @@ 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();
@ -454,6 +482,8 @@ namespace Jellyfin.Plugin.MetaShark.Api
return searchResult; return searchResult;
} }
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}";
@ -505,6 +535,8 @@ namespace Jellyfin.Plugin.MetaShark.Api
return photos; return photos;
} }
EnsureLoadDoubanCookie();
var list = new List<DoubanPhoto>(); var list = new List<DoubanPhoto>();
var url = $"https://movie.douban.com/subject/{sid}/photos?type=W&start=0&sortby=size&size=a&subtype=a"; var url = $"https://movie.douban.com/subject/{sid}/photos?type=W&start=0&sortby=size&size=a&subtype=a";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false); var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);

View File

@ -25,8 +25,8 @@
<div class="inputContainer"> <div class="inputContainer">
<label class="inputLabel inputLabelUnfocused" for="DoubanCookies">豆瓣网站cookie</label> <label class="inputLabel inputLabelUnfocused" for="DoubanCookies">豆瓣网站cookie</label>
<textarea rows="5" is="emby-input" type="text" id="DoubanCookies" name="DoubanCookies" <textarea rows="5" is="emby-input" type="text" id="DoubanCookies" name="DoubanCookies"
class="emby-input"></textarea> class="emby-input" placeholder="_vwo_uuid_v2=1; __utmv=2; ..."></textarea>
<div class="fieldDescription">可为空,填写可搜索到需登录访问的影片.(需重启才能生效)</div> <div class="fieldDescription">可为空,填写可搜索到需登录访问的影片,使用分号“;”分隔格式cookie.</div>
</div> </div>
<fieldset class="verticalSection verticalSection-extrabottompadding"> <fieldset class="verticalSection verticalSection-extrabottompadding">
@ -38,7 +38,7 @@
<input id="EnableTmdb" name="EnableTmdb" type="checkbox" is="emby-checkbox" /> <input id="EnableTmdb" name="EnableTmdb" type="checkbox" is="emby-checkbox" />
<span>启用从TheMovieDb获取元数据</span> <span>启用从TheMovieDb获取元数据</span>
</label> </label>
<div class="fieldDescription">勾选后会尝试从TheMovieDb获取季度和剧集元数据</div> <div class="fieldDescription">勾选后会尝试从TheMovieDb获取剧集元数据</div>
</div> </div>
<div class="checkboxContainer checkboxContainer-withDescription"> <div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label" for="EnableTmdbSearch"> <label class="emby-checkbox-label" for="EnableTmdbSearch">
@ -78,7 +78,7 @@
Dashboard.showLoadingMsg(); Dashboard.showLoadingMsg();
ApiClient.getPluginConfiguration(TemplateConfig.pluginUniqueId).then(function (config) { ApiClient.getPluginConfiguration(TemplateConfig.pluginUniqueId).then(function (config) {
$('#current_version').text("v" + config.Version); $('#current_version').text("v" + config.Version);
document.querySelector('#DoubanCookies').value = config.DoubanCookies; document.querySelector('#DoubanCookies').value = config.DoubanCookies;
document.querySelector('#EnableTmdb').checked = config.EnableTmdb; document.querySelector('#EnableTmdb').checked = config.EnableTmdb;
document.querySelector('#EnableTmdbSearch').checked = config.EnableTmdbSearch; document.querySelector('#EnableTmdbSearch').checked = config.EnableTmdbSearch;