[CHANGE] Reworked client creation
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
using Manager.YouTube.Models.Innertube;
|
||||
|
||||
namespace Manager.YouTube.Models;
|
||||
|
||||
public class ClientExternalData
|
||||
{
|
||||
public ClientState? State { get; set; }
|
||||
public Channel? Channel { get; set; }
|
||||
public List<string> DatasyncIds { get; set; } = [];
|
||||
|
||||
public string GetDatasyncId()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
||||
{
|
||||
return State.WebPlayerContextConfig.WebPlayerContext.DatasyncId;
|
||||
}
|
||||
|
||||
var tempDatasyncId = "";
|
||||
foreach (var datasyncId in DatasyncIds)
|
||||
{
|
||||
var split = datasyncId.Split("||", StringSplitOptions.RemoveEmptyEntries);
|
||||
switch (split.Length)
|
||||
{
|
||||
case 0:
|
||||
case 2 when tempDatasyncId.Equals(split[1]):
|
||||
continue;
|
||||
case 2:
|
||||
tempDatasyncId = split[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return tempDatasyncId;
|
||||
}
|
||||
}
|
@@ -7,24 +7,18 @@ public static class NetworkService
|
||||
{
|
||||
public const string Origin = "https://www.youtube.com";
|
||||
|
||||
public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client)
|
||||
public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client, bool skipAuthenticationHeader = false)
|
||||
{
|
||||
request.Headers.Add("Origin", Origin);
|
||||
request.Headers.UserAgent.ParseAdd(client.UserAgent);
|
||||
if (client.SapisidCookie != null)
|
||||
if (client.SapisidCookie != null && !skipAuthenticationHeader)
|
||||
{
|
||||
request.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.External.GetDatasyncId(), client.SapisidCookie.Value, Origin);
|
||||
}
|
||||
|
||||
var httpClient = client.GetHttpClient();
|
||||
if (httpClient == null)
|
||||
{
|
||||
return ResultError.Fail("Failed getting http client!");
|
||||
request.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.GetDatasyncId(), client.SapisidCookie.Value, Origin);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var response = await httpClient.SendAsync(request);
|
||||
var response = await client.HttpClient.SendAsync(request);
|
||||
var contentString = await response.Content.ReadAsStringAsync();
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
|
@@ -10,16 +10,27 @@ public static class AuthenticationUtilities
|
||||
private const string HeaderScheme = "SAPISIDHASH";
|
||||
|
||||
// Dave Thomas & windy for updated answer @ https://stackoverflow.com/a/32065323/9948300
|
||||
public static AuthenticationHeaderValue? GetSapisidHashHeader(string datasyncId, string sapisid, string origin)
|
||||
public static AuthenticationHeaderValue GetSapisidHashHeader(string datasyncId, string sapisid, string origin)
|
||||
{
|
||||
var strHash = GetSapisidHash(datasyncId, sapisid, origin);
|
||||
return strHash == null ? null : new AuthenticationHeaderValue(HeaderScheme, strHash);
|
||||
return new AuthenticationHeaderValue(HeaderScheme, strHash);
|
||||
}
|
||||
|
||||
public static string? GetSapisidHash(string datasyncId, string sapisid, string origin, string? time = null)
|
||||
public static string GetSapisidHash(string datasyncId, string sapisid, string origin, string? time = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(datasyncId) || string.IsNullOrWhiteSpace(sapisid) || string.IsNullOrWhiteSpace(origin))
|
||||
return null;
|
||||
if (string.IsNullOrWhiteSpace(datasyncId))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(datasyncId));
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(sapisid))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(sapisid));
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(origin))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(origin));
|
||||
}
|
||||
|
||||
datasyncId = datasyncId.Replace("||", "");
|
||||
sapisid = Uri.UnescapeDataString(sapisid);
|
||||
if (string.IsNullOrWhiteSpace(time))
|
||||
|
@@ -4,7 +4,6 @@ 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;
|
||||
@@ -16,44 +15,62 @@ 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 ClientState? State { get; private set; }
|
||||
public List<string> DatasyncIds { get; } = [];
|
||||
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"];
|
||||
public HttpClient? GetHttpClient() => _httpClient;
|
||||
public HttpClient HttpClient { get; }
|
||||
|
||||
private HttpClient? _httpClient;
|
||||
|
||||
public YouTubeClient()
|
||||
private YouTubeClient(CookieCollection cookies, string userAgent)
|
||||
{
|
||||
SetupClient();
|
||||
if (string.IsNullOrWhiteSpace(userAgent))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(userAgent));
|
||||
}
|
||||
UserAgent = userAgent;
|
||||
if (cookies.Count == 0)
|
||||
{
|
||||
Id = $"anon_{Guid.NewGuid()}";
|
||||
}
|
||||
|
||||
CookieContainer.Add(cookies);
|
||||
HttpClient = new HttpClient(GetHttpClientHandler());
|
||||
}
|
||||
|
||||
private void SetupClient()
|
||||
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection cookies, string userAgent)
|
||||
{
|
||||
_httpClient?.Dispose();
|
||||
var client = new YouTubeClient(cookies, userAgent);
|
||||
var clientInitializeResult = await client.FetchClientDataAsync();
|
||||
if (!clientInitializeResult.IsSuccess)
|
||||
{
|
||||
return clientInitializeResult.Error ?? ResultError.Fail("Failed to initialize YouTube client!");
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
private HttpClientHandler GetHttpClientHandler()
|
||||
{
|
||||
var clientHandler = new HttpClientHandler
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
||||
UseCookies = true,
|
||||
CookieContainer = CookieContainer
|
||||
};
|
||||
_httpClient = new HttpClient(clientHandler);
|
||||
_httpClient.DefaultRequestHeaders.Clear();
|
||||
return clientHandler;
|
||||
}
|
||||
|
||||
public async Task<Result> BuildClientAsync()
|
||||
internal async Task<Result> FetchClientDataAsync()
|
||||
{
|
||||
if (External.State is not { LoggedIn: true })
|
||||
if (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))
|
||||
if (string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
||||
{
|
||||
var datasyncResult = await GetDatasyncIds();
|
||||
if (!datasyncResult.IsSuccess)
|
||||
@@ -63,9 +80,9 @@ public sealed class YouTubeClient : IDisposable
|
||||
|
||||
foreach (var id in datasyncResult.Value)
|
||||
{
|
||||
if (External.DatasyncIds.Contains(id))
|
||||
if (DatasyncIds.Contains(id))
|
||||
continue;
|
||||
External.DatasyncIds.Add(id);
|
||||
DatasyncIds.Add(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,18 +96,11 @@ public sealed class YouTubeClient : IDisposable
|
||||
|
||||
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()
|
||||
private async Task<Result> GetClientStateAsync()
|
||||
{
|
||||
var httpRequest = new HttpRequestMessage
|
||||
{
|
||||
@@ -98,7 +108,7 @@ public sealed class YouTubeClient : IDisposable
|
||||
RequestUri = new Uri(NetworkService.Origin)
|
||||
};
|
||||
|
||||
var result = await NetworkService.MakeRequestAsync(httpRequest, this);
|
||||
var result = await NetworkService.MakeRequestAsync(httpRequest, this, true);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
return result.Error ?? ResultError.Fail("Request failed!");
|
||||
@@ -110,10 +120,9 @@ public sealed class YouTubeClient : IDisposable
|
||||
return clientStateResult.Error;
|
||||
}
|
||||
|
||||
ClientState? clientState;
|
||||
try
|
||||
{
|
||||
clientState = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value.Item1);
|
||||
State = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value.Item1);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -121,19 +130,19 @@ public sealed class YouTubeClient : IDisposable
|
||||
}
|
||||
|
||||
|
||||
if (clientState == null)
|
||||
if (State == null)
|
||||
{
|
||||
return ResultError.Fail("Unable to parse client state!");
|
||||
}
|
||||
|
||||
clientState.IsPremiumUser = clientStateResult.Value.Item2;
|
||||
State.IsPremiumUser = clientStateResult.Value.Item2;
|
||||
|
||||
return clientState;
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public async Task<Result<Channel>> GetChannelByIdAsync(string channelId)
|
||||
{
|
||||
if (External.State == null)
|
||||
if (State == null)
|
||||
{
|
||||
return ResultError.Fail("No client state!");
|
||||
}
|
||||
@@ -143,12 +152,12 @@ public sealed class YouTubeClient : IDisposable
|
||||
return ResultError.Fail("Channel id is empty!");
|
||||
}
|
||||
|
||||
var serializedContext = JsonSerializer.SerializeToNode(External.State.InnerTubeContext);
|
||||
var serializedContext = JsonSerializer.SerializeToNode(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}"),
|
||||
RequestUri = new Uri($"{NetworkService.Origin}/youtubei/v1/browse?key={State.InnertubeApiKey}"),
|
||||
Content = new StringContent(payload.ToJsonString(), Encoding.UTF8, MediaTypeNames.Application.Json)
|
||||
};
|
||||
var responseResult = await NetworkService.MakeRequestAsync(requestMessage, this);
|
||||
@@ -160,14 +169,39 @@ public sealed class YouTubeClient : IDisposable
|
||||
return ChannelJsonParser.ParseJsonToChannelData(responseResult.Value);
|
||||
}
|
||||
|
||||
public string GetDatasyncId()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
||||
{
|
||||
return State.WebPlayerContextConfig.WebPlayerContext.DatasyncId;
|
||||
}
|
||||
|
||||
var tempDatasyncId = "";
|
||||
foreach (var datasyncId in DatasyncIds)
|
||||
{
|
||||
var split = datasyncId.Split("||", StringSplitOptions.RemoveEmptyEntries);
|
||||
switch (split.Length)
|
||||
{
|
||||
case 0:
|
||||
case 2 when tempDatasyncId.Equals(split[1]):
|
||||
continue;
|
||||
case 2:
|
||||
tempDatasyncId = split[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return tempDatasyncId;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_httpClient?.Dispose();
|
||||
HttpClient?.Dispose();
|
||||
}
|
||||
|
||||
private async Task<Result<string>> GetCurrentAccountIdAsync()
|
||||
{
|
||||
if (External.State is not { LoggedIn: true })
|
||||
if (State is not { LoggedIn: true })
|
||||
{
|
||||
return ResultError.Fail("Client not logged in!");
|
||||
}
|
||||
@@ -177,7 +211,7 @@ public sealed class YouTubeClient : IDisposable
|
||||
Method = HttpMethod.Post,
|
||||
RequestUri = new Uri($"{NetworkService.Origin}/youtubei/v1/account/account_menu")
|
||||
};
|
||||
var serializedContext = JsonSerializer.SerializeToNode(External.State.InnerTubeContext);
|
||||
var serializedContext = JsonSerializer.SerializeToNode(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);
|
||||
@@ -192,7 +226,7 @@ public sealed class YouTubeClient : IDisposable
|
||||
|
||||
private async Task<Result<string[]>> GetDatasyncIds()
|
||||
{
|
||||
if (External.State is not { LoggedIn: true } || CookieContainer.Count == 0)
|
||||
if (State is not { LoggedIn: true } || CookieContainer.Count == 0)
|
||||
{
|
||||
return ResultError.Fail("Client is not logged in, requires logged in client for this endpoint (/getDatasyncIdsEndpoint).");
|
||||
}
|
||||
|
Reference in New Issue
Block a user