358 lines
12 KiB
C#
358 lines
12 KiB
C#
using System.Net;
|
|
using System.Net.Mime;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using DotBased.Logging;
|
|
using DotBased.Monads;
|
|
using Manager.YouTube.Models;
|
|
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 readonly ILogger _logger;
|
|
|
|
private YouTubeClient(CookieCollection? cookies, string userAgent, ILogger logger)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(userAgent))
|
|
{
|
|
throw new ArgumentNullException(nameof(userAgent));
|
|
}
|
|
_logger = logger;
|
|
UserAgent = userAgent;
|
|
if (cookies == null || cookies.Count == 0)
|
|
{
|
|
Id = $"anon_{Guid.NewGuid()}";
|
|
IsAnonymous = true;
|
|
}
|
|
else
|
|
{
|
|
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 or null for anonymous requests.</param>
|
|
/// <param name="userAgent">The user agent to use for the requests. Only WEB client is supported.</param>
|
|
/// <param name="logger">The logger that the client is going to use, if null will create a new logger.</param>
|
|
/// <returns></returns>
|
|
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection? cookies, string userAgent, ILogger? logger = null)
|
|
{
|
|
logger ??= LogService.RegisterLogger<YouTubeClient>();
|
|
|
|
var client = new YouTubeClient(cookies, userAgent, logger);
|
|
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 GetDatasyncIdsAsync();
|
|
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 stateResult = SetClientStateFromHtml(result.Value);
|
|
if (!stateResult.IsSuccess)
|
|
{
|
|
return stateResult;
|
|
}
|
|
|
|
var cookieRotationResult = await RotateCookiesPageAsync();
|
|
return !cookieRotationResult.IsSuccess ? cookieRotationResult : Result.Success();
|
|
}
|
|
|
|
private Result SetClientStateFromHtml(string html)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(html))
|
|
{
|
|
return ResultError.Fail("HTML is empty!!");
|
|
}
|
|
|
|
var clientStateResult = HtmlParser.GetStateJson(html);
|
|
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 async Task<Result> RotateCookiesPageAsync(string origin = NetworkService.Origin, int ytPid = 1)
|
|
{
|
|
if (IsAnonymous)
|
|
{
|
|
return ResultError.Fail("Anonymous clients cannot rotate cookies!");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(origin))
|
|
{
|
|
return ResultError.Fail("Origin is empty!");
|
|
}
|
|
|
|
var rotatePageCookiesRequest = new HttpRequestMessage(HttpMethod.Get, new Uri($"https://accounts.youtube.com/RotateCookiesPage?origin={origin}&yt_pid={ytPid}"));
|
|
return await NetworkService.MakeRequestAsync(rotatePageCookiesRequest, this, true);
|
|
}
|
|
|
|
public async Task<Result> RotateCookiesAsync()
|
|
{
|
|
if (IsAnonymous)
|
|
{
|
|
return ResultError.Fail("Anonymous clients cannot rotate cookies!");
|
|
}
|
|
|
|
var rotateRequest = new HttpRequestMessage(HttpMethod.Post, new Uri("https://accounts.youtube.com/RotateCookies"));
|
|
return await NetworkService.MakeRequestAsync(rotateRequest, this, true);
|
|
}
|
|
|
|
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[]>> GetDatasyncIdsAsync()
|
|
{
|
|
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.");
|
|
}
|
|
|
|
public async Task<Result<YouTubeVideo>> GetVideoByIdAsync(string videoId, CancellationToken cancellationToken = default)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(videoId))
|
|
{
|
|
return ResultError.Fail("Video id is empty!");
|
|
}
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, new Uri($"{NetworkService.Origin}/watch?v={videoId}"));
|
|
|
|
var response = await NetworkService.MakeRequestAsync(request, this);
|
|
if (!response.IsSuccess && !string.IsNullOrWhiteSpace(response.Value))
|
|
{
|
|
return response.Error ?? ResultError.Fail("Request failed!");
|
|
}
|
|
|
|
var html = response.Value;
|
|
|
|
var stateResult = SetClientStateFromHtml(html);
|
|
if (!stateResult.IsSuccess)
|
|
{
|
|
_logger.Warning("Failed to update client state.");
|
|
}
|
|
|
|
var htmlParseResult = HtmlParser.GetVideoDataFromHtml(html);
|
|
if (!htmlParseResult.IsSuccess)
|
|
{
|
|
return htmlParseResult.Error ?? ResultError.Fail("Failed to parse HTML video data!");
|
|
}
|
|
|
|
var videoParseResult = VideoJsonParser.ParseVideoData(htmlParseResult.Value);
|
|
if (!videoParseResult.IsSuccess)
|
|
{
|
|
return videoParseResult;
|
|
}
|
|
|
|
//TODO: decipher stream urls
|
|
|
|
return videoParseResult.Value;
|
|
}
|
|
} |