Skip to content

Commit

Permalink
降低依赖侵入,适配低版本
Browse files Browse the repository at this point in the history
  • Loading branch information
91270 committed Apr 9, 2022
1 parent 2d49401 commit bc72ca4
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 135 deletions.
6 changes: 3 additions & 3 deletions Emby.MeiamSub.Shooter/Emby.MeiamSub.Shooter.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<Version>1.0.5</Version>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
<FileVersion>1.0.6.0</FileVersion>
<Version>1.0.6</Version>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
Expand Down
6 changes: 3 additions & 3 deletions Emby.MeiamSub.Thunder/Emby.MeiamSub.Thunder.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.5.0</FileVersion>
<Version>1.0.5</Version>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
<FileVersion>1.0.6.0</FileVersion>
<Version>1.0.6</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
8 changes: 4 additions & 4 deletions Jellyfin.MeiamSub.Shooter/Jellyfin.MeiamSub.Shooter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>1.0.5</Version>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.5.0</FileVersion>
<Version>1.0.6</Version>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
<FileVersion>1.0.6.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\Release</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Jellyfin.Controller" Version="10.7.7" />
<PackageReference Include="Jellyfin.Controller" Version="10.7.0" />
</ItemGroup>

</Project>
153 changes: 80 additions & 73 deletions Jellyfin.MeiamSub.Shooter/ShooterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,12 +10,11 @@
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
{
Expand All @@ -31,8 +29,7 @@ public class ShooterProvider : ISubtitleProvider, IHasOrder
public const string SRT = "srt";

private readonly ILogger<ShooterProvider> _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";
Expand All @@ -44,10 +41,11 @@ public class ShooterProvider : ISubtitleProvider, IHasOrder
#endregion

#region 构造函数
public ShooterProvider(ILogger<ShooterProvider> logger, IJsonSerializer jsonSerializer)
public ShooterProvider(ILogger<ShooterProvider> logger)
{
_logger = logger;
_jsonSerializer = jsonSerializer;
_httpClient.Timeout = TimeSpan.FromSeconds(30);
_logger.LogDebug("MeiamSub.Shooter Init");
}
#endregion

Expand All @@ -61,7 +59,7 @@ public ShooterProvider(ILogger<ShooterProvider> logger, IJsonSerializer jsonSeri
/// <returns></returns>
public async Task<IEnumerable<RemoteSubtitleInfo>> 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);

Expand All @@ -84,63 +82,74 @@ private async Task<IEnumerable<RemoteSubtitleInfo>> SearchSubtitlesAsync(Subtitl

var hash = ComputeFileHash(fileInfo);

using (var _httpClient = new HttpClient())
HttpContent content = new FormUrlEncodedContent(new[]
{
var options = new {
new KeyValuePair<string, string>("filehash", hash),
new KeyValuePair<string, string>("pathinfo", request.MediaPath),
new KeyValuePair<string, string>("format", "json"),
new KeyValuePair<string, string>("lang", request.Language == "chi" ? "chn" : "eng"),
});

filehash = HttpUtility.UrlEncode(hash),
pathinfo = HttpUtility.UrlEncode(request.MediaPath),
format = "json",
lang = request.Language == "chi" ? "chn" : "eng"
};
using var options = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("http://www.shooter.cn/api/subapi.php"),
Content = content,
Headers =
{
UserAgent = { new ProductInfoHeaderValue(new ProductHeaderValue("Jellyfin.MeiamSub.Shooter")) },
Accept = { new MediaTypeWithQualityHeaderValue("*/*") }
}
};

_logger.LogInformation($"MeiamSub.Shooter Search | Request -> { _jsonSerializer.SerializeToString(options) }");
_logger.LogDebug($"MeiamSub.Shooter Search | Request -> { JsonSerializer.Serialize(options) }");

var response = await PostAsync($"http://www.shooter.cn/api/subapi.php", options);

_logger.LogInformation($"MeiamSub.Shooter Search | Response -> { _jsonSerializer.SerializeToString(response) }");
var response = await _httpClient.SendAsync(options).ConfigureAwait(false);

if (response.StatusCode == HttpStatusCode.OK && response.Headers.Any(m => m.Value.Contains("application/json")))
{
var subtitleResponse = _jsonSerializer.DeserializeFromStream<List<SubtitleResponseRoot>>(await response.Content.ReadAsStreamAsync());

if (subtitleResponse != null)
{
_logger.LogInformation($"MeiamSub.Shooter Search | Response -> { _jsonSerializer.SerializeToString(subtitleResponse) }");
_logger.LogDebug($"MeiamSub.Shooter Search | Response -> { JsonSerializer.Serialize(response) }");

var remoteSubtitleInfos = new List<RemoteSubtitleInfo>();
if (response.StatusCode == HttpStatusCode.OK && response.Headers.Any(m => m.Value.Contains("application/json")))
{
var subtitleResponse = JsonSerializer.Deserialize<List<SubtitleResponseRoot>>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));

foreach (var subFileInfo in subtitleResponse)
if (subtitleResponse != null)
{
_logger.LogDebug($"MeiamSub.Shooter Search | Response -> { JsonSerializer.Serialize(subtitleResponse) }");

var remoteSubtitleInfos = new List<RemoteSubtitleInfo>();

foreach (var subFileInfo in subtitleResponse)
{
foreach (var subFile in subFileInfo.Files)
{
foreach (var subFile in subFileInfo.Files)
remoteSubtitleInfos.Add(new RemoteSubtitleInfo()
{
remoteSubtitleInfos.Add(new RemoteSubtitleInfo()
Id = Base64Encode(JsonSerializer.Serialize(new DownloadSubInfo
{
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",
Url = subFile.Link,
Format = subFile.Ext,
Comment = $"Format : { ExtractFormat(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");
_logger.LogDebug($"MeiamSub.Shooter Search | Summary -> Get { remoteSubtitleInfos.Count } Subtitles");

return remoteSubtitleInfos;
}
return remoteSubtitleInfos;
}
}

_logger.LogInformation($"MeiamSub.Shooter Search | Summary -> Get 0 Subtitles");
_logger.LogDebug($"MeiamSub.Shooter Search | Summary -> Get 0 Subtitles");

}
return Array.Empty<RemoteSubtitleInfo>();
}
#endregion
Expand All @@ -156,7 +165,7 @@ public async Task<SubtitleResponse> GetSubtitles(string id, CancellationToken ca
{
await Task.Run(() =>
{
_logger.LogInformation($"MeiamSub.Shooter DownloadSub | Request -> {id}");
_logger.LogDebug($"MeiamSub.Shooter DownloadSub | Request -> {id}");
});

return await DownloadSubAsync(id);
Expand All @@ -169,31 +178,40 @@ await Task.Run(() =>
/// <returns></returns>
private async Task<SubtitleResponse> DownloadSubAsync(string info)
{
var downloadSub = _jsonSerializer.DeserializeFromString<DownloadSubInfo>(Base64Decode(info));
var downloadSub = JsonSerializer.Deserialize<DownloadSubInfo>(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.GetAsync(downloadSub.Url);
var response = await _httpClient.SendAsync(options).ConfigureAwait(false);

_logger.LogInformation($"MeiamSub.Shooter DownloadSub | Response -> { response.StatusCode }");
_logger.LogDebug($"MeiamSub.Shooter DownloadSub | Response -> { response.StatusCode }");

if (response.StatusCode == HttpStatusCode.OK)
{
if (response.StatusCode == HttpStatusCode.OK)
{

return new SubtitleResponse()
{
Language = downloadSub.Language,
IsForced = false,
Format = downloadSub.Format,
Stream = await response.Content.ReadAsStreamAsync(),
};
}
return new SubtitleResponse()
{
Language = downloadSub.Language,
IsForced = false,
Format = downloadSub.Format,
Stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false),
};
}

return new SubtitleResponse();

}
Expand Down Expand Up @@ -294,16 +312,5 @@ public static string ComputeFileHash(FileInfo fileInfo)
return ret;
}
#endregion

#region HTTP
public async Task<HttpResponseMessage> 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
}
}
4 changes: 2 additions & 2 deletions Jellyfin.MeiamSub.Thunder/Jellyfin.MeiamSub.Thunder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<TargetFramework>net5.0</TargetFramework>
<ApplicationIcon />
<StartupObject />
<Version>1.0.5</Version>
<Version>1.0.6</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\Release</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Jellyfin.Controller" Version="10.7.7" />
<PackageReference Include="Jellyfin.Controller" Version="10.7.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit bc72ca4

Please sign in to comment.