222 lines
7.2 KiB
C#
222 lines
7.2 KiB
C#
using System.Net;
|
|
using System.Net.Mime;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using DotBased.Monads;
|
|
using Manager.YouTube.Models;
|
|
using Manager.YouTube.Models.Innertube;
|
|
using Manager.YouTube.Parsers;
|
|
using Manager.YouTube.Parsers.Json;
|
|
|
|
namespace Manager.YouTube;
|
|
|
|
public sealed class YouTubeClient : IDisposable
|
|
{
|
|
public string Id { get; private set; } = "";
|
|
public string? UserAgent { get; set; }
|
|
public CookieContainer CookieContainer { get; } = new() { PerDomainCapacity = 50 };
|
|
public ClientExternalData External { get; set; } = new();
|
|
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"];
|
|
public HttpClient? GetHttpClient() => _httpClient;
|
|
|
|
private HttpClient? _httpClient;
|
|
|
|
public YouTubeClient()
|
|
{
|
|
SetupClient();
|
|
}
|
|
|
|
private void SetupClient()
|
|
{
|
|
_httpClient?.Dispose();
|
|
|
|
var clientHandler = new HttpClientHandler
|
|
{
|
|
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
|
UseCookies = true,
|
|
CookieContainer = CookieContainer
|
|
};
|
|
_httpClient = new HttpClient(clientHandler);
|
|
_httpClient.DefaultRequestHeaders.Clear();
|
|
}
|
|
|
|
public async Task<Result> BuildClientAsync()
|
|
{
|
|
if (External.State is not { LoggedIn: true })
|
|
{
|
|
var state = await GetClientStateAsync();
|
|
if (!state.IsSuccess)
|
|
{
|
|
return state;
|
|
}
|
|
External.State = state.Value;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(External.State.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
|
{
|
|
var datasyncResult = await GetDatasyncIds();
|
|
if (!datasyncResult.IsSuccess)
|
|
{
|
|
return datasyncResult;
|
|
}
|
|
|
|
foreach (var id in datasyncResult.Value)
|
|
{
|
|
if (External.DatasyncIds.Contains(id))
|
|
continue;
|
|
External.DatasyncIds.Add(id);
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(Id))
|
|
{
|
|
var accountInfoResult = await GetCurrentAccountIdAsync();
|
|
if (!accountInfoResult.IsSuccess)
|
|
{
|
|
return accountInfoResult;
|
|
}
|
|
|
|
Id = accountInfoResult.Value;
|
|
}
|
|
|
|
var channelResult = await GetChannelByIdAsync(Id);
|
|
if (!channelResult.IsSuccess)
|
|
{
|
|
return channelResult.Error ?? ResultError.Fail("Failed to get channel.");
|
|
}
|
|
External.Channel = channelResult.Value;
|
|
|
|
return Result.Success();
|
|
}
|
|
|
|
public async Task<Result<ClientState>> GetClientStateAsync()
|
|
{
|
|
var httpRequest = new HttpRequestMessage
|
|
{
|
|
Method = HttpMethod.Get,
|
|
RequestUri = new Uri(NetworkService.Origin)
|
|
};
|
|
|
|
var result = await NetworkService.MakeRequestAsync(httpRequest, this);
|
|
if (!result.IsSuccess)
|
|
{
|
|
return result.Error ?? ResultError.Fail("Request failed!");
|
|
}
|
|
|
|
var clientStateResult = HtmlParser.GetStateJson(result.Value);
|
|
if (clientStateResult is { IsSuccess: false, Error: not null })
|
|
{
|
|
return clientStateResult.Error;
|
|
}
|
|
|
|
ClientState? clientState;
|
|
try
|
|
{
|
|
clientState = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value.Item1);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return ResultError.Error(e, "Error while parsing JSON!");
|
|
}
|
|
|
|
|
|
if (clientState == null)
|
|
{
|
|
return ResultError.Fail("Unable to parse client state!");
|
|
}
|
|
|
|
clientState.IsPremiumUser = clientStateResult.Value.Item2;
|
|
|
|
return clientState;
|
|
}
|
|
|
|
public async Task<Result<Channel>> GetChannelByIdAsync(string channelId)
|
|
{
|
|
if (External.State == null)
|
|
{
|
|
return ResultError.Fail("No client state!");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(channelId))
|
|
{
|
|
return ResultError.Fail("Channel id is empty!");
|
|
}
|
|
|
|
var serializedContext = JsonSerializer.SerializeToNode(External.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={External.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 void Dispose()
|
|
{
|
|
_httpClient?.Dispose();
|
|
}
|
|
|
|
private async Task<Result<string>> GetCurrentAccountIdAsync()
|
|
{
|
|
if (External.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(External.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[]>> GetDatasyncIds()
|
|
{
|
|
if (External.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.");
|
|
}
|
|
} |