[CHANGE] Rework && adding channel fetching

This commit is contained in:
max
2025-09-06 20:40:46 +02:00
parent d0eca248bb
commit c528ad9bb3
10 changed files with 145 additions and 100 deletions

View File

@@ -1,10 +1,8 @@
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.Util;
@@ -59,7 +57,7 @@ public static class NetworkService
public static async Task<Result<string[]>> GetDatasyncIds(YouTubeClient client)
{
if (client.ClientState is not { LoggedIn: true } || client.CookieContainer.Count == 0)
if (client.External.State is not { LoggedIn: true } || client.CookieContainer.Count == 0)
{
return ResultError.Fail("Client is not logged in, requires logged in client for this endpoint (/getDatasyncIdsEndpoint).");
}
@@ -98,9 +96,9 @@ public static class NetworkService
return ResultError.Fail("Failed to get datasyncIds!");
}
public static async Task<Result<AccountMenuInfo>> GetCurrentAccountInfoAsync(YouTubeClient client)
public static async Task<Result<string>> GetCurrentAccountIdAsync(YouTubeClient client)
{
if (client.ClientState is not { LoggedIn: true })
if (client.External.State is not { LoggedIn: true })
{
return ResultError.Fail("Client not logged in!");
}
@@ -115,12 +113,12 @@ public static class NetworkService
if (client.SapisidCookie != null)
{
httpRequest.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.ClientState.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId ?? "", client.SapisidCookie.Value, Origin);
httpRequest.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.External.GetDatasyncId(), client.SapisidCookie.Value, Origin);
}
var serializedContext = JsonSerializer.SerializeToNode(client.ClientState.InnerTubeContext);
var contextJson = new JsonObject { { "context", serializedContext } };
httpRequest.Content = new StringContent(contextJson.ToJsonString(), Encoding.UTF8, MediaTypeNames.Application.Json);
var serializedContext = JsonSerializer.SerializeToNode(client.External.State.InnerTubeContext);
var payload = new JsonObject { { "context", serializedContext } };
httpRequest.Content = new StringContent(payload.ToJsonString(), Encoding.UTF8, MediaTypeNames.Application.Json);
var http = client.GetHttpClient();
if (http == null)
@@ -139,41 +137,75 @@ public static class NetworkService
var json = await response.Content.ReadAsStringAsync();
var jsonDocument = JsonDocument.Parse(json);
var activeAccountHeader = jsonDocument.RootElement
var matRuns = jsonDocument.RootElement
.GetProperty("actions")[0]
.GetProperty("openPopupAction")
.GetProperty("popup")
.GetProperty("multiPageMenuRenderer")
.GetProperty("header")
.GetProperty("activeAccountHeaderRenderer");
var matRuns = activeAccountHeader
.GetProperty("activeAccountHeaderRenderer")
.GetProperty("manageAccountTitle")
.GetProperty("runs");
var accountPhotos = activeAccountHeader
.GetProperty("accountPhoto")
.GetProperty("thumbnails");
var accInfo = new AccountMenuInfo();
var highestImage = accountPhotos.EnumerateArray().OrderBy(x => x.GetProperty("width").Deserialize<int>())
.FirstOrDefault();
accInfo.ImageUrl = highestImage.GetProperty("url").Deserialize<string>();
accInfo.ImageWidth = highestImage.GetProperty("width").Deserialize<int>();
accInfo.ImageHeight = highestImage.GetProperty("height").Deserialize<int>();
foreach (var run in matRuns.EnumerateArray())
var firstElement = matRuns.EnumerateArray().FirstOrDefault();
var id = firstElement.GetProperty("navigationEndpoint").GetProperty("browseEndpoint").GetProperty("browseId").GetString();
if (string.IsNullOrWhiteSpace(id))
{
var browseEndpoint = run.GetProperty("navigationEndpoint").GetProperty("browseEndpoint");
accInfo.AccountId = browseEndpoint.GetProperty("browseId").GetString();
accInfo.AccountHandle = browseEndpoint.GetProperty("canonicalBaseUrl").GetString()?.Replace("/", "");
return ResultError.Fail("Unable to get account id!");
}
return accInfo;
return id;
}
catch (Exception e)
{
return ResultError.Error(e);
}
}
public static async Task<Result> GetChannelAsync(string channelId, YouTubeClient client)
{
if (client.External.State == null)
{
return ResultError.Fail("No client state!");
}
if (string.IsNullOrWhiteSpace(channelId))
{
return ResultError.Fail("Channel id is empty!");
}
var httpClient = client.GetHttpClient();
if (httpClient == null)
{
return ResultError.Fail("Unable to get http client!");
}
var serializedContext = JsonSerializer.SerializeToNode(client.External.State.InnerTubeContext);
var payload = new JsonObject { { "context", serializedContext }, { "browseId", channelId } };
var requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri($"{Origin}/youtubei/v1/browse?key={client.External.State.InnertubeApiKey}"),
Content = new StringContent(payload.ToJsonString(), Encoding.UTF8, MediaTypeNames.Application.Json)
};
requestMessage.Headers.UserAgent.ParseAdd(client.UserAgent);
requestMessage.Headers.Add("Origin", Origin);
if (client.SapisidCookie != null)
{
requestMessage.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.External.GetDatasyncId(), client.SapisidCookie.Value, Origin);
}
var response = await httpClient.SendAsync(requestMessage);
if (!response.IsSuccessStatusCode)
{
var responseResult = await response.Content.ReadAsStringAsync();
return ResultError.Fail(responseResult);
}
var jsonContent = await response.Content.ReadAsStringAsync();
return Result.Fail(ResultError.Fail("Not implemented!"));
}
}