[CHANGE] Update client. Reworked small things

This commit is contained in:
max
2025-10-23 21:28:08 +02:00
parent 972af513f0
commit 25589d18d8
3 changed files with 168 additions and 143 deletions

View File

@@ -8,7 +8,7 @@ public static class NetworkService
public const string Origin = "https://www.youtube.com"; public const string Origin = "https://www.youtube.com";
private static readonly HttpClient HttpClient = new(); private static readonly HttpClient HttpClient = new();
public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client, bool skipAuthenticationHeader = false) public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client, bool skipAuthenticationHeader = false, CancellationToken cancellationToken = default)
{ {
request.Headers.Add("Origin", Origin); request.Headers.Add("Origin", Origin);
request.Headers.UserAgent.ParseAdd(client.UserAgent); request.Headers.UserAgent.ParseAdd(client.UserAgent);
@@ -19,8 +19,8 @@ public static class NetworkService
try try
{ {
var response = await client.HttpClient.SendAsync(request); var response = await client.HttpClient.SendAsync(request, cancellationToken);
var contentString = await response.Content.ReadAsStringAsync(); var contentString = await response.Content.ReadAsStringAsync(cancellationToken);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
return ResultError.Fail(contentString); return ResultError.Fail(contentString);

View File

@@ -1,5 +1,6 @@
using DotBased.Logging; using DotBased.Logging;
using DotBased.Monads; using DotBased.Monads;
using Manager.YouTube.Models.Innertube;
namespace Manager.YouTube.Util.Cipher; namespace Manager.YouTube.Util.Cipher;
@@ -8,9 +9,9 @@ public static class CipherManager
private static readonly CipherDecoderCollection LoadedCiphers = []; private static readonly CipherDecoderCollection LoadedCiphers = [];
private static readonly ILogger Logger = LogService.RegisterLogger(typeof(CipherManager)); private static readonly ILogger Logger = LogService.RegisterLogger(typeof(CipherManager));
public static async Task<Result<CipherDecoder>> GetDecoderAsync(YouTubeClient client) public static async Task<Result<CipherDecoder>> GetDecoderAsync(ClientState clientState)
{ {
var relativePlayerJsUrl = client.State?.PlayerJsUrl; var relativePlayerJsUrl = clientState.PlayerJsUrl;
if (string.IsNullOrEmpty(relativePlayerJsUrl)) if (string.IsNullOrEmpty(relativePlayerJsUrl))
{ {
return ResultError.Fail("Could not get player js url."); return ResultError.Fail("Could not get player js url.");

View File

@@ -16,8 +16,8 @@ namespace Manager.YouTube;
public sealed class YouTubeClient : IDisposable public sealed class YouTubeClient : IDisposable
{ {
public string Id { get; private set; } = ""; public string Id { get; private set; } = "";
public string? UserAgent { get; set; } public string UserAgent { get; private set; }
public bool IsAnonymous { get; } public bool IsAnonymous { get; private set; }
public CookieContainer CookieContainer { get; } = new() { PerDomainCapacity = 50 }; public CookieContainer CookieContainer { get; } = new() { PerDomainCapacity = 50 };
public ClientState? State { get; private set; } public ClientState? State { get; private set; }
public List<string> DatasyncIds { get; } = []; public List<string> DatasyncIds { get; } = [];
@@ -52,7 +52,7 @@ public sealed class YouTubeClient : IDisposable
/// </summary> /// </summary>
/// <param name="cookies">The cookies to use for making requests. Empty collection or null for anonymous requests.</param> /// <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="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, if null will create a new logger.</param> /// <param name="logger">The logger that the client is going to use, null will create a new logger.</param>
/// <returns></returns> /// <returns></returns>
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection? cookies, string userAgent, ILogger? logger = null) public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection? cookies, string userAgent, ILogger? logger = null)
{ {
@@ -68,112 +68,56 @@ public sealed class YouTubeClient : IDisposable
return client; return client;
} }
private HttpClientHandler GetHttpClientHandler() public void SetUserAgent(string userAgent)
{ {
var clientHandler = new HttpClientHandler if (string.IsNullOrWhiteSpace(userAgent))
{ {
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, _logger.Warning("UserAgent cannot be null or empty!");
UseCookies = true, return;
CookieContainer = CookieContainer }
}; UserAgent = userAgent;
return clientHandler;
} }
internal async Task<Result> FetchClientDataAsync() public async Task<Result<YouTubeVideo>> GetVideoByIdAsync(string videoId, CancellationToken cancellationToken = default)
{ {
if (State is not { LoggedIn: true }) if (string.IsNullOrWhiteSpace(videoId))
{ {
var state = await GetClientStateAsync(); return ResultError.Fail("Video id is empty!");
if (!state.IsSuccess)
{
return state;
}
} }
if (string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId)) var request = new HttpRequestMessage(HttpMethod.Get, new Uri($"{NetworkService.Origin}/watch?v={videoId}"));
var response = await NetworkService.MakeRequestAsync(request, this, true, cancellationToken);
if (!response.IsSuccess && !string.IsNullOrWhiteSpace(response.Value))
{ {
var datasyncResult = await GetDatasyncIdsAsync(); return response.Error ?? ResultError.Fail("Request failed!");
if (!datasyncResult.IsSuccess)
{
return datasyncResult;
} }
foreach (var id in datasyncResult.Value) var html = response.Value;
var stateResult = GetClientStateFromHtml(html);
var state = stateResult.Value;
if (!stateResult.IsSuccess && State != null)
{ {
if (DatasyncIds.Contains(id)) state = State;
continue;
DatasyncIds.Add(id);
}
} }
if (string.IsNullOrWhiteSpace(Id))
var htmlParseResult = HtmlParser.GetVideoDataFromHtml(html);
if (!htmlParseResult.IsSuccess)
{ {
var accountInfoResult = await GetCurrentAccountIdAsync(); return htmlParseResult.Error ?? ResultError.Fail("Failed to parse HTML video data!");
if (!accountInfoResult.IsSuccess)
{
return accountInfoResult;
} }
Id = accountInfoResult.Value; var videoParseResult = VideoJsonParser.ParseVideoData(htmlParseResult.Value);
if (!videoParseResult.IsSuccess)
{
return videoParseResult;
} }
return Result.Success(); await DecipherSignatures(videoParseResult.Value, state);
}
private async Task<Result> GetClientStateAsync() return videoParseResult.Value;
{
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;
}
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 = HtmlParser.GetStateJson(html);
if (clientStateResult is { IsSuccess: false, Error: not null })
{
return clientStateResult.Error;
}
try
{
State = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value.Item1);
}
catch (Exception e)
{
return ResultError.Error(e, "Error while parsing JSON!");
}
if (State == null)
{
return ResultError.Fail("Unable to parse client state!");
}
State.IsPremiumUser = clientStateResult.Value.Item2;
return Result.Success();
} }
public async Task<Result<InnertubeChannel>> GetChannelByIdAsync(string channelId) public async Task<Result<InnertubeChannel>> GetChannelByIdAsync(string channelId)
@@ -262,6 +206,126 @@ public sealed class YouTubeClient : IDisposable
HttpClient.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;
}
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() private async Task<Result<string>> GetCurrentAccountIdAsync()
{ {
if (State is not { LoggedIn: true }) if (State is not { LoggedIn: true })
@@ -317,47 +381,7 @@ public sealed class YouTubeClient : IDisposable
return ResultError.Fail("Failed to get datasyncIds! Client not logged in."); return ResultError.Fail("Failed to get datasyncIds! Client not logged in.");
} }
public async Task<Result<YouTubeVideo>> GetVideoByIdAsync(string videoId, CancellationToken cancellationToken = default) private async Task DecipherSignatures(YouTubeVideo video, ClientState state)
{
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 response = await NetworkService.MakeRequestAsync(request, this);
if (!response.IsSuccess && !string.IsNullOrWhiteSpace(response.Value))
{
return response.Error ?? ResultError.Fail("Request failed!");
}
var html = response.Value;
var stateResult = SetClientStateFromHtml(html);
if (!stateResult.IsSuccess)
{
_logger.Warning("Failed to update client 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);
return videoParseResult.Value;
}
private async Task DecipherSignatures(YouTubeVideo video)
{ {
var streamingData = video.StreamingData; var streamingData = video.StreamingData;
if (streamingData == null) if (streamingData == null)
@@ -373,7 +397,7 @@ public sealed class YouTubeClient : IDisposable
return; return;
} }
var decipherDecoderResult = await CipherManager.GetDecoderAsync(this); var decipherDecoderResult = await CipherManager.GetDecoderAsync(state);
if (!decipherDecoderResult.IsSuccess) if (!decipherDecoderResult.IsSuccess)
{ {
_logger.Warning(decipherDecoderResult.Error?.Description ?? "Failed to get the cipher decoder!"); _logger.Warning(decipherDecoderResult.Error?.Description ?? "Failed to get the cipher decoder!");