Minor improvements

This commit is contained in:
Bond_009 2021-12-14 23:48:38 +01:00
parent 2ad0dd1663
commit 31c044b1f5
8 changed files with 145 additions and 50 deletions

View File

@ -3,6 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Plugin.OpenSubtitles", "Jellyfin.Plugin.OpenSubtitles\Jellyfin.Plugin.OpenSubtitles.csproj", "{DCD0C8B9-A493-4A66-B8EE-8748D430B660}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Plugin.OpenSubtitles", "Jellyfin.Plugin.OpenSubtitles\Jellyfin.Plugin.OpenSubtitles.csproj", "{DCD0C8B9-A493-4A66-B8EE-8748D430B660}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AF4DDF0B-B564-4925-B73D-C24A61923D42}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Plugin.OpenSubtitles.Tests", "tests\Jellyfin.Plugin.OpenSubtitles.Tests\Jellyfin.Plugin.OpenSubtitles.Tests.csproj", "{C033E72D-81A7-4F3A-A097-9A3934657145}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -13,5 +17,12 @@ Global
{DCD0C8B9-A493-4A66-B8EE-8748D430B660}.Debug|Any CPU.Build.0 = Debug|Any CPU {DCD0C8B9-A493-4A66-B8EE-8748D430B660}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DCD0C8B9-A493-4A66-B8EE-8748D430B660}.Release|Any CPU.ActiveCfg = Release|Any CPU {DCD0C8B9-A493-4A66-B8EE-8748D430B660}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DCD0C8B9-A493-4A66-B8EE-8748D430B660}.Release|Any CPU.Build.0 = Release|Any CPU {DCD0C8B9-A493-4A66-B8EE-8748D430B660}.Release|Any CPU.Build.0 = Release|Any CPU
{C033E72D-81A7-4F3A-A097-9A3934657145}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C033E72D-81A7-4F3A-A097-9A3934657145}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C033E72D-81A7-4F3A-A097-9A3934657145}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C033E72D-81A7-4F3A-A097-9A3934657145}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C033E72D-81A7-4F3A-A097-9A3934657145} = {AF4DDF0B-B564-4925-B73D-C24A61923D42}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -127,7 +126,11 @@ namespace Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler
/// <returns>The list of response data.</returns> /// <returns>The list of response data.</returns>
public static async Task<ApiResponse<IReadOnlyList<ResponseData>>> SearchSubtitlesAsync(Dictionary<string, string> options, string apiKey, CancellationToken cancellationToken) public static async Task<ApiResponse<IReadOnlyList<ResponseData>>> SearchSubtitlesAsync(Dictionary<string, string> options, string apiKey, CancellationToken cancellationToken)
{ {
var opts = System.Web.HttpUtility.ParseQueryString(string.Empty); var opts = new Dictionary<string, string>();
foreach (var (key, value) in options)
{
opts.Add(key.ToLowerInvariant(), value.ToLowerInvariant());
}
var max = -1; var max = -1;
var current = 1; var current = 1;
@ -138,23 +141,15 @@ namespace Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler
do do
{ {
opts.Clear();
if (current > 1) if (current > 1)
{ {
options["page"] = current.ToString(CultureInfo.InvariantCulture); options["page"] = current.ToString(CultureInfo.InvariantCulture);
} }
foreach (var (key, value) in options.OrderBy(x => x.Key)) var url = RequestHandler.AddQueryString("/subtitles", opts);
{ response = await RequestHandler.SendRequestAsync(url, HttpMethod.Get, null, null, apiKey, 1, cancellationToken).ConfigureAwait(false);
opts.Add(key.ToLower(CultureInfo.InvariantCulture), value.ToLower(CultureInfo.InvariantCulture));
}
var qs = opts.ToString()!.Replace("%20", "+", StringComparison.Ordinal); last = new ApiResponse<SearchResult>(response, $"url: {url}", $"page: {current}");
response = await RequestHandler.SendRequestAsync($"/subtitles?{qs}", HttpMethod.Get, null, null, apiKey, 1, cancellationToken).ConfigureAwait(false);
last = new ApiResponse<SearchResult>(response, $"query: {qs}", $"page: {current}");
if (!last.Ok || last.Data == null) if (!last.Ok || last.Data == null)
{ {

View File

@ -1,13 +1,12 @@
using System; using System;
using System.Buffers.Binary;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Mime; using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -38,45 +37,32 @@ namespace Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler
public static OpenSubtitlesRequestHelper? Instance { get; set; } public static OpenSubtitlesRequestHelper? Instance { get; set; }
/// <summary> /// <summary>
/// Compute movie hash. /// Calculates: size + 64bit chksum of the first and last 64k (even if they overlap because the file is smaller than 128k).
/// </summary> /// </summary>
/// <param name="stream">The input stream.</param> /// <param name="input">The input stream.</param>
/// <returns>The hash as Hexadecimal string.</returns> /// <returns>The hash as Hexadecimal string.</returns>
public static string ComputeHash(Stream stream) public static string ComputeHash(Stream input)
{ {
var hash = ComputeMovieHash(stream); const int HashLength = 8; // 64 bit hash
return Convert.ToHexString(hash).ToLowerInvariant(); const long HashPos = 64 * 1024; // 64k
}
/// <summary> long streamsize = input.Length;
/// Compute hash of specified movie stream. ulong hash = (ulong)streamsize;
/// </summary>
/// <returns>Hash of the movie.</returns> Span<byte> buffer = stackalloc byte[HashLength];
private static byte[] ComputeMovieHash(Stream input) while (input.Position < HashPos && input.Read(buffer) > 0)
{
using (input)
{ {
long streamSize = input.Length, lHash = streamSize; hash += BinaryPrimitives.ReadUInt64LittleEndian(buffer);
int size = sizeof(long), count = 65536 / size;
var buffer = new byte[size];
for (int i = 0; i < count && input.Read(buffer, 0, size) > 0; i++)
{
lHash += BitConverter.ToInt64(buffer, 0);
}
input.Position = Math.Max(0, streamSize - 65536);
for (int i = 0; i < count && input.Read(buffer, 0, size) > 0; i++)
{
lHash += BitConverter.ToInt64(buffer, 0);
}
var result = BitConverter.GetBytes(lHash);
Array.Reverse(result);
return result;
} }
input.Seek(-HashPos, SeekOrigin.End);
while (input.Read(buffer) > 0)
{
hash += BinaryPrimitives.ReadUInt64LittleEndian(buffer);
}
BinaryPrimitives.WriteUInt64BigEndian(buffer, hash);
return Convert.ToHexString(buffer).ToLowerInvariant();
} }
internal async Task<(string body, Dictionary<string, string> headers, HttpStatusCode statusCode)> SendRequestAsync( internal async Task<(string body, Dictionary<string, string> headers, HttpStatusCode statusCode)> SendRequestAsync(
@ -91,7 +77,7 @@ namespace Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler
HttpContent? content = null; HttpContent? content = null;
if (method != HttpMethod.Get && body != null) if (method != HttpMethod.Get && body != null)
{ {
content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, MediaTypeNames.Application.Json); content = JsonContent.Create(body);
} }
using var request = new HttpRequestMessage using var request = new HttpRequestMessage

View File

@ -1,9 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web;
using Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler.Models; using Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler.Models;
namespace Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler namespace Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler
@ -121,5 +124,32 @@ namespace Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler
Reason = value Reason = value
}; };
} }
/// <summary>
/// Append the given query keys and values to the URI.
/// </summary>
/// <param name="path">The base URI.</param>
/// <param name="param">A dictionary of query keys and values to append.</param>
/// <returns>The combined result.</returns>
public static string AddQueryString(string path, Dictionary<string, string> param)
{
if (param.Count == 0)
{
return path;
}
var url = new StringBuilder(path);
url.Append('?');
foreach (var (key, value) in param.OrderBy(x => x.Key))
{
url.Append(HttpUtility.UrlEncode(key))
.Append('=')
.Append(HttpUtility.UrlEncode(value))
.Append('&');
}
url.Length -= 1; // Remove last &
return url.ToString();
}
} }
} }

