[CHANGE] Added jint. Reworking decipher functionality
This commit is contained in:
58
Manager.YouTube/Interpreter/JavaScriptEngineManager.cs
Normal file
58
Manager.YouTube/Interpreter/JavaScriptEngineManager.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
42
Manager.YouTube/Interpreter/PlayerEngine.cs
Normal file
42
Manager.YouTube/Interpreter/PlayerEngine.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
11
Manager.YouTube/Interpreter/PlayerEngineCollection.cs
Normal file
11
Manager.YouTube/Interpreter/PlayerEngineCollection.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user