[CHANGE] Finished impl required data for client
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
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;
|
||||
|
||||
@@ -38,7 +45,7 @@ public sealed class YouTubeClient : IDisposable
|
||||
{
|
||||
if (External.State is not { LoggedIn: true })
|
||||
{
|
||||
var state = await NetworkService.GetClientStateAsync(this);
|
||||
var state = await GetClientStateAsync();
|
||||
if (!state.IsSuccess)
|
||||
{
|
||||
return state;
|
||||
@@ -48,7 +55,7 @@ public sealed class YouTubeClient : IDisposable
|
||||
|
||||
if (string.IsNullOrWhiteSpace(External.State.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
||||
{
|
||||
var datasyncResult = await NetworkService.GetDatasyncIds(this);
|
||||
var datasyncResult = await GetDatasyncIds();
|
||||
if (!datasyncResult.IsSuccess)
|
||||
{
|
||||
return datasyncResult;
|
||||
@@ -64,7 +71,7 @@ public sealed class YouTubeClient : IDisposable
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Id))
|
||||
{
|
||||
var accountInfoResult = await NetworkService.GetCurrentAccountIdAsync(this);
|
||||
var accountInfoResult = await GetCurrentAccountIdAsync();
|
||||
if (!accountInfoResult.IsSuccess)
|
||||
{
|
||||
return accountInfoResult;
|
||||
@@ -73,13 +80,143 @@ public sealed class YouTubeClient : IDisposable
|
||||
Id = accountInfoResult.Value;
|
||||
}
|
||||
|
||||
var accountInfo = await NetworkService.GetChannelAsync(Id, this);
|
||||
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.");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user