54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
using System.Net;
|
|
using DotBased.Logging;
|
|
using Manager.YouTube.Models.Innertube;
|
|
|
|
namespace Manager.YouTube;
|
|
|
|
public sealed class YouTubeClient
|
|
{
|
|
public string Id { get; private set; }
|
|
public string AccountName { get; private set; }
|
|
public string? UserAgent { get; private set; }
|
|
public CookieContainer CookieContainer { get; }
|
|
public ClientState? ClientState { get; private set; }
|
|
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"];
|
|
public HttpClient? GetHttpClient() => _httpClient;
|
|
|
|
private readonly ILogger? _logger;
|
|
private HttpClient? _httpClient;
|
|
|
|
public YouTubeClient(CookieContainer cookieContainer, string userAgent, ILogger? logger = null)
|
|
{
|
|
CookieContainer = cookieContainer;
|
|
_logger = logger;
|
|
UserAgent = userAgent;
|
|
SetupClient();
|
|
}
|
|
|
|
private void SetupClient()
|
|
{
|
|
_logger?.Information("Building http client...");
|
|
_httpClient?.Dispose();
|
|
|
|
var clientHandler = new HttpClientHandler
|
|
{
|
|
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
|
UseCookies = true,
|
|
CookieContainer = CookieContainer
|
|
};
|
|
_httpClient = new HttpClient(clientHandler);
|
|
}
|
|
|
|
private async Task GetStateAsync()
|
|
{
|
|
var state = await NetworkService.GetClientStateAsync(this);
|
|
if (!state.IsSuccess)
|
|
{
|
|
_logger?.Warning($"Error getting client state: {state.Error}");
|
|
return;
|
|
}
|
|
|
|
ClientState = state.Value;
|
|
_logger?.Information("Client state retrieved. With API key: {InnertubeApiKey}", ClientState.InnertubeApiKey);
|
|
}
|
|
} |