From 4c04378080fe382c80529f4f4a6a2069e2c824d8 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 28 Oct 2025 19:08:59 +0100 Subject: [PATCH] [CHANGE] Added jint. Reworking decipher functionality --- .../Interpreter/JavaScriptEngineManager.cs | 58 +++++++++++++++++++ Manager.YouTube/Interpreter/PlayerEngine.cs | 42 ++++++++++++++ .../Interpreter/PlayerEngineCollection.cs | 11 ++++ Manager.YouTube/Manager.YouTube.csproj | 1 + 4 files changed, 112 insertions(+) create mode 100644 Manager.YouTube/Interpreter/JavaScriptEngineManager.cs create mode 100644 Manager.YouTube/Interpreter/PlayerEngine.cs create mode 100644 Manager.YouTube/Interpreter/PlayerEngineCollection.cs diff --git a/Manager.YouTube/Interpreter/JavaScriptEngineManager.cs b/Manager.YouTube/Interpreter/JavaScriptEngineManager.cs new file mode 100644 index 0000000..fbd7ad1 --- /dev/null +++ b/Manager.YouTube/Interpreter/JavaScriptEngineManager.cs @@ -0,0 +1,58 @@ +using System.Text; +using DotBased.Monads; + +namespace Manager.YouTube.Interpreter; + +public class JavaScriptEngineManager +{ + private readonly PlayerEngineCollection _engines = []; + + public async Task> 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> 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; + } +} \ No newline at end of file diff --git a/Manager.YouTube/Interpreter/PlayerEngine.cs b/Manager.YouTube/Interpreter/PlayerEngine.cs new file mode 100644 index 0000000..2d35a33 --- /dev/null +++ b/Manager.YouTube/Interpreter/PlayerEngine.cs @@ -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(obj => + { + var logStr = obj.ToString(); + if (string.IsNullOrEmpty(logStr)) + { + return; + } + Logger.Information(logStr); + })); + + } + + public void InitializePlayer() + { + JsEngine.Execute("createPlayer"); + } +} \ No newline at end of file diff --git a/Manager.YouTube/Interpreter/PlayerEngineCollection.cs b/Manager.YouTube/Interpreter/PlayerEngineCollection.cs new file mode 100644 index 0000000..f15ab19 --- /dev/null +++ b/Manager.YouTube/Interpreter/PlayerEngineCollection.cs @@ -0,0 +1,11 @@ +using System.Collections.ObjectModel; + +namespace Manager.YouTube.Interpreter; + +public class PlayerEngineCollection : KeyedCollection +{ + protected override string GetKeyForItem(PlayerEngine item) + { + return item.Version; + } +} \ No newline at end of file diff --git a/Manager.YouTube/Manager.YouTube.csproj b/Manager.YouTube/Manager.YouTube.csproj index 4166dec..4bd844d 100644 --- a/Manager.YouTube/Manager.YouTube.csproj +++ b/Manager.YouTube/Manager.YouTube.csproj @@ -9,6 +9,7 @@ +