diff --git a/Emby.MeiamSub.Shooter/Emby.MeiamSub.Shooter.csproj b/Emby.MeiamSub.Shooter/Emby.MeiamSub.Shooter.csproj index b4aeeb4..acaf4e2 100644 --- a/Emby.MeiamSub.Shooter/Emby.MeiamSub.Shooter.csproj +++ b/Emby.MeiamSub.Shooter/Emby.MeiamSub.Shooter.csproj @@ -1,8 +1,8 @@ - 1.0.5.0 - 1.0.4.0 - 1.0.5 + 1.0.6.0 + 1.0.6.0 + 1.0.6 netcoreapp3.1 diff --git a/Emby.MeiamSub.Thunder/Emby.MeiamSub.Thunder.csproj b/Emby.MeiamSub.Thunder/Emby.MeiamSub.Thunder.csproj index 11ce7f0..ad0be48 100644 --- a/Emby.MeiamSub.Thunder/Emby.MeiamSub.Thunder.csproj +++ b/Emby.MeiamSub.Thunder/Emby.MeiamSub.Thunder.csproj @@ -1,9 +1,9 @@  - 1.0.5.0 - 1.0.5.0 - 1.0.5 + 1.0.6.0 + 1.0.6.0 + 1.0.6 diff --git a/Jellyfin.MeiamSub.Shooter/Jellyfin.MeiamSub.Shooter.csproj b/Jellyfin.MeiamSub.Shooter/Jellyfin.MeiamSub.Shooter.csproj index 39fe3e7..ae9a294 100644 --- a/Jellyfin.MeiamSub.Shooter/Jellyfin.MeiamSub.Shooter.csproj +++ b/Jellyfin.MeiamSub.Shooter/Jellyfin.MeiamSub.Shooter.csproj @@ -2,9 +2,9 @@ net5.0 - 1.0.5 - 1.0.5.0 - 1.0.5.0 + 1.0.6 + 1.0.6.0 + 1.0.6.0 @@ -12,7 +12,7 @@ - + diff --git a/Jellyfin.MeiamSub.Shooter/ShooterProvider.cs b/Jellyfin.MeiamSub.Shooter/ShooterProvider.cs index aac9b3b..d469d5c 100644 --- a/Jellyfin.MeiamSub.Shooter/ShooterProvider.cs +++ b/Jellyfin.MeiamSub.Shooter/ShooterProvider.cs @@ -2,7 +2,6 @@ using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Subtitles; using MediaBrowser.Model.Providers; -using MediaBrowser.Model.Serialization; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -11,12 +10,11 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; -using System.Net.Http.Json; using System.Security.Cryptography; using System.Text; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; -using System.Web; namespace Jellyfin.MeiamSub.Shooter { @@ -31,8 +29,7 @@ namespace Jellyfin.MeiamSub.Shooter public const string SRT = "srt"; private readonly ILogger _logger; - private readonly IJsonSerializer _jsonSerializer; - private static readonly HttpClient Client = new HttpClient(); + private static readonly HttpClient _httpClient = new HttpClient(); public int Order => 0; public string Name => "MeiamSub.Shooter"; @@ -44,10 +41,11 @@ namespace Jellyfin.MeiamSub.Shooter #endregion #region 构造函数 - public ShooterProvider(ILogger logger, IJsonSerializer jsonSerializer) + public ShooterProvider(ILogger logger) { _logger = logger; - _jsonSerializer = jsonSerializer; + _httpClient.Timeout = TimeSpan.FromSeconds(30); + _logger.LogDebug("MeiamSub.Shooter Init"); } #endregion @@ -61,7 +59,7 @@ namespace Jellyfin.MeiamSub.Shooter /// public async Task> Search(SubtitleSearchRequest request, CancellationToken cancellationToken) { - _logger.LogInformation($"MeiamSub.Shooter Search | Request -> { _jsonSerializer.SerializeToString(request) }"); + _logger.LogDebug($"MeiamSub.Shooter Search | Request -> { JsonSerializer.Serialize(request) }"); var subtitles = await SearchSubtitlesAsync(request); @@ -84,63 +82,74 @@ namespace Jellyfin.MeiamSub.Shooter var hash = ComputeFileHash(fileInfo); - using (var _httpClient = new HttpClient()) + HttpContent content = new FormUrlEncodedContent(new[] { - var options = new { + new KeyValuePair("filehash", hash), + new KeyValuePair("pathinfo", request.MediaPath), + new KeyValuePair("format", "json"), + new KeyValuePair("lang", request.Language == "chi" ? "chn" : "eng"), + }); - filehash = HttpUtility.UrlEncode(hash), - pathinfo = HttpUtility.UrlEncode(request.MediaPath), - format = "json", - lang = request.Language == "chi" ? "chn" : "eng" - }; - - _logger.LogInformation($"MeiamSub.Shooter Search | Request -> { _jsonSerializer.SerializeToString(options) }"); - - var response = await PostAsync($"http://www.shooter.cn/api/subapi.php", options); - - _logger.LogInformation($"MeiamSub.Shooter Search | Response -> { _jsonSerializer.SerializeToString(response) }"); - - if (response.StatusCode == HttpStatusCode.OK && response.Headers.Any(m => m.Value.Contains("application/json"))) + using var options = new HttpRequestMessage + { + Method = HttpMethod.Post, + RequestUri = new Uri("http://www.shooter.cn/api/subapi.php"), + Content = content, + Headers = { - var subtitleResponse = _jsonSerializer.DeserializeFromStream>(await response.Content.ReadAsStreamAsync()); - - if (subtitleResponse != null) - { - _logger.LogInformation($"MeiamSub.Shooter Search | Response -> { _jsonSerializer.SerializeToString(subtitleResponse) }"); - - var remoteSubtitleInfos = new List(); - - foreach (var subFileInfo in subtitleResponse) - { - foreach (var subFile in subFileInfo.Files) - { - remoteSubtitleInfos.Add(new RemoteSubtitleInfo() - { - Id = Base64Encode(_jsonSerializer.SerializeToString(new DownloadSubInfo - { - Url = subFile.Link, - Format = subFile.Ext, - Language = request.Language, - TwoLetterISOLanguageName = request.TwoLetterISOLanguageName, - })), - Name = $"[MEIAMSUB] { Path.GetFileName(request.MediaPath) } | {request.TwoLetterISOLanguageName} | 射手", - Author = "Meiam ", - ProviderName = "MeiamSub.Shooter", - Format = subFile.Ext, - Comment = $"Format : { ExtractFormat(subFile.Ext)}" - }); - } - } - - _logger.LogInformation($"MeiamSub.Shooter Search | Summary -> Get { remoteSubtitleInfos.Count } Subtitles"); - - return remoteSubtitleInfos; - } + UserAgent = { new ProductInfoHeaderValue(new ProductHeaderValue("Jellyfin.MeiamSub.Shooter")) }, + Accept = { new MediaTypeWithQualityHeaderValue("*/*") } } + }; - _logger.LogInformation($"MeiamSub.Shooter Search | Summary -> Get 0 Subtitles"); + _logger.LogDebug($"MeiamSub.Shooter Search | Request -> { JsonSerializer.Serialize(options) }"); + + var response = await _httpClient.SendAsync(options).ConfigureAwait(false); + + + _logger.LogDebug($"MeiamSub.Shooter Search | Response -> { JsonSerializer.Serialize(response) }"); + + if (response.StatusCode == HttpStatusCode.OK && response.Headers.Any(m => m.Value.Contains("application/json"))) + { + var subtitleResponse = JsonSerializer.Deserialize>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); + + if (subtitleResponse != null) + { + _logger.LogDebug($"MeiamSub.Shooter Search | Response -> { JsonSerializer.Serialize(subtitleResponse) }"); + + var remoteSubtitleInfos = new List(); + + foreach (var subFileInfo in subtitleResponse) + { + foreach (var subFile in subFileInfo.Files) + { + remoteSubtitleInfos.Add(new RemoteSubtitleInfo() + { + Id = Base64Encode(JsonSerializer.Serialize(new DownloadSubInfo + { + Url = subFile.Link, + Format = subFile.Ext, + Language = request.Language, + TwoLetterISOLanguageName = request.TwoLetterISOLanguageName + })), + Name = $"[MEIAMSUB] { Path.GetFileName(request.MediaPath) } | {request.TwoLetterISOLanguageName} | 射手", + Author = "Meiam ", + ProviderName = "MeiamSub.Shooter", + Format = subFile.Ext, + Comment = $"Format : { ExtractFormat(subFile.Ext)}" + }); + } + } + + _logger.LogDebug($"MeiamSub.Shooter Search | Summary -> Get { remoteSubtitleInfos.Count } Subtitles"); + + return remoteSubtitleInfos; + } } + + _logger.LogDebug($"MeiamSub.Shooter Search | Summary -> Get 0 Subtitles"); + return Array.Empty(); } #endregion @@ -156,7 +165,7 @@ namespace Jellyfin.MeiamSub.Shooter { await Task.Run(() => { - _logger.LogInformation($"MeiamSub.Shooter DownloadSub | Request -> {id}"); + _logger.LogDebug($"MeiamSub.Shooter DownloadSub | Request -> {id}"); }); return await DownloadSubAsync(id); @@ -169,31 +178,40 @@ namespace Jellyfin.MeiamSub.Shooter /// private async Task DownloadSubAsync(string info) { - var downloadSub = _jsonSerializer.DeserializeFromString(Base64Decode(info)); + var downloadSub = JsonSerializer.Deserialize(Base64Decode(info)); downloadSub.Url = downloadSub.Url.Replace("https://www.shooter.cn", "http://www.shooter.cn"); - _logger.LogInformation($"MeiamSub.Shooter DownloadSub | Url -> { downloadSub.Url } | Format -> { downloadSub.Format } | Language -> { downloadSub.Language } "); + _logger.LogDebug($"MeiamSub.Shooter DownloadSub | Url -> { downloadSub.Url } | Format -> { downloadSub.Format } | Language -> { downloadSub.Language } "); - using (var _httpClient = new HttpClient()) + + using var options = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri(downloadSub.Url), + Headers = + { + UserAgent = { new ProductInfoHeaderValue(new ProductHeaderValue("Emby.MeiamSub.Shooter")) }, + Accept = { new MediaTypeWithQualityHeaderValue("*/*") } + } + }; + + var response = await _httpClient.SendAsync(options).ConfigureAwait(false); + + _logger.LogDebug($"MeiamSub.Shooter DownloadSub | Response -> { response.StatusCode }"); + + if (response.StatusCode == HttpStatusCode.OK) { - var response = await _httpClient.GetAsync(downloadSub.Url); - - _logger.LogInformation($"MeiamSub.Shooter DownloadSub | Response -> { response.StatusCode }"); - - if (response.StatusCode == HttpStatusCode.OK) + return new SubtitleResponse() { - - return new SubtitleResponse() - { - Language = downloadSub.Language, - IsForced = false, - Format = downloadSub.Format, - Stream = await response.Content.ReadAsStreamAsync(), - }; - } + Language = downloadSub.Language, + IsForced = false, + Format = downloadSub.Format, + Stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false), + }; } + return new SubtitleResponse(); } @@ -294,16 +312,5 @@ namespace Jellyfin.MeiamSub.Shooter return ret; } #endregion - - #region HTTP - public async Task PostAsync(string url, object data) - { - string content = _jsonSerializer.SerializeToString(data); - var buffer = Encoding.UTF8.GetBytes(content); - var byteContent = new ByteArrayContent(buffer); - byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); - return await Client.PostAsync(url, byteContent); - } - #endregion } } diff --git a/Jellyfin.MeiamSub.Thunder/Jellyfin.MeiamSub.Thunder.csproj b/Jellyfin.MeiamSub.Thunder/Jellyfin.MeiamSub.Thunder.csproj index 9ecd5b6..41adbb5 100644 --- a/Jellyfin.MeiamSub.Thunder/Jellyfin.MeiamSub.Thunder.csproj +++ b/Jellyfin.MeiamSub.Thunder/Jellyfin.MeiamSub.Thunder.csproj @@ -5,7 +5,7 @@ net5.0 - 1.0.5 + 1.0.6 @@ -13,7 +13,7 @@ - + diff --git a/Jellyfin.MeiamSub.Thunder/ThunderProvider.cs b/Jellyfin.MeiamSub.Thunder/ThunderProvider.cs index f6b1e05..d4efa37 100644 --- a/Jellyfin.MeiamSub.Thunder/ThunderProvider.cs +++ b/Jellyfin.MeiamSub.Thunder/ThunderProvider.cs @@ -2,7 +2,6 @@ using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Subtitles; using MediaBrowser.Model.Providers; -using MediaBrowser.Model.Serialization; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -10,8 +9,10 @@ using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Security.Cryptography; using System.Text; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -20,7 +21,7 @@ namespace Jellyfin.MeiamSub.Thunder /// /// 迅雷字幕组件 /// - public class ThunderProvider : ISubtitleProvider + public class ThunderProvider : ISubtitleProvider, IHasOrder { #region 变量声明 public const string ASS = "ass"; @@ -28,10 +29,9 @@ namespace Jellyfin.MeiamSub.Thunder public const string SRT = "srt"; private readonly ILogger _logger; - private readonly IJsonSerializer _jsonSerializer; + private static readonly HttpClient _httpClient = new HttpClient(); public int Order => 0; - public string Name => "MeiamSub.Thunder"; /// @@ -41,10 +41,11 @@ namespace Jellyfin.MeiamSub.Thunder #endregion #region 构造函数 - public ThunderProvider(ILogger logger, IJsonSerializer jsonSerializer) + public ThunderProvider(ILogger logger) { _logger = logger; - _jsonSerializer = jsonSerializer; + _httpClient.Timeout = TimeSpan.FromSeconds(30); + _logger.LogDebug("MeiamSub.Thunder Init"); } #endregion @@ -58,7 +59,7 @@ namespace Jellyfin.MeiamSub.Thunder /// public async Task> Search(SubtitleSearchRequest request, CancellationToken cancellationToken) { - _logger.LogDebug($"MeiamSub.Thunder Search | Request -> { _jsonSerializer.SerializeToString(request) }"); + _logger.LogDebug($"MeiamSub.Thunder Search | Request -> { JsonSerializer.Serialize(request) }"); var subtitles = await SearchSubtitlesAsync(request); @@ -79,48 +80,55 @@ namespace Jellyfin.MeiamSub.Thunder var cid = GetCidByFile(request.MediaPath); - - using (var _httpClient = new HttpClient()) + using var options = new HttpRequestMessage { - var response = await _httpClient.GetAsync($"http://sub.xmp.sandai.net:8000/subxl/{cid}.json"); - - _logger.LogDebug($"MeiamSub.Thunder Search | Response -> { _jsonSerializer.SerializeToString(response) }"); - - if (response.StatusCode == HttpStatusCode.OK) - { - var subtitleResponse = _jsonSerializer.DeserializeFromStream(await response.Content.ReadAsStreamAsync()); - - if (subtitleResponse != null) + Method = HttpMethod.Get, + RequestUri = new Uri($"http://sub.xmp.sandai.net:8000/subxl/{cid}.json"), + Headers = { - _logger.LogDebug($"MeiamSub.Thunder Search | Response -> { _jsonSerializer.SerializeToString(subtitleResponse) }"); + UserAgent = { new ProductInfoHeaderValue(new ProductHeaderValue("Jellyfin.MeiamSub.Thunder")) }, + Accept = { new MediaTypeWithQualityHeaderValue("*/*") } + } + }; - var subtitles = subtitleResponse.sublist.Where(m => !string.IsNullOrEmpty(m.sname)); + var response = await _httpClient.SendAsync(options).ConfigureAwait(false); - if (subtitles.Count() > 0) + _logger.LogDebug($"MeiamSub.Thunder Search | Response -> { JsonSerializer.Serialize(response) }"); + + if (response.StatusCode == HttpStatusCode.OK) + { + var subtitleResponse = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); + + if (subtitleResponse != null) + { + _logger.LogDebug($"MeiamSub.Thunder Search | Response -> { JsonSerializer.Serialize(subtitleResponse) }"); + + var subtitles = subtitleResponse.sublist.Where(m => !string.IsNullOrEmpty(m.sname)); + + if (subtitles.Count() > 0) + { + _logger.LogDebug($"MeiamSub.Thunder Search | Summary -> Get { subtitles.Count() } Subtitles"); + + return subtitles.Select(m => new RemoteSubtitleInfo() { - _logger.LogDebug($"MeiamSub.Thunder Search | Summary -> Get { subtitles.Count() } Subtitles"); - - return subtitles.Select(m => new RemoteSubtitleInfo() + Id = Base64Encode(JsonSerializer.Serialize(new DownloadSubInfo { - Id = Base64Encode(_jsonSerializer.SerializeToString(new DownloadSubInfo - { - Url = m.surl, - Format = ExtractFormat(m.sname), - Language = request.Language, - TwoLetterISOLanguageName = request.TwoLetterISOLanguageName, - })), - Name = $"[MEIAMSUB] { Path.GetFileName(request.MediaPath) } | {request.TwoLetterISOLanguageName} | 迅雷", - Author = "Meiam ", - CommunityRating = Convert.ToSingle(m.rate), - ProviderName = "MeiamSub.Thunder", + Url = m.surl, Format = ExtractFormat(m.sname), - Comment = $"Format : { ExtractFormat(m.sname)} - Rate : { m.rate }" - }).OrderByDescending(m => m.CommunityRating); - } + Language = request.Language, + TwoLetterISOLanguageName = request.TwoLetterISOLanguageName, + })), + Name = $"[MEIAMSUB] { Path.GetFileName(request.MediaPath) } | {request.TwoLetterISOLanguageName} | 迅雷", + Author = "Meiam ", + CommunityRating = Convert.ToSingle(m.rate), + ProviderName = "MeiamSub.Thunder", + Format = ExtractFormat(m.sname), + Comment = $"Format : { ExtractFormat(m.sname)} - Rate : { m.rate }" + }).OrderByDescending(m => m.CommunityRating); } } - } + _logger.LogDebug($"MeiamSub.Thunder Search | Summary -> Get 0 Subtitles"); return Array.Empty(); @@ -151,29 +159,37 @@ namespace Jellyfin.MeiamSub.Thunder /// private async Task DownloadSubAsync(string info) { - var downloadSub = _jsonSerializer.DeserializeFromString(Base64Decode(info)); + var downloadSub = JsonSerializer.Deserialize(Base64Decode(info)); _logger.LogDebug($"MeiamSub.Thunder DownloadSub | Url -> { downloadSub.Url } | Format -> { downloadSub.Format } | Language -> { downloadSub.Language } "); - using (var _httpClient = new HttpClient()) + using var options = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri(downloadSub.Url), + Headers = + { + UserAgent = { new ProductInfoHeaderValue(new ProductHeaderValue("Jellyfin.MeiamSub.Thunder")) }, + Accept = { new MediaTypeWithQualityHeaderValue("*/*") } + } + }; + + var response = await _httpClient.SendAsync(options).ConfigureAwait(false); + + _logger.LogDebug($"MeiamSub.Thunder DownloadSub | Response -> { response.StatusCode }"); + + if (response.StatusCode == HttpStatusCode.OK) { - var response = await _httpClient.GetAsync(downloadSub.Url); - - _logger.LogDebug($"MeiamSub.Thunder DownloadSub | Response -> { response.StatusCode }"); - - if (response.StatusCode == HttpStatusCode.OK) + return new SubtitleResponse() { - - return new SubtitleResponse() - { - Language = downloadSub.Language, - IsForced = false, - Format = downloadSub.Format, - Stream = await response.Content.ReadAsStreamAsync(), - }; - } + Language = downloadSub.Language, + IsForced = false, + Format = downloadSub.Format, + Stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false), + }; } + return new SubtitleResponse(); }