View File

@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Include="Test Data\**\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../Jellyfin.Plugin.OpenSubtitles/Jellyfin.Plugin.OpenSubtitles.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,15 @@
using System.IO;
using Xunit;
namespace Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler.Tests;
public class OpenSubtitlesRequestHelperTests
{
[Theory]
[InlineData("breakdance.avi", "8e245d9679d31e12")]
public void ComputeHash_Success(string filename, string hash)
{
using var str = File.OpenRead(Path.Join("Test Data", filename));
Assert.Equal(hash, OpenSubtitlesRequestHelper.ComputeHash(str));
}
}

View File

@ -0,0 +1,26 @@
using System.Collections.Generic;
using Xunit;
namespace Jellyfin.Plugin.OpenSubtitles.OpenSubtitlesHandler.Tests;
public static class RequestHandlerTests
{
public static TheoryData<string, Dictionary<string, string>, string> ComputeHash_Success_TestData()
=> new TheoryData<string, Dictionary<string, string>, string>
{
{
"/subtitles",
new Dictionary<string, string>()
{
{ "b", "c and d" },
{ "a", "1" }
},
"/subtitles?a=1&b=c+and+d"
}
};
[Theory]
[MemberData(nameof(ComputeHash_Success_TestData))]
public static void ComputeHash_Success(string path, Dictionary<string, string> param, string expected)
=> Assert.Equal(expected, RequestHandler.AddQueryString(path, param));
}