264 lines
8.6 KiB
C#
264 lines
8.6 KiB
C#
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.Innertube;
|
|
using Manager.YouTube.Parsers;
|
|
using Manager.YouTube.Parsers.Json;
|
|
|
|
namespace Manager.YouTube;
|
|
|
|
public sealed class YouTubeClient : IDisposable
|
|
{
|
|
public string Id { get; private set; } = "";
|
|
public string? UserAgent { get; set; }
|
|
public bool IsAnonymous { get; }
|
|
public CookieContainer CookieContainer { get; } = new() { PerDomainCapacity = 50 };
|
|
public ClientState? State { get; private set; }
|
|
public List<string> DatasyncIds { get; } = [];
|
|
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"];
|
|
public HttpClient HttpClient { get; }
|
|
|
|
private YouTubeClient(CookieCollection cookies, string userAgent)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(userAgent))
|
|
{
|
|
throw new ArgumentNullException(nameof(userAgent));
|
|
}
|
|
UserAgent = userAgent;
|
|
if (cookies.Count == 0)
|
|
{
|
|
Id = $"anon_{Guid.NewGuid()}";
|
|
IsAnonymous = true;
|
|
}
|
|
|
|
CookieContainer.Add(cookies);
|
|
HttpClient = new HttpClient(GetHttpClientHandler());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads the given cookies and fetch client state.
|
|
/// </summary>
|
|
/// <param name="cookies">The cookies to use for making requests. Empty collection for anonymous requests.</param>
|
|
/// <param name="userAgent">The user agent to use for the requests. Only WEB client is supported.</param>
|
|
/// <returns></returns>
|
|
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection cookies, string userAgent)
|
|
{
|
|
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
|
|
};
|
|
return clientHandler;
|
|
}
|
|
|
|
internal async Task<Result> FetchClientDataAsync()
|
|
{
|
|
if (State is not { LoggedIn: true })
|
|
{
|
|
var state = await GetClientStateAsync();
|
|
if (!state.IsSuccess)
|
|
{
|
|
return state;
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
|
{
|
|
var datasyncResult = await GetDatasyncIds();
|
|
if (!datasyncResult.IsSuccess)
|
|
{
|
|
return datasyncResult;
|
|
}
|
|
|
|
foreach (var id in datasyncResult.Value)
|
|
{
|
|
if (DatasyncIds.Contains(id))
|
|
continue;
|
|
DatasyncIds.Add(id);
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(Id))
|
|
{
|
|
var accountInfoResult = await GetCurrentAccountIdAsync();
|
|
if (!accountInfoResult.IsSuccess)
|
|
{
|
|
return accountInfoResult;
|
|
}
|
|
|
|
Id = accountInfoResult.Value;
|
|
}
|
|
|
|
return Result.Success();
|
|
}
|
|
|
|
private async Task<Result> GetClientStateAsync()
|
|
{
|
|
var httpRequest = new HttpRequestMessage
|
|
{
|
|
Method = HttpMethod.Get,
|
|
RequestUri = new Uri(NetworkService.Origin)
|
|
};
|
|
|
|
var result = await NetworkService.MakeRequestAsync(httpRequest, this, true);
|
|
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;
|
|
}
|
|
|
|
try
|
|
{
|
|
State = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value.Item1);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return ResultError.Error(e, "Error while parsing JSON!");
|
|
}
|
|
|
|
|
|
if (State == null)
|
|
{
|
|
return ResultError.Fail("Unable to parse client state!");
|
|
}
|
|
|
|
State.IsPremiumUser = clientStateResult.Value.Item2;
|
|
|
|
return Result.Success();
|
|
}
|
|
|
|
public async Task<Result<InnertubeChannel>> GetChannelByIdAsync(string channelId)
|
|
{
|
|
if (State == null)
|
|
{
|
|
return ResultError.Fail("No client state!");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(channelId))
|
|
{
|
|
return ResultError.Fail("Channel id is empty!");
|
|
}
|
|
|
|
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={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 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();
|
|
}
|
|
|
|
private async Task<Result<string>> GetCurrentAccountIdAsync()
|
|
{
|
|
if (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(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 (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.");
|
|
}
|
|
} |