46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using DotBased.Logging;
|
|
using DotBased.Monads;
|
|
|
|
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>> GetDecoder(YouTubeClient client)
|
|
{
|
|
var relativePlayerJsUrl = client.State?.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);
|
|
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('/');
|
|
var v = split[2];
|
|
var lang = split[4];
|
|
return $"{v}_{lang}";
|
|
}
|
|
} |