75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using System.Text.Json;
|
|
using DotBased.Monads;
|
|
using Manager.YouTube.Models.Innertube;
|
|
using Manager.YouTube.Parsers;
|
|
|
|
namespace Manager.YouTube;
|
|
|
|
public static class NetworkService
|
|
{
|
|
private const string Origin = "https://www.youtube.com/";
|
|
|
|
public static async Task<Result<ClientState>> GetClientStateAsync(YouTubeClient client)
|
|
{
|
|
var httpRequest = new HttpRequestMessage
|
|
{
|
|
Method = HttpMethod.Get,
|
|
RequestUri = new Uri(Origin)
|
|
};
|
|
httpRequest.Headers.IfModifiedSince = new DateTimeOffset(DateTime.UtcNow);
|
|
httpRequest.Headers.UserAgent.ParseAdd(client.UserAgent);
|
|
httpRequest.Headers.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
|
httpRequest.Headers.Connection.Add("keep-alive");
|
|
httpRequest.Headers.Add("DNT", "1");
|
|
httpRequest.Headers.Add("Upgrade-Insecure-Requests", "1");
|
|
|
|
var http = client.GetHttpClient();
|
|
if (http == null)
|
|
{
|
|
return ResultError.Fail("Unable to get http client!");
|
|
}
|
|
|
|
var response = await http.SendAsync(httpRequest);
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var responseResult = await response.Content.ReadAsStringAsync();
|
|
return Result<ClientState>.Fail(ResultError.Fail(responseResult));
|
|
}
|
|
var responseHtml = await response.Content.ReadAsStringAsync();
|
|
var clientStateResult = HtmlParser.GetStateJson(responseHtml);
|
|
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!");
|
|
}
|
|
|
|
return clientState == null ? ResultError.Fail("Unable to parse client state!") : clientState;
|
|
}
|
|
|
|
public static async Task<Result> GetCurrentAccountAsync()
|
|
{
|
|
//URL: /youtubei/v1/account/account_menu
|
|
// Payload
|
|
// "context": {
|
|
// "client": {CLIENT INFO FROM STATE}
|
|
// }
|
|
|
|
/* Auth header
|
|
* if (client.SapisidCookie != null)
|
|
{
|
|
httpRequest.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.SapisidCookie.Value, origin);
|
|
httpRequest.Headers.Add("Origin", origin);
|
|
}
|
|
*/
|
|
return ResultError.Fail("Not implemented");
|
|
}
|
|
} |