[CHANGE] Added jint. Reworking decipher functionality

This commit is contained in:
max
2025-10-28 19:08:59 +01:00
parent b5c701b971
commit 4c04378080
4 changed files with 112 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
using System.Text;
using DotBased.Monads;
namespace Manager.YouTube.Interpreter;
public class JavaScriptEngineManager
{
private readonly PlayerEngineCollection _engines = [];
public async Task<Result<PlayerEngine>> GetPlayerEngine(string playerUrl)
{
if (string.IsNullOrEmpty(playerUrl))
{
return ResultError.Fail("player url is empty or null!");
}
var version = GetScriptVersion(playerUrl);
if (_engines.TryGetValue(version, out var engine))
{
return engine;
}
var playerJsSourceResult = await DownloadPlayerScriptAsync(playerUrl);
if (!playerJsSourceResult.IsSuccess)
{
return playerJsSourceResult.Error ?? ResultError.Fail("Download player script failed!");
}
return new PlayerEngine(version, playerJsSourceResult.Value);
}
private static string GetScriptVersion(string relativePlayerUrl)
{
var split = relativePlayerUrl.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var v = split[2];
var lang = split[4];
return $"{v}-{lang}";
}
private static async Task<Result<string>> DownloadPlayerScriptAsync(string relativeUrl, YouTubeClient? client = null)
{
var downloadRequest = new HttpRequestMessage(HttpMethod.Get, new Uri($"{NetworkService.Origin}/{relativeUrl}"));
var downloadResponse = await NetworkService.DownloadBytesAsync(downloadRequest, client);
if (!downloadResponse.IsSuccess)
{
return downloadResponse.Error ?? ResultError.Fail($"Failed to download script from url: {relativeUrl}");
}
var playerJs = Encoding.UTF8.GetString(downloadResponse.Value.Data);
if (string.IsNullOrWhiteSpace(playerJs))
{
return ResultError.Fail("Script value is empty!");
}
return playerJs;
}
}

View File

@@ -0,0 +1,42 @@
using DotBased.Logging;
using Jint;
namespace Manager.YouTube.Interpreter;
public class PlayerEngine
{
public string Version { get; set; }
public Engine JsEngine { get; set; }
private ILogger Logger { get; set; }
public PlayerEngine(string version, string script)
{
if (string.IsNullOrEmpty(version))
{
throw new ArgumentNullException(nameof(version));
}
if (string.IsNullOrEmpty(script))
{
throw new ArgumentNullException(nameof(script));
}
Logger = LogService.RegisterLogger(typeof(PlayerEngine), version);
Version = version;
JsEngine = new Engine().Execute(script).SetValue("log", new Action<object>(obj =>
{
var logStr = obj.ToString();
if (string.IsNullOrEmpty(logStr))
{
return;
}
Logger.Information(logStr);
}));
}
public void InitializePlayer()
{
JsEngine.Execute("createPlayer");
}
}

View File

@@ -0,0 +1,11 @@
using System.Collections.ObjectModel;
namespace Manager.YouTube.Interpreter;
public class PlayerEngineCollection : KeyedCollection<string, PlayerEngine>
{
protected override string GetKeyForItem(PlayerEngine item)
{
return item.Version;
}
}

View File

@@ -9,6 +9,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DotBased" Version="1.0.0" /> <PackageReference Include="DotBased" Version="1.0.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.12.2" /> <PackageReference Include="HtmlAgilityPack" Version="1.12.2" />
<PackageReference Include="Jint" Version="4.4.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>