[CHANGE] Finished impl required data for client
This commit is contained in:
@@ -20,11 +20,11 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Account name:</td>
|
<td>Account name:</td>
|
||||||
<td>@Client.External.Information.AccountName</td>
|
<td>@Client.External.Channel?.ChannelName</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Account handle:</td>
|
<td>Account handle:</td>
|
||||||
<td>@Client.External.Information.AccountHandle</td>
|
<td>@Client.External.Channel?.Handle</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Logged in:</td>
|
<td>Logged in:</td>
|
||||||
@@ -32,17 +32,13 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>YouTube Premium:</td>
|
<td>YouTube Premium:</td>
|
||||||
<td style="@($"color: {(Client.External.Information.IsPremiumUser ? "green" : "red")}")">@Client.External.Information.IsPremiumUser</td>
|
<td style="@($"color: {(Client.External.State?.IsPremiumUser ?? false ? "green" : "red")}")">@Client.External.State?.IsPremiumUser</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>User agent:</td>
|
<td>User agent:</td>
|
||||||
<td>@Client.UserAgent</td>
|
<td>@Client.UserAgent</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
|
||||||
<td>InnerTube API key:</td>
|
|
||||||
<td>@Client.External.State?.InnertubeApiKey</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>InnerTube client:</td>
|
<td>InnerTube client:</td>
|
||||||
<td>@Client.External.State?.InnerTubeClient</td>
|
<td>@Client.External.State?.InnerTubeClient</td>
|
||||||
@@ -51,16 +47,23 @@
|
|||||||
<td>InnerTube client version:</td>
|
<td>InnerTube client version:</td>
|
||||||
<td>@Client.External.State?.InnerTubeClientVersion</td>
|
<td>@Client.External.State?.InnerTubeClientVersion</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>InnerTube API key:</td>
|
||||||
|
<td>@Client.External.State?.InnertubeApiKey</td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Language:</td>
|
<td>Language:</td>
|
||||||
<td>@Client.External.State?.InnerTubeContext?.InnerTubeClient?.HLanguage</td>
|
<td>@Client.External.State?.InnerTubeContext?.InnerTubeClient?.HLanguage</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</MudSimpleTable>
|
</MudSimpleTable>
|
||||||
@*@if (!string.IsNullOrWhiteSpace(Client.AccountImage))
|
@{
|
||||||
|
var avatar = Client.External.Channel?.AvatarImages.FirstOrDefault();
|
||||||
|
}
|
||||||
|
@if (avatar != null)
|
||||||
{
|
{
|
||||||
<MudImage Src="@Client.AccountImage" Elevation="0" ObjectFit="ObjectFit.Contain"/>
|
<MudImage Src="@avatar.Url" Elevation="0" ObjectFit="ObjectFit.ScaleDown" Width="75"/>
|
||||||
}*@
|
}
|
||||||
</MudStack>
|
</MudStack>
|
||||||
|
|
||||||
<MudPaper Elevation="0" Outlined Class="pa-2">
|
<MudPaper Elevation="0" Outlined Class="pa-2">
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
using DotBased.Monads;
|
||||||
using Manager.YouTube;
|
using Manager.YouTube;
|
||||||
|
|
||||||
namespace Manager.App.Services.System;
|
namespace Manager.App.Services.System;
|
||||||
@@ -17,4 +18,19 @@ public class ClientManager : BackgroundService
|
|||||||
{
|
{
|
||||||
// Clear up
|
// Clear up
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Result> SaveClientAsync(YouTubeClient client)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Not implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result<YouTubeClient>> LoadClientByIdAsync(string id)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(id))
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Client ID is empty!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResultError.Fail("Not implemented");
|
||||||
|
}
|
||||||
}
|
}
|
@@ -5,7 +5,7 @@ namespace Manager.YouTube.Models;
|
|||||||
public class ClientExternalData
|
public class ClientExternalData
|
||||||
{
|
{
|
||||||
public ClientState? State { get; set; }
|
public ClientState? State { get; set; }
|
||||||
public ClientInformation Information { get; set; } = new();
|
public Channel? Channel { get; set; }
|
||||||
public List<string> DatasyncIds { get; set; } = [];
|
public List<string> DatasyncIds { get; set; } = [];
|
||||||
|
|
||||||
public string GetDatasyncId()
|
public string GetDatasyncId()
|
||||||
|
@@ -1,9 +0,0 @@
|
|||||||
namespace Manager.YouTube.Models;
|
|
||||||
|
|
||||||
public class ClientInformation
|
|
||||||
{
|
|
||||||
public string? AccountName { get; set; }
|
|
||||||
public string? AccountHandle { get; set; }
|
|
||||||
public string? Description { get; set; }
|
|
||||||
public bool IsPremiumUser { get; set; }
|
|
||||||
}
|
|
@@ -1,10 +1,11 @@
|
|||||||
namespace Manager.YouTube.Models.Innertube;
|
namespace Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
public class ChannelFetch
|
public class Channel
|
||||||
{
|
{
|
||||||
public bool NoIndex { get; set; }
|
public bool NoIndex { get; set; }
|
||||||
public bool Unlisted { get; set; }
|
public bool Unlisted { get; set; }
|
||||||
public bool FamilySafe { get; set; }
|
public bool FamilySafe { get; set; }
|
||||||
|
public string? ChannelName { get; set; }
|
||||||
public string? Handle { get; set; }
|
public string? Handle { get; set; }
|
||||||
public string? Description { get; set; }
|
public string? Description { get; set; }
|
||||||
public List<string> AvailableCountries { get; set; } = [];
|
public List<string> AvailableCountries { get; set; } = [];
|
@@ -4,6 +4,7 @@ namespace Manager.YouTube.Models.Innertube;
|
|||||||
|
|
||||||
public class ClientState : AdditionalJsonData
|
public class ClientState : AdditionalJsonData
|
||||||
{
|
{
|
||||||
|
public bool IsPremiumUser { get; set; }
|
||||||
[JsonPropertyName("INNERTUBE_API_KEY")]
|
[JsonPropertyName("INNERTUBE_API_KEY")]
|
||||||
public string? InnertubeApiKey { get; set; }
|
public string? InnertubeApiKey { get; set; }
|
||||||
|
|
||||||
|
@@ -1,200 +1,40 @@
|
|||||||
using System.Net.Mime;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Nodes;
|
|
||||||
using DotBased.Monads;
|
using DotBased.Monads;
|
||||||
using Manager.YouTube.Models.Innertube;
|
|
||||||
using Manager.YouTube.Parsers;
|
|
||||||
using Manager.YouTube.Parsers.Json;
|
|
||||||
using Manager.YouTube.Util;
|
using Manager.YouTube.Util;
|
||||||
|
|
||||||
namespace Manager.YouTube;
|
namespace Manager.YouTube;
|
||||||
|
|
||||||
public static class NetworkService
|
public static class NetworkService
|
||||||
{
|
{
|
||||||
private const string Origin = "https://www.youtube.com";
|
public 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.Clear();
|
|
||||||
httpRequest.Headers.UserAgent.ParseAdd(client.UserAgent);
|
|
||||||
|
|
||||||
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 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;
|
public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client)
|
||||||
try
|
|
||||||
{
|
|
||||||
clientState = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value.Item1);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
return ResultError.Error(e, "Error while parsing JSON!");
|
|
||||||
}
|
|
||||||
|
|
||||||
client.External.Information.IsPremiumUser = clientStateResult.Value.Item2;
|
|
||||||
|
|
||||||
return clientState == null ? ResultError.Fail("Unable to parse client state!") : clientState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<Result<string[]>> GetDatasyncIds(YouTubeClient client)
|
|
||||||
{
|
{
|
||||||
if (client.External.State is not { LoggedIn: true } || client.CookieContainer.Count == 0)
|
request.Headers.Add("Origin", Origin);
|
||||||
|
request.Headers.UserAgent.ParseAdd(client.UserAgent);
|
||||||
|
if (client.SapisidCookie != null)
|
||||||
{
|
{
|
||||||
return ResultError.Fail("Client is not logged in, requires logged in client for this endpoint (/getDatasyncIdsEndpoint).");
|
request.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.External.GetDatasyncId(), client.SapisidCookie.Value, Origin);
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpClient = client.GetHttpClient();
|
var httpClient = client.GetHttpClient();
|
||||||
if (httpClient == null)
|
if (httpClient == null)
|
||||||
{
|
{
|
||||||
return ResultError.Fail("Unable to get http client!");
|
return ResultError.Fail("Failed getting http client!");
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpRequest = new HttpRequestMessage
|
|
||||||
{
|
|
||||||
Method = HttpMethod.Get,
|
|
||||||
RequestUri = new Uri($"{Origin}/getDatasyncIdsEndpoint")
|
|
||||||
};
|
|
||||||
httpRequest.Headers.UserAgent.ParseAdd(client.UserAgent);
|
|
||||||
httpRequest.Headers.Add("Origin", Origin);
|
|
||||||
|
|
||||||
var response = await httpClient.SendAsync(httpRequest);
|
|
||||||
if (!response.IsSuccessStatusCode)
|
|
||||||
{
|
|
||||||
var responseResult = await response.Content.ReadAsStringAsync();
|
|
||||||
return ResultError.Fail(responseResult);
|
|
||||||
}
|
|
||||||
|
|
||||||
var responseContent = await response.Content.ReadAsStringAsync();
|
|
||||||
var datasyncIdsJson = JsonNode.Parse(responseContent.Replace(")]}'", ""));
|
|
||||||
|
|
||||||
var isLoggedOut = datasyncIdsJson?["responseContext"]?["mainAppWebResponseContext"]?["loggedOut"]
|
|
||||||
.Deserialize<bool>() ?? true;
|
|
||||||
if (!isLoggedOut)
|
|
||||||
{
|
|
||||||
return datasyncIdsJson?["datasyncIds"].Deserialize<string[]>() ?? [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResultError.Fail("Failed to get datasyncIds!");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<Result<string>> GetCurrentAccountIdAsync(YouTubeClient client)
|
|
||||||
{
|
|
||||||
if (client.External.State is not { LoggedIn: true })
|
|
||||||
{
|
|
||||||
return ResultError.Fail("Client not logged in!");
|
|
||||||
}
|
|
||||||
|
|
||||||
var httpRequest = new HttpRequestMessage
|
|
||||||
{
|
|
||||||
Method = HttpMethod.Post,
|
|
||||||
RequestUri = new Uri($"{Origin}/youtubei/v1/account/account_menu")
|
|
||||||
};
|
|
||||||
httpRequest.Headers.UserAgent.ParseAdd(client.UserAgent);
|
|
||||||
httpRequest.Headers.Add("Origin", Origin);
|
|
||||||
|
|
||||||
if (client.SapisidCookie != null)
|
|
||||||
{
|
|
||||||
httpRequest.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.External.GetDatasyncId(), client.SapisidCookie.Value, Origin);
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
return ResultError.Fail("Unable to get http client!");
|
|
||||||
}
|
|
||||||
|
|
||||||
string json;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var response = await http.SendAsync(httpRequest);
|
var response = await httpClient.SendAsync(request);
|
||||||
|
var contentString = await response.Content.ReadAsStringAsync();
|
||||||
if (!response.IsSuccessStatusCode)
|
if (!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
var responseResult = await response.Content.ReadAsStringAsync();
|
return ResultError.Fail(contentString);
|
||||||
return ResultError.Fail(responseResult);
|
|
||||||
}
|
}
|
||||||
|
return contentString;
|
||||||
json = await response.Content.ReadAsStringAsync();
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return ResultError.Error(e);
|
return ResultError.Error(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonAccountParser.ParseAccountId(json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<Result<ChannelFetch>> 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();
|
|
||||||
var parsed = ChannelJsonParser.ParseJsonToChannelData(jsonContent);
|
|
||||||
|
|
||||||
return ResultError.Fail("Not implemented!");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -9,25 +9,26 @@ namespace Manager.YouTube.Parsers.Json;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class ChannelJsonParser
|
public static class ChannelJsonParser
|
||||||
{
|
{
|
||||||
public static Result<ChannelFetch> ParseJsonToChannelData(string json)
|
public static Result<Channel> ParseJsonToChannelData(string json)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var channel = new Channel();
|
||||||
var doc = JsonDocument.Parse(json);
|
var doc = JsonDocument.Parse(json);
|
||||||
var rootDoc = doc.RootElement;
|
var rootDoc = doc.RootElement;
|
||||||
|
|
||||||
var microformat = rootDoc.GetProperty("microformat").GetProperty("microformatDataRenderer");
|
var microformat = rootDoc.GetProperty("microformat").GetProperty("microformatDataRenderer");
|
||||||
|
|
||||||
var availableCountries = microformat
|
channel.AvailableCountries = microformat
|
||||||
.GetProperty("availableCountries")
|
.GetProperty("availableCountries")
|
||||||
.EnumerateArray()
|
.EnumerateArray()
|
||||||
.Select(e => e.GetString())
|
.Select(e => e.GetString())
|
||||||
.ToList();
|
.OfType<string>().ToList();
|
||||||
|
channel.Description = microformat.GetProperty("description").GetString();
|
||||||
var description = microformat.GetProperty("description").GetString();
|
channel.NoIndex = microformat.GetProperty("noindex").GetBoolean();
|
||||||
var noIndex = microformat.GetProperty("noindex").GetBoolean();
|
channel.Unlisted = microformat.GetProperty("unlisted").GetBoolean();
|
||||||
var unlisted = microformat.GetProperty("unlisted").GetBoolean();
|
channel.FamilySafe = microformat.GetProperty("familySafe").GetBoolean();
|
||||||
var familySafe = microformat.GetProperty("familySafe").GetBoolean();
|
channel.ChannelName = microformat.GetProperty("title").GetString();
|
||||||
|
|
||||||
var avatarThumbnails = rootDoc
|
var avatarThumbnails = rootDoc
|
||||||
.GetProperty("metadata")
|
.GetProperty("metadata")
|
||||||
@@ -35,15 +36,14 @@ public static class ChannelJsonParser
|
|||||||
.GetProperty("avatar")
|
.GetProperty("avatar")
|
||||||
.GetProperty("thumbnails")
|
.GetProperty("thumbnails")
|
||||||
.EnumerateArray();
|
.EnumerateArray();
|
||||||
|
channel.AvatarImages = JsonParser.ParseImages(avatarThumbnails);
|
||||||
var avatars = JsonParser.ParseImages(avatarThumbnails);
|
|
||||||
|
|
||||||
var headerContent = rootDoc
|
var headerContent = rootDoc
|
||||||
.GetProperty("header")
|
.GetProperty("header")
|
||||||
.GetProperty("pageHeaderRenderer")
|
.GetProperty("pageHeaderRenderer")
|
||||||
.GetProperty("content");
|
.GetProperty("content");
|
||||||
|
|
||||||
var metadataPartHandle = headerContent
|
channel.Handle = headerContent
|
||||||
.GetProperty("pageHeaderViewModel")
|
.GetProperty("pageHeaderViewModel")
|
||||||
.GetProperty("metadata")
|
.GetProperty("metadata")
|
||||||
.GetProperty("contentMetadataViewModel")
|
.GetProperty("contentMetadataViewModel")
|
||||||
@@ -63,21 +63,9 @@ public static class ChannelJsonParser
|
|||||||
.GetProperty("image")
|
.GetProperty("image")
|
||||||
.GetProperty("sources")
|
.GetProperty("sources")
|
||||||
.EnumerateArray();
|
.EnumerateArray();
|
||||||
|
channel.BannerImages = JsonParser.ParseImages(bannerImages);
|
||||||
var banners = JsonParser.ParseImages(bannerImages);
|
|
||||||
|
return channel;
|
||||||
var resultFetch = new ChannelFetch
|
|
||||||
{
|
|
||||||
NoIndex = noIndex,
|
|
||||||
Unlisted = unlisted,
|
|
||||||
FamilySafe = familySafe,
|
|
||||||
Handle = metadataPartHandle,
|
|
||||||
Description = description,
|
|
||||||
AvailableCountries = availableCountries.OfType<string>().ToList(),
|
|
||||||
AvatarImages = avatars,
|
|
||||||
BannerImages = banners
|
|
||||||
};
|
|
||||||
return resultFetch;
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@@ -1,6 +1,13 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Mime;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
using DotBased.Monads;
|
using DotBased.Monads;
|
||||||
using Manager.YouTube.Models;
|
using Manager.YouTube.Models;
|
||||||
|
using Manager.YouTube.Models.Innertube;
|
||||||
|
using Manager.YouTube.Parsers;
|
||||||
|
using Manager.YouTube.Parsers.Json;
|
||||||
|
|
||||||
namespace Manager.YouTube;
|
namespace Manager.YouTube;
|
||||||
|
|
||||||
@@ -38,7 +45,7 @@ public sealed class YouTubeClient : IDisposable
|
|||||||
{
|
{
|
||||||
if (External.State is not { LoggedIn: true })
|
if (External.State is not { LoggedIn: true })
|
||||||
{
|
{
|
||||||
var state = await NetworkService.GetClientStateAsync(this);
|
var state = await GetClientStateAsync();
|
||||||
if (!state.IsSuccess)
|
if (!state.IsSuccess)
|
||||||
{
|
{
|
||||||
return state;
|
return state;
|
||||||
@@ -48,7 +55,7 @@ public sealed class YouTubeClient : IDisposable
|
|||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(External.State.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
if (string.IsNullOrWhiteSpace(External.State.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
||||||
{
|
{
|
||||||
var datasyncResult = await NetworkService.GetDatasyncIds(this);
|
var datasyncResult = await GetDatasyncIds();
|
||||||
if (!datasyncResult.IsSuccess)
|
if (!datasyncResult.IsSuccess)
|
||||||
{
|
{
|
||||||
return datasyncResult;
|
return datasyncResult;
|
||||||
@@ -64,7 +71,7 @@ public sealed class YouTubeClient : IDisposable
|
|||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(Id))
|
if (string.IsNullOrWhiteSpace(Id))
|
||||||
{
|
{
|
||||||
var accountInfoResult = await NetworkService.GetCurrentAccountIdAsync(this);
|
var accountInfoResult = await GetCurrentAccountIdAsync();
|
||||||
if (!accountInfoResult.IsSuccess)
|
if (!accountInfoResult.IsSuccess)
|
||||||
{
|
{
|
||||||
return accountInfoResult;
|
return accountInfoResult;
|
||||||
@@ -73,13 +80,143 @@ public sealed class YouTubeClient : IDisposable
|
|||||||
Id = accountInfoResult.Value;
|
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();
|
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()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_httpClient?.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.");
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,6 +0,0 @@
|
|||||||
namespace Manager.YouTube;
|
|
||||||
|
|
||||||
public class YouTubeService()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
Reference in New Issue
Block a user