47 lines
1.6 KiB
C#
47 lines
1.6 KiB
C#
using DotBased.Logging;
|
|
using DotBased.Monads;
|
|
using Manager.YouTube.Models.Innertube;
|
|
|
|
namespace Manager.YouTube.Util.Cipher;
|
|
|
|
public static class CipherManager
|
|
{
|
|
private static readonly CipherDecoderCollection LoadedCiphers = [];
|
|
private static readonly ILogger Logger = LogService.RegisterLogger(typeof(CipherManager));
|
|
|
|
public static async Task<Result<CipherDecoder>> GetDecoderAsync(ClientState clientState, YouTubeClient? client = null)
|
|
{
|
|
var relativePlayerJsUrl = clientState.PlayerJsUrl;
|
|
if (string.IsNullOrEmpty(relativePlayerJsUrl))
|
|
{
|
|
return ResultError.Fail("Could not get player js url.");
|
|
}
|
|
var version = GetCipherVersion(relativePlayerJsUrl);
|
|
|
|
Logger.Debug($"Getting cipher decoder for version: {version}");
|
|
if (LoadedCiphers.TryGetValue(version, out var cipher))
|
|
{
|
|
return cipher;
|
|
}
|
|
|
|
try
|
|
{
|
|
var decoder = await CipherDecoder.CreateAsync(relativePlayerJsUrl, version, client);
|
|
LoadedCiphers.Add(decoder);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.Error(e, "Could not create cipher decoder. Version: {DecoderVersion}", version);
|
|
}
|
|
|
|
return ResultError.Fail($"Could not create cipher decoder for {relativePlayerJsUrl} (v: {version})");
|
|
}
|
|
|
|
private static string GetCipherVersion(string relativePlayerUrl)
|
|
{
|
|
var split = relativePlayerUrl.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
|
var v = split[2];
|
|
var lang = split[4];
|
|
return $"{v}_{lang}";
|
|
}
|
|
} |