Skip to content

Commit

Permalink
Merge pull request #8 from cxfksword/youku
Browse files Browse the repository at this point in the history
Add youku danmu
  • Loading branch information
cxfksword authored Feb 2, 2023
2 parents fb5f2a0 + 1a7a684 commit db3cb61
Show file tree
Hide file tree
Showing 48 changed files with 2,427 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jellyfin.Plugin.Danmu/Vendor/** linguist-vendored
41 changes: 18 additions & 23 deletions Jellyfin.Plugin.Danmu.Test/Jellyfin.Plugin.Danmu.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Jellyfin.Plugin.Danmu\Jellyfin.Plugin.Danmu.csproj" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Jellyfin.Plugin.Danmu\Jellyfin.Plugin.Danmu.csproj" />
</ItemGroup>
</Project>
107 changes: 107 additions & 0 deletions Jellyfin.Plugin.Danmu.Test/YoukuApiTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Jellyfin.Plugin.Danmu.Model;
using Jellyfin.Plugin.Danmu.Scrapers.Youku;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Plugin.Danmu.Test
{

[TestClass]
public class YoukuApiTest
{
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
builder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "hh:mm:ss ";
}));

[TestMethod]
public void TestSearch()
{
var keyword = "夏洛特";
var api = new YoukuApi(loggerFactory);

Task.Run(async () =>
{
try
{
var result = await api.SearchAsync(keyword, CancellationToken.None);
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}).GetAwaiter().GetResult();
}


[TestMethod]
public void TestGetVideo()
{
var api = new YoukuApi(loggerFactory);

Task.Run(async () =>
{
try
{
var vid = "0b39c5b6569311e5b2ad";
var result = await api.GetVideoAsync(vid, CancellationToken.None);
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}).GetAwaiter().GetResult();
}


[TestMethod]
public void TestGetDanmuContentByMat()
{
var api = new YoukuApi(loggerFactory);

Task.Run(async () =>
{
try
{
var vid = "XMTM1MTc4MDU3Ng==";
var result = await api.GetDanmuContentByMatAsync(vid, 0, CancellationToken.None);
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}).GetAwaiter().GetResult();
}

[TestMethod]
public void TestGetDanmu()
{
var api = new YoukuApi(loggerFactory);

Task.Run(async () =>
{
try
{
var vid = "XMTM1MTc4MDU3Ng==";
var result = await api.GetDanmuContentAsync(vid, CancellationToken.None);
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}).GetAwaiter().GetResult();
}

}
}
58 changes: 58 additions & 0 deletions Jellyfin.Plugin.Danmu/Core/Extensions/ElementExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// using AngleSharp.Dom;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jellyfin.Plugin.Danmu.Core.Extensions
{
public static class ElementExtension
{
// public static string? GetText(this IElement el, string css)
// {
// var node = el.QuerySelector(css);
// if (node != null)
// {
// return node.Text().Trim();
// }

// return null;
// }

// public static string GetTextOrDefault(this IElement el, string css, string defaultVal = "")
// {
// var node = el.QuerySelector(css);
// if (node != null)
// {
// return node.Text().Trim();
// }

// return defaultVal;
// }

// public static string? GetAttr(this IElement el, string css, string attr)
// {
// var node = el.QuerySelector(css);
// if (node != null)
// {
// var attrVal = node.GetAttribute(attr);
// return attrVal != null ? attrVal.Trim() : null;
// }

// return null;
// }

// public static string? GetAttrOrDefault(this IElement el, string css, string attr, string defaultVal = "")
// {
// var node = el.QuerySelector(css);
// if (node != null)
// {
// var attrVal = node.GetAttribute(attr);
// return attrVal != null ? attrVal.Trim() : defaultVal;
// }

// return defaultVal;
// }
}
}
5 changes: 4 additions & 1 deletion Jellyfin.Plugin.Danmu/Core/Extensions/JsonExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public static string ToJson(this object obj)
{
if (obj == null) return string.Empty;

return JsonSerializer.Serialize(obj);
// 不指定UnsafeRelaxedJsonEscaping,+号会被转码为unicode字符,和js/java的序列化不一致
var jso = new JsonSerializerOptions();
jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
return JsonSerializer.Serialize(obj, jso);
}
}
}
35 changes: 35 additions & 0 deletions Jellyfin.Plugin.Danmu/Core/Extensions/RegexExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Jellyfin.Plugin.Danmu.Core.Extensions
{
public static class RegexExtension
{
public static string FirstMatch(this Regex reg, string text, string defaultVal = "")
{
var match = reg.Match(text);
if (match.Success)
{
return match.Value;
}

return defaultVal;
}

public static string FirstMatchGroup(this Regex reg, string text, string defaultVal = "")
{
var match = reg.Match(text);
if (match.Success && match.Groups.Count > 1)
{
return match.Groups[1].Value.Trim();
}

return defaultVal;
}
}
}
30 changes: 30 additions & 0 deletions Jellyfin.Plugin.Danmu/Core/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,36 @@ public static float ToFloat(this string s)
return 0.0f;
}

public static double ToDouble(this string s)
{
double val;
if (double.TryParse(s, out val))
{
return val;
}

return 0.0;
}

public static string ToMD5(this string str)
{
using (var cryptoMD5 = System.Security.Cryptography.MD5.Create())
{
//將字串編碼成 UTF8 位元組陣列
var bytes = Encoding.UTF8.GetBytes(str);

//取得雜湊值位元組陣列
var hash = cryptoMD5.ComputeHash(bytes);

//取得 MD5
var md5 = BitConverter.ToString(hash)
.Replace("-", String.Empty)
.ToUpper();

return md5;
}
}

public static double Distance(this string s1, string s2)
{
var jw = new JaroWinkler();
Expand Down
84 changes: 84 additions & 0 deletions Jellyfin.Plugin.Danmu/Core/Http/HttpLoggingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

// ReSharper disable TemplateIsNotCompileTimeConstantProblem

namespace Jellyfin.Plugin.Danmu.Core.Http
{
[SuppressMessage("Usage", "CA2254:模板应为静态表达式", Justification = "<挂起>")]
public class HttpLoggingHandler : DelegatingHandler
{
private readonly ILogger _logger;

public HttpLoggingHandler(HttpMessageHandler innerHandler, ILogger logger)
: base(innerHandler)
{
_logger = logger;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage? response = null;
try
{
response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

await LogRequestAndResponse(request, response, null, cancellationToken).ConfigureAwait(false);


return response;
}
catch (Exception exception)
{
await LogRequestAndResponse(request, response, exception, cancellationToken).ConfigureAwait(false);
throw;
}
}

private async Task LogRequestAndResponse(HttpRequestMessage? request, HttpResponseMessage? response, Exception? exception, CancellationToken cancellationToken)
{
var log = new StringBuilder();
try
{
if (request != null)
{
log.Append("Request: ").Append(request).Append('\n');
}

if (request?.Content != null)
{
log.Append("Content: ").Append(await request.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)).Append('\n');
}

if (response != null)
{
log.Append("Response: ").Append(response).Append('\n');
}

if (response?.Content != null)
{
log.Append("Content: ").Append(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)).Append('\n');
}
}
catch
{
}
finally
{
if (exception == null)
{
_logger.LogWarning(log.ToString());
}
else
{
_logger.LogError(exception, log.ToString());
}
}
}
}
}
Loading

0 comments on commit db3cb61

Please sign in to comment.