418 lines
14 KiB
C#
418 lines
14 KiB
C#
using System.Net;
|
|
using System.Net.Mime;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using DotBased.Logging;
|
|
using DotBased.Monads;
|
|
using Manager.YouTube.Models;
|
|
using Manager.YouTube.Models.Innertube;
|
|
using Manager.YouTube.Parsers;
|
|
using Manager.YouTube.Parsers.Json;
|
|
using Manager.YouTube.Util.Cipher;
|
|
|
|
namespace Manager.YouTube;
|
|
|
|
public sealed class YouTubeClient : IDisposable
|
|
{
|
|
public string Id { get; private set; } = "";
|
|
public string UserAgent { get; private set; }
|
|
public bool IsAnonymous { get; private set; }
|
|
public CookieContainer CookieContainer { get; } = new() { PerDomainCapacity = 50 };
|
|
public ClientState? State { get; private set; }
|
|
public List<string> DatasyncIds { get; } = [];
|
|
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"];
|
|
public HttpClient HttpClient { get; }
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private YouTubeClient(CookieCollection? cookies, string userAgent, ILogger logger)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(userAgent))
|
|
{
|
|
throw new ArgumentNullException(nameof(userAgent));
|
|
}
|
|
_logger = logger;
|
|
UserAgent = userAgent;
|
|
if (cookies == null || cookies.Count == 0)
|
|
{
|
|
Id = $"anon_{Guid.NewGuid()}";
|
|
IsAnonymous = true;
|
|
}
|
|
else
|
|
{
|
|
CookieContainer.Add(cookies);
|
|
}
|
|
|
|
HttpClient = new HttpClient(GetHttpClientHandler());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads the given cookies and fetch client state.
|
|
/// </summary>
|
|
/// <param name="cookies">The cookies to use for making requests. Empty collection or null for anonymous requests.</param>
|
|
/// <param name="userAgent">The user agent to use for the requests. Only WEB client is supported.</param>
|
|
/// <param name="logger">The logger that the client is going to use, null will create a new logger.</param>
|
|
/// <returns></returns>
|
|
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection? cookies, string userAgent, ILogger? logger = null)
|
|
{
|
|
logger ??= LogService.RegisterLogger<YouTubeClient>();
|
|
|
|
var client = new YouTubeClient(cookies, userAgent, logger);
|
|
var clientInitializeResult = await client.FetchClientDataAsync();
|
|
if (!clientInitializeResult.IsSuccess)
|
|
{
|
|
return clientInitializeResult.Error ?? ResultError.Fail("Failed to initialize YouTube client!");
|
|
}
|
|
|
|
return client;
|
|
}
|
|
|
|
public void SetUserAgent(string userAgent)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(userAgent))
|
|
{
|
|
_logger.Warning("UserAgent cannot be null or empty!");
|
|
return;
|
|
}
|
|
UserAgent = userAgent;
|
|
}
|
|
|
|
public async Task<Result<YouTubeVideo>> GetVideoByIdAsync(string videoId, CancellationToken cancellationToken = default)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(videoId))
|
|
{
|
|
return ResultError.Fail("Video id is empty!");
|
|
}
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, new Uri($"{NetworkService.Origin}/watch?v={videoId}"));
|
|
|
|
var videoResponse = await NetworkService.MakeRequestAsync(request, this, true, cancellationToken);
|
|
if (!videoResponse.IsSuccess && !string.IsNullOrWhiteSpace(videoResponse.Value))
|
|
{
|
|
return videoResponse.Error ?? ResultError.Fail("Request failed!");
|
|
}
|
|
|
|
var html = videoResponse.Value;
|
|
|
|
var stateResult = GetClientStateFromHtml(html);
|
|
var state = stateResult.Value;
|
|
if (!stateResult.IsSuccess && State != null)
|
|
{
|
|
state = State;
|
|
}
|
|
|
|
var htmlParseResult = HtmlParser.GetVideoDataFromHtml(html);
|
|
if (!htmlParseResult.IsSuccess)
|
|
{
|
|
return htmlParseResult.Error ?? ResultError.Fail("Failed to parse HTML video data!");
|
|
}
|
|
|
|
var videoParseResult = VideoJsonParser.ParseVideoData(htmlParseResult.Value);
|
|
if (!videoParseResult.IsSuccess)
|
|
{
|
|
return videoParseResult;
|
|
}
|
|
|
|
await DecipherSignatures(videoParseResult.Value, state);
|
|
|
|
return videoParseResult.Value;
|
|
}
|
|
|
|
public async Task<Result<InnertubeChannel>> GetChannelByIdAsync(string channelId)
|
|
{
|
|
if (State == null)
|
|
{
|
|
return ResultError.Fail("No client state!");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(channelId))
|
|
{
|
|
return ResultError.Fail("Channel id is empty!");
|
|
}
|
|
|
|
var serializedContext = JsonSerializer.SerializeToNode(State.InnerTubeContext);
|
|
var payload = new JsonObject { { "context", serializedContext }, { "browseId", channelId } };
|
|
var requestMessage = new HttpRequestMessage
|
|
{
|
|
Method = HttpMethod.Post,
|
|
RequestUri = new Uri($"{NetworkService.Origin}/youtubei/v1/browse?key={State.InnertubeApiKey}"),
|
|
Content = new StringContent(payload.ToJsonString(), Encoding.UTF8, MediaTypeNames.Application.Json)
|
|
};
|
|
var responseResult = await NetworkService.MakeRequestAsync(requestMessage, this);
|
|
if (!responseResult.IsSuccess)
|
|
{
|
|
return responseResult.Error ?? ResultError.Fail("Request failed!");
|
|
}
|
|
|
|
return ChannelJsonParser.ParseJsonToChannelData(responseResult.Value);
|
|
}
|
|
|
|
public string GetDatasyncId()
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
|
{
|
|
return State.WebPlayerContextConfig.WebPlayerContext.DatasyncId;
|
|
}
|
|
|
|
var tempDatasyncId = "";
|
|
foreach (var datasyncId in DatasyncIds)
|
|
{
|
|
var split = datasyncId.Split("||", StringSplitOptions.RemoveEmptyEntries);
|
|
switch (split.Length)
|
|
{
|
|
case 0:
|
|
case 2 when tempDatasyncId.Equals(split[1]):
|
|
continue;
|
|
case 2:
|
|
tempDatasyncId = split[1];
|
|
break;
|
|
}
|
|
}
|
|
|
|
return tempDatasyncId;
|
|
}
|
|
|
|
public async Task<Result> RotateCookiesPageAsync(string origin = NetworkService.Origin, int ytPid = 1)
|
|
{
|
|
/*if (IsAnonymous)
|
|
{
|
|
return ResultError.Fail("Anonymous clients cannot rotate cookies!");
|
|
}*/
|
|
|
|
if (string.IsNullOrWhiteSpace(origin))
|
|
{
|
|
return ResultError.Fail("Origin is empty!");
|
|
}
|
|
|
|
var rotatePageCookiesRequest = new HttpRequestMessage(HttpMethod.Get, new Uri($"https://accounts.youtube.com/RotateCookiesPage?origin={origin}&yt_pid={ytPid}"));
|
|
return await NetworkService.MakeRequestAsync(rotatePageCookiesRequest, this, true);
|
|
}
|
|
|
|
public async Task<Result> RotateCookiesAsync()
|
|
{
|
|
if (IsAnonymous)
|
|
{
|
|
return ResultError.Fail("Anonymous clients cannot rotate cookies!");
|
|
}
|
|
|
|
var rotateRequest = new HttpRequestMessage(HttpMethod.Post, new Uri("https://accounts.youtube.com/RotateCookies"));
|
|
return await NetworkService.MakeRequestAsync(rotateRequest, this, true);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
HttpClient.Dispose();
|
|
}
|
|
|
|
private async Task<Result> FetchClientDataAsync()
|
|
{
|
|
if (State is not { LoggedIn: true })
|
|
{
|
|
var stateResult = await GetClientStateAsync();
|
|
if (!stateResult.IsSuccess)
|
|
{
|
|
return stateResult;
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
|
{
|
|
var datasyncResult = await GetDatasyncIdsAsync();
|
|
if (!datasyncResult.IsSuccess)
|
|
{
|
|
return datasyncResult;
|
|
}
|
|
|
|
foreach (var id in datasyncResult.Value)
|
|
{
|
|
if (DatasyncIds.Contains(id))
|
|
continue;
|
|
DatasyncIds.Add(id);
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(Id))
|
|
{
|
|
var accountInfoResult = await GetCurrentAccountIdAsync();
|
|
if (!accountInfoResult.IsSuccess)
|
|
{
|
|
return accountInfoResult;
|
|
}
|
|
|
|
Id = accountInfoResult.Value;
|
|
}
|
|
|
|
return Result.Success();
|
|
}
|
|
|
|
private HttpClientHandler GetHttpClientHandler()
|
|
{
|
|
var clientHandler = new HttpClientHandler
|
|
{
|
|
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
|
UseCookies = true,
|
|
CookieContainer = CookieContainer
|
|
};
|
|
return clientHandler;
|
|
}
|
|
|
|
private async Task<Result> GetClientStateAsync()
|
|
{
|
|
var httpRequest = new HttpRequestMessage
|
|
{
|
|
Method = HttpMethod.Get,
|
|
RequestUri = new Uri(NetworkService.Origin)
|
|
};
|
|
|
|
var result = await NetworkService.MakeRequestAsync(httpRequest, this, true);
|
|
if (!result.IsSuccess)
|
|
{
|
|
return result.Error ?? ResultError.Fail("Request failed!");
|
|
}
|
|
|
|
var stateResult = SetClientStateFromHtml(result.Value);
|
|
if (!stateResult.IsSuccess)
|
|
{
|
|
return stateResult;
|
|
}
|
|
|
|
if (State is { LoggedIn: false })
|
|
{
|
|
_logger.Warning("Client is not logged in!");
|
|
return ResultError.Fail("Client login failed!");
|
|
}
|
|
|
|
var cookieRotationResult = await RotateCookiesPageAsync();
|
|
return !cookieRotationResult.IsSuccess ? cookieRotationResult : Result.Success();
|
|
}
|
|
|
|
private Result SetClientStateFromHtml(string html)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(html))
|
|
{
|
|
return ResultError.Fail("HTML is empty!!");
|
|
}
|
|
|
|
var clientStateResult = GetClientStateFromHtml(html);
|
|
if (clientStateResult is { IsSuccess: false, Error: not null })
|
|
{
|
|
return clientStateResult.Error;
|
|
}
|
|
|
|
State = clientStateResult.Value;
|
|
IsAnonymous = !State.LoggedIn;
|
|
|
|
return Result.Success();
|
|
}
|
|
|
|
private Result<ClientState> GetClientStateFromHtml(string html)
|
|
{
|
|
var clientStateResult = HtmlParser.GetStateJson(html);
|
|
if (clientStateResult is { IsSuccess: false, Error: not null })
|
|
{
|
|
return clientStateResult.Error;
|
|
}
|
|
|
|
ClientState? state;
|
|
try
|
|
{
|
|
state = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value.Item1);
|
|
if (state != null)
|
|
{
|
|
state.IsPremiumUser = clientStateResult.Value.Item2;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return ResultError.Error(e, "Error while parsing JSON!");
|
|
}
|
|
|
|
return state == null ? ResultError.Fail("Unable to parse client state!") : state;
|
|
}
|
|
|
|
private async Task<Result<string>> GetCurrentAccountIdAsync()
|
|
{
|
|
if (State is not { LoggedIn: true })
|
|
{
|
|
return ResultError.Fail("Client not logged in!");
|
|
}
|
|
|
|
var httpRequest = new HttpRequestMessage
|
|
{
|
|
Method = HttpMethod.Post,
|
|
RequestUri = new Uri($"{NetworkService.Origin}/youtubei/v1/account/account_menu")
|
|
};
|
|
var serializedContext = JsonSerializer.SerializeToNode(State.InnerTubeContext);
|
|
var payload = new JsonObject { { "context", serializedContext } };
|
|
httpRequest.Content = new StringContent(payload.ToJsonString(), Encoding.UTF8, MediaTypeNames.Application.Json);
|
|
var responseResult = await NetworkService.MakeRequestAsync(httpRequest, this);
|
|
|
|
if (!responseResult.IsSuccess)
|
|
{
|
|
return responseResult.Error ?? ResultError.Fail("Request failed!");
|
|
}
|
|
|
|
return JsonAccountParser.ParseAccountId(responseResult.Value);
|
|
}
|
|
|
|
private async Task<Result<string[]>> GetDatasyncIdsAsync()
|
|
{
|
|
if (State is not { LoggedIn: true } || CookieContainer.Count == 0)
|
|
{
|
|
return ResultError.Fail("Client is not logged in, requires logged in client for this endpoint (/getDatasyncIdsEndpoint).");
|
|
}
|
|
|
|
var httpRequest = new HttpRequestMessage
|
|
{
|
|
Method = HttpMethod.Get,
|
|
RequestUri = new Uri($"{NetworkService.Origin}/getDatasyncIdsEndpoint")
|
|
};
|
|
var responseResult = await NetworkService.MakeRequestAsync(httpRequest, this);
|
|
if (!responseResult.IsSuccess)
|
|
{
|
|
return responseResult.Error ?? ResultError.Fail("Request failed!");
|
|
}
|
|
|
|
var datasyncIdsJson = JsonNode.Parse(responseResult.Value.Replace(")]}'", ""));
|
|
|
|
var isLoggedOut = datasyncIdsJson?["responseContext"]?["mainAppWebResponseContext"]?["loggedOut"]
|
|
.Deserialize<bool>() ?? true;
|
|
if (!isLoggedOut)
|
|
{
|
|
return datasyncIdsJson?["datasyncIds"].Deserialize<string[]>() ?? [];
|
|
}
|
|
|
|
return ResultError.Fail("Failed to get datasyncIds! Client not logged in.");
|
|
}
|
|
|
|
private async Task DecipherSignatures(YouTubeVideo video, ClientState state)
|
|
{
|
|
var streamingData = video.StreamingData;
|
|
if (streamingData == null)
|
|
{
|
|
_logger.Debug("No streaming data available, skipping decipher.");
|
|
return;
|
|
}
|
|
|
|
var formatsWithCipher = streamingData.Formats.Concat(streamingData.AdaptiveFormats).Where(x => !string.IsNullOrWhiteSpace(x.SignatureCipher)).ToList();
|
|
if (formatsWithCipher.Count == 0)
|
|
{
|
|
_logger.Debug("Skipping decipher, no signatures found to decipher.");
|
|
return;
|
|
}
|
|
|
|
var decipherDecoderResult = await CipherManager.GetDecoderAsync(state, this);
|
|
if (!decipherDecoderResult.IsSuccess)
|
|
{
|
|
_logger.Warning(decipherDecoderResult.Error?.Description ?? "Failed to get the cipher decoder!");
|
|
return;
|
|
}
|
|
var decoder = decipherDecoderResult.Value;
|
|
|
|
foreach (var format in formatsWithCipher)
|
|
{
|
|
format.Url = decoder.Decipher(format.SignatureCipher);
|
|
}
|
|
}
|
|
} |