[CHANGE] Update client. Reworked small things
This commit is contained in:
@@ -8,7 +8,7 @@ public static class NetworkService
|
||||
public const string Origin = "https://www.youtube.com";
|
||||
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.UserAgent.ParseAdd(client.UserAgent);
|
||||
@@ -19,8 +19,8 @@ public static class NetworkService
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.HttpClient.SendAsync(request);
|
||||
var contentString = await response.Content.ReadAsStringAsync();
|
||||
var response = await client.HttpClient.SendAsync(request, cancellationToken);
|
||||
var contentString = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return ResultError.Fail(contentString);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using DotBased.Logging;
|
||||
using DotBased.Monads;
|
||||
using Manager.YouTube.Models.Innertube;
|
||||
|
||||
namespace Manager.YouTube.Util.Cipher;
|
||||
|
||||
@@ -8,9 +9,9 @@ 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(YouTubeClient client)
|
||||
public static async Task<Result<CipherDecoder>> GetDecoderAsync(ClientState clientState)
|
||||
{
|
||||
var relativePlayerJsUrl = client.State?.PlayerJsUrl;
|
||||
var relativePlayerJsUrl = clientState.PlayerJsUrl;
|
||||
if (string.IsNullOrEmpty(relativePlayerJsUrl))
|
||||
{
|
||||
return ResultError.Fail("Could not get player js url.");
|
||||
|
||||
@@ -16,8 +16,8 @@ namespace Manager.YouTube;
|
||||
public sealed class YouTubeClient : IDisposable
|
||||
{
|
||||
public string Id { get; private set; } = "";
|
||||
public string? UserAgent { get; set; }
|
||||
public bool IsAnonymous { get; }
|
||||
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; } = [];
|
||||
@@ -52,7 +52,7 @@ public sealed class YouTubeClient : IDisposable
|
||||
/// </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, 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>
|
||||
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection? cookies, string userAgent, ILogger? logger = null)
|
||||
{
|
||||
@@ -68,114 +68,58 @@ public sealed class YouTubeClient : IDisposable
|
||||
return client;
|
||||
}
|
||||
|
||||
private HttpClientHandler GetHttpClientHandler()
|
||||
public void SetUserAgent(string userAgent)
|
||||
{
|
||||
var clientHandler = new HttpClientHandler
|
||||
if (string.IsNullOrWhiteSpace(userAgent))
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
||||
UseCookies = true,
|
||||
CookieContainer = CookieContainer
|
||||
};
|
||||
return clientHandler;
|
||||
_logger.Warning("UserAgent cannot be null or empty!");
|
||||
return;
|
||||
}
|
||||
UserAgent = userAgent;
|
||||
}
|
||||
|
||||
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();
|
||||
if (!state.IsSuccess)
|
||||
{
|
||||
return state;
|
||||
}
|
||||
return ResultError.Fail("Video id is empty!");
|
||||
}
|
||||
|
||||
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();
|
||||
if (!datasyncResult.IsSuccess)
|
||||
{
|
||||
return datasyncResult;
|
||||
}
|
||||
|
||||
foreach (var id in datasyncResult.Value)
|
||||
{
|
||||
if (DatasyncIds.Contains(id))
|
||||
continue;
|
||||
DatasyncIds.Add(id);
|
||||
}
|
||||
return response.Error ?? ResultError.Fail("Request failed!");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Id))
|
||||
var html = response.Value;
|
||||
|
||||
var stateResult = GetClientStateFromHtml(html);
|
||||
var state = stateResult.Value;
|
||||
if (!stateResult.IsSuccess && State != null)
|
||||
{
|
||||
var accountInfoResult = await GetCurrentAccountIdAsync();
|
||||
if (!accountInfoResult.IsSuccess)
|
||||
{
|
||||
return accountInfoResult;
|
||||
}
|
||||
|
||||
Id = accountInfoResult.Value;
|
||||
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 Result.Success();
|
||||
return videoParseResult.Value;
|
||||
}
|
||||
|
||||
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 = 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)
|
||||
{
|
||||
if (State == null)
|
||||
@@ -256,11 +200,131 @@ public sealed class YouTubeClient : IDisposable
|
||||
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;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
@@ -317,47 +381,7 @@ public sealed class YouTubeClient : IDisposable
|
||||
return ResultError.Fail("Failed to get datasyncIds! Client not logged in.");
|
||||
}
|
||||
|
||||
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 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)
|
||||
private async Task DecipherSignatures(YouTubeVideo video, ClientState state)
|
||||
{
|
||||
var streamingData = video.StreamingData;
|
||||
if (streamingData == null)
|
||||
@@ -373,7 +397,7 @@ public sealed class YouTubeClient : IDisposable
|
||||
return;
|
||||
}
|
||||
|
||||
var decipherDecoderResult = await CipherManager.GetDecoderAsync(this);
|
||||
var decipherDecoderResult = await CipherManager.GetDecoderAsync(state);
|
||||
if (!decipherDecoderResult.IsSuccess)
|
||||
{
|
||||
_logger.Warning(decipherDecoderResult.Error?.Description ?? "Failed to get the cipher decoder!");
|
||||
|
||||
Reference in New Issue
Block a user