Compare commits
15 Commits
97f7f5dcf6
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d20c116da | ||
|
|
a849b7524d | ||
|
|
bf957436f0 | ||
|
|
16343c9a56 | ||
|
|
4c04378080 | ||
|
|
b5c701b971 | ||
|
|
a84195aefa | ||
|
|
de28591d24 | ||
|
|
264be8d529 | ||
|
|
25589d18d8 | ||
|
|
972af513f0 | ||
|
|
e87e1c57f9 | ||
|
|
41f880cfef | ||
|
|
9fdde5e756 | ||
|
|
ed9cb7eff1 |
13
Manager.App/Components/Application/Dev/CipherDev.razor
Normal file
13
Manager.App/Components/Application/Dev/CipherDev.razor
Normal file
@@ -0,0 +1,13 @@
|
||||
@using Manager.App.Models.System
|
||||
@using Manager.App.Services.System
|
||||
|
||||
@inject ISnackbar Snackbar
|
||||
@inject ClientService ClientService
|
||||
|
||||
<MudText>Cipher manager</MudText>
|
||||
<MudStack Row Spacing="2">
|
||||
<MudAutocomplete T="YouTubeClientItem" Label="Client" @bind-Value="@_selectedClient" SearchFunc="SearchClientsAsync" ToStringFunc="@(i => i == null ? "null?" : $"{i.Name} ({i.Handle})")"
|
||||
Variant="Variant.Outlined" ShowProgressIndicator ProgressIndicatorColor="Color.Primary">
|
||||
</MudAutocomplete>
|
||||
<MudButton OnClick="ExecCipher">Exec</MudButton>
|
||||
</MudStack>
|
||||
43
Manager.App/Components/Application/Dev/CipherDev.razor.cs
Normal file
43
Manager.App/Components/Application/Dev/CipherDev.razor.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Manager.App.Models.System;
|
||||
using Manager.YouTube.Util.Cipher;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using MudBlazor;
|
||||
|
||||
namespace Manager.App.Components.Application.Dev;
|
||||
|
||||
public partial class CipherDev : ComponentBase
|
||||
{
|
||||
private YouTubeClientItem? _selectedClient;
|
||||
|
||||
private async Task ExecCipher(MouseEventArgs obj)
|
||||
{
|
||||
if (_selectedClient == null)
|
||||
{
|
||||
Snackbar.Add("No client selected", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
var ytClientResult = await ClientService.LoadClientByIdAsync(_selectedClient.Id);
|
||||
if (!ytClientResult.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(ytClientResult.Error?.Description ?? "Failed to get the client!", Severity.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
var ytClient = ytClientResult.Value;
|
||||
if (ytClient.State == null)
|
||||
{
|
||||
Snackbar.Add("Client state is null!", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
var decoder = await CipherManager.GetDecoderAsync(ytClient.State, ytClient);
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<YouTubeClientItem>> SearchClientsAsync(string? search, CancellationToken cancellationToken)
|
||||
{
|
||||
var searchResults = await ClientService.GetClientsAsync(search, cancellationToken: cancellationToken);
|
||||
return !searchResults.IsSuccess ? [] : searchResults.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
@using Manager.App.Models.System
|
||||
@using Manager.App.Services.System
|
||||
|
||||
@inject ISnackbar Snackbar
|
||||
@inject ClientService ClientService
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
<MudText>Video data</MudText>
|
||||
<MudStack Row Spacing="2">
|
||||
<MudAutocomplete T="YouTubeClientItem" Label="Client" @bind-Value="@_selectedClient" SearchFunc="SearchClientsAsync" ToStringFunc="@(i => i == null ? "null?" : $"{i.Name} ({i.Handle})")"
|
||||
Variant="Variant.Outlined" ShowProgressIndicator ProgressIndicatorColor="Color.Primary">
|
||||
</MudAutocomplete>
|
||||
<MudTextField Label="Video id" @bind-Value="@_videoId"/>
|
||||
</MudStack>
|
||||
<MudStack>
|
||||
<MudButton OnClick="NavigateToVideo">Get data</MudButton>
|
||||
</MudStack>
|
||||
@@ -0,0 +1,40 @@
|
||||
using Manager.App.Models.System;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using MudBlazor;
|
||||
|
||||
namespace Manager.App.Components.Application.Dev;
|
||||
|
||||
public partial class DevelopmentVideo : ComponentBase
|
||||
{
|
||||
private YouTubeClientItem? _selectedClient;
|
||||
private string _videoId = "";
|
||||
|
||||
private async Task<IEnumerable<YouTubeClientItem>> SearchClientsAsync(string? search, CancellationToken cancellationToken)
|
||||
{
|
||||
var searchResults = await ClientService.GetClientsAsync(search, cancellationToken: cancellationToken);
|
||||
return !searchResults.IsSuccess ? [] : searchResults.Value;
|
||||
}
|
||||
|
||||
private void NavigateToVideo(MouseEventArgs obj)
|
||||
{
|
||||
if (_selectedClient == null)
|
||||
{
|
||||
Snackbar.Add("No client selected!", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_videoId))
|
||||
{
|
||||
Snackbar.Add("No video ID set!", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_videoId.Length != 11)
|
||||
{
|
||||
Snackbar.Add("Video ID needs to have an length of 11 chars!", Severity.Warning);
|
||||
}
|
||||
|
||||
NavigationManager.NavigateTo($"/video/{_videoId}?clientId={_selectedClient.Id}");
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,10 @@
|
||||
<MudNavMenu>
|
||||
<MudNavLink Href="/" Icon="@Icons.Material.Filled.Home" Match="NavLinkMatch.All">Home</MudNavLink>
|
||||
<MudNavGroup Title="Library" Expanded Icon="@Icons.Custom.Brands.YouTube" IconColor="Color.Error">
|
||||
<MudNavLink Href="/Search" Icon="@Icons.Material.Filled.Search" Match="NavLinkMatch.All" Disabled>Search</MudNavLink>
|
||||
<MudNavLink Href="/Accounts" Icon="@Icons.Material.Filled.AccountBox" Match="NavLinkMatch.All">Accounts</MudNavLink>
|
||||
<MudNavLink Href="/Channels" Icon="@Icons.Material.Filled.AccountCircle" Match="NavLinkMatch.All">Channels</MudNavLink>
|
||||
<MudNavLink Href="/Playlists" Icon="@Icons.Material.Filled.ViewList" Match="NavLinkMatch.All">Playlists</MudNavLink>
|
||||
<MudNavLink Href="/Playlists" Icon="@Icons.Material.Filled.ViewList" Match="NavLinkMatch.All" Disabled>Playlists</MudNavLink>
|
||||
<MudNavLink Href="/Library" Icon="@Icons.Material.Filled.Info" Match="NavLinkMatch.All" IconColor="Color.Info">Info</MudNavLink>
|
||||
</MudNavGroup>
|
||||
<MudNavGroup Title="Application" Expanded Icon="@Icons.Material.Filled.SettingsSystemDaydream" IconColor="Color.Primary">
|
||||
|
||||
@@ -6,4 +6,10 @@
|
||||
<MudTabPanel Text="Authentication">
|
||||
<AuthenticationHasher />
|
||||
</MudTabPanel>
|
||||
<MudTabPanel Text="Video">
|
||||
<DevelopmentVideo />
|
||||
</MudTabPanel>
|
||||
<MudTabPanel Text="Cipher">
|
||||
<CipherDev />
|
||||
</MudTabPanel>
|
||||
</MudTabs>
|
||||
244
Manager.App/Components/Pages/Video.razor
Normal file
244
Manager.App/Components/Pages/Video.razor
Normal file
@@ -0,0 +1,244 @@
|
||||
@page "/Video/{VideoId}"
|
||||
@using Manager.App.Services.System
|
||||
|
||||
@inject ISnackbar Snackbar
|
||||
@inject ClientService ClientService
|
||||
@inject CacheService Cache
|
||||
|
||||
<ForcedLoadingOverlay Visible="_loading"/>
|
||||
@if (!_loading && _video != null)
|
||||
{
|
||||
<MudStack Spacing="2">
|
||||
<MudCard>
|
||||
@{
|
||||
var thumbnailUrl = _video.Thumbnails.OrderByDescending(t => t.Width).FirstOrDefault()?.Url;
|
||||
}
|
||||
@if (!string.IsNullOrWhiteSpace(thumbnailUrl))
|
||||
{
|
||||
<MudCardMedia Image="@Cache.CreateCacheUrl(thumbnailUrl)" Height="500"/>
|
||||
}
|
||||
<MudCardContent>
|
||||
<MudText Typo="Typo.h5">@_video.Title</MudText>
|
||||
<MudText Typo="Typo.body2">@_video.Description</MudText>
|
||||
</MudCardContent>
|
||||
</MudCard>
|
||||
<MudExpansionPanels MultiExpansion>
|
||||
<MudExpansionPanel Text="Info" Expanded>
|
||||
<MudStack Spacing="2" Row Wrap="Wrap.Wrap">
|
||||
@* Info *@
|
||||
<MudSimpleTable Bordered Dense Elevation="0" Outlined Square Hover>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Video ID:</td>
|
||||
<td>@_video.VideoId</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Title:</td>
|
||||
<td>@_video.Title</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Description:</td>
|
||||
<td>@_video.Description</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>HashTags:</td>
|
||||
<td>@foreach (var hashtag in _video.HashTags)
|
||||
{
|
||||
<MudChip T="string" Variant="Variant.Text" Color="Color.Info">@hashtag</MudChip>
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>View count:</td>
|
||||
<td>@_video.ViewCount</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Like count:</td>
|
||||
<td>@_video.LikeCount</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Channel ID:</td>
|
||||
<td>@_video.ChannelId</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Author:</td>
|
||||
<td>@_video.Author</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Playability status:</td>
|
||||
<td>@_video.PlayabilityStatus</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Length seconds:</td>
|
||||
<td>@_video.LengthSeconds</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Keywords:</td>
|
||||
<td>@foreach (var keyword in _video.Keywords)
|
||||
{
|
||||
<MudChip T="string" Variant="Variant.Text">@keyword</MudChip>
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Publish date:</td>
|
||||
<td>@_video.PublishDate</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Upload date:</td>
|
||||
<td>@_video.UploadDate</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Category:</td>
|
||||
<td>@_video.Category</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</MudSimpleTable>
|
||||
@* Boolean values *@
|
||||
<MudSimpleTable Bordered Dense Elevation="0" Outlined Square Hover>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Is owner viewing:</td>
|
||||
<td>@_video.IsOwnerViewing</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Allow rating:</td>
|
||||
<td>@_video.AllowRating</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Is crawlable:</td>
|
||||
<td>@_video.IsCrawlable</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Is private:</td>
|
||||
<td>@_video.IsPrivate</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Is unplugged corpus:</td>
|
||||
<td>@_video.IsUnpluggedCorpus</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Is live:</td>
|
||||
<td>@_video.IsLive</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Is family save:</td>
|
||||
<td>@_video.IsFamilySave</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Is unlisted:</td>
|
||||
<td>@_video.IsUnlisted</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Has Ypc metadata:</td>
|
||||
<td>@_video.HasYpcMetadata</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Is shorts eligible:</td>
|
||||
<td>@_video.IsShortsEligible</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</MudSimpleTable>
|
||||
</MudStack>
|
||||
</MudExpansionPanel>
|
||||
<MudExpansionPanel Text="Streaming data">
|
||||
@if (_video.StreamingData == null)
|
||||
{
|
||||
<MudAlert Severity="Severity.Info">No streaming data available!</MudAlert>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudStack Spacing="2" Row Wrap="Wrap.Wrap">
|
||||
<MudStack>
|
||||
<MudText Typo="Typo.h5">Adaptive Formats</MudText>
|
||||
<MudTable Items="@_video.StreamingData.AdaptiveFormats">
|
||||
<HeaderContent>
|
||||
<MudTh>Id</MudTh>
|
||||
<MudTh>Mime type</MudTh>
|
||||
<MudTh>Bitrate</MudTh>
|
||||
<MudTh>Resolution</MudTh>
|
||||
<MudTh>Last modified (UNIX epoch)</MudTh>
|
||||
<MudTh>Quality</MudTh>
|
||||
<MudTh>FPS</MudTh>
|
||||
</HeaderContent>
|
||||
<RowTemplate>
|
||||
<MudTd>@context.Itag</MudTd>
|
||||
<MudTd>@context.MimeType</MudTd>
|
||||
<MudTd>@context.Bitrate</MudTd>
|
||||
<MudTd>@($"{context.Width}x{context.Height}")</MudTd>
|
||||
<MudTd>@context.LastModified</MudTd>
|
||||
<MudTd>@context.Quality</MudTd>
|
||||
<MudTd>@context.Fps</MudTd>
|
||||
</RowTemplate>
|
||||
</MudTable>
|
||||
</MudStack>
|
||||
<MudStack>
|
||||
<MudText Typo="Typo.h5">Formats</MudText>
|
||||
<MudTable Items="@_video.StreamingData.Formats">
|
||||
<HeaderContent>
|
||||
<MudTh>Id</MudTh>
|
||||
<MudTh>Mime type</MudTh>
|
||||
<MudTh>Bitrate</MudTh>
|
||||
<MudTh>Resolution</MudTh>
|
||||
<MudTh>Last modified (UNIX epoch)</MudTh>
|
||||
<MudTh>Quality</MudTh>
|
||||
<MudTh>FPS</MudTh>
|
||||
</HeaderContent>
|
||||
<RowTemplate>
|
||||
<MudTd>@context.Itag</MudTd>
|
||||
<MudTd>@context.MimeType</MudTd>
|
||||
<MudTd>@context.Bitrate</MudTd>
|
||||
<MudTd>@($"{context.Width}x{context.Height}")</MudTd>
|
||||
<MudTd>@context.LastModified</MudTd>
|
||||
<MudTd>@context.Quality</MudTd>
|
||||
<MudTd>@context.Fps</MudTd>
|
||||
</RowTemplate>
|
||||
</MudTable>
|
||||
</MudStack>
|
||||
</MudStack>
|
||||
}
|
||||
</MudExpansionPanel>
|
||||
<MudExpansionPanel Text="Player config">
|
||||
@if (_video.PlayerConfig == null)
|
||||
{
|
||||
<MudAlert Severity="Severity.Info">No player config available!</MudAlert>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudSimpleTable Bordered Dense Elevation="0" Outlined Square Hover>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Audio loudness DB:</td>
|
||||
<td>@_video.PlayerConfig.AudioLoudnessDb</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Audio perceptual loudness DB:</td>
|
||||
<td>@_video.PlayerConfig.AudioPerceptualLoudnessDb</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Audio enable per format loudness:</td>
|
||||
<td>@_video.PlayerConfig.AudioLoudnessDb</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Max bitrate:</td>
|
||||
<td>@_video.PlayerConfig.MaxBitrate</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Max read ahead time MS:</td>
|
||||
<td>@_video.PlayerConfig.MaxReadAheadMediaTimeMs</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Min read ahead time MS:</td>
|
||||
<td>@_video.PlayerConfig.MinReadAheadMediaTimeMs</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Read ahead growth rate MS:</td>
|
||||
<td>@_video.PlayerConfig.ReadAheadGrowthRateMs</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</MudSimpleTable>
|
||||
}
|
||||
</MudExpansionPanel>
|
||||
</MudExpansionPanels>
|
||||
</MudStack>
|
||||
}
|
||||
48
Manager.App/Components/Pages/Video.razor.cs
Normal file
48
Manager.App/Components/Pages/Video.razor.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Manager.YouTube.Models;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using MudBlazor;
|
||||
|
||||
namespace Manager.App.Components.Pages;
|
||||
|
||||
public partial class Video : ComponentBase
|
||||
{
|
||||
[Parameter]
|
||||
public required string VideoId { get; set; }
|
||||
|
||||
[SupplyParameterFromQuery(Name = "clientId")]
|
||||
public string ClientId { get; set; } = "";
|
||||
|
||||
private bool _loading = true;
|
||||
private YouTubeVideo? _video;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (string.IsNullOrEmpty(VideoId))
|
||||
{
|
||||
Snackbar.Add("Video id is null or empty!", Severity.Error);
|
||||
_loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var clientResult = await ClientService.LoadClientByIdAsync(ClientId);
|
||||
if (!clientResult.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(clientResult.Error?.Description ?? "Failed to load client!", Severity.Error);
|
||||
_loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var client = clientResult.Value;
|
||||
|
||||
var videoResult = await client.GetVideoByIdAsync(VideoId);
|
||||
if (!videoResult.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(videoResult.Error?.Description ?? "Failed to get video.", Severity.Error);
|
||||
_loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
_video = videoResult.Value;
|
||||
_loading = false;
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@ namespace Manager.App.Services;
|
||||
public class LibraryService : ILibraryService
|
||||
{
|
||||
private readonly ILogger<LibraryService> _logger;
|
||||
private readonly LibrarySettings _librarySettings;
|
||||
private readonly IDbContextFactory<LibraryDbContext> _dbContextFactory;
|
||||
private readonly DirectoryInfo _libraryDirectory;
|
||||
private readonly CacheService _cacheService;
|
||||
@@ -24,13 +23,13 @@ public class LibraryService : ILibraryService
|
||||
public LibraryService(ILogger<LibraryService> logger, IOptions<LibrarySettings> librarySettings, IDbContextFactory<LibraryDbContext> contextFactory, CacheService cacheService)
|
||||
{
|
||||
_logger = logger;
|
||||
_librarySettings = librarySettings.Value;
|
||||
var librarySettings1 = librarySettings.Value;
|
||||
_dbContextFactory = contextFactory;
|
||||
_cacheService = cacheService;
|
||||
_libraryDirectory = Directory.CreateDirectory(_librarySettings.Path);
|
||||
_libraryDirectory = Directory.CreateDirectory(librarySettings1.Path);
|
||||
logger.LogDebug("Library directory: {LibraryWorkingDir}", _libraryDirectory.FullName);
|
||||
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirMedia));
|
||||
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirChannels));
|
||||
Directory.CreateDirectory(Path.Combine(librarySettings1.Path, LibraryConstants.Directories.SubDirMedia));
|
||||
Directory.CreateDirectory(Path.Combine(librarySettings1.Path, LibraryConstants.Directories.SubDirChannels));
|
||||
}
|
||||
|
||||
private async Task AddWebImagesAsync(LibraryDbContext context, List<WebImage> images, string foreignKey, string libSubDir, string fileType, string subDir)
|
||||
@@ -102,6 +101,7 @@ public class LibraryService : ILibraryService
|
||||
}
|
||||
else
|
||||
{
|
||||
context.HttpCookies.RemoveRange(context.HttpCookies.Where(x => x.ClientId == client.Id));
|
||||
context.ClientAccounts.Add(dbClient);
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ public class LibraryService : ILibraryService
|
||||
try
|
||||
{
|
||||
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var channel = await context.Channels
|
||||
var channel = await context.Channels.AsSplitQuery()
|
||||
.Include(c => c.ClientAccount)
|
||||
.ThenInclude(p => p!.HttpCookies)
|
||||
.Include(f => f.Files)
|
||||
|
||||
@@ -31,16 +31,15 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
|
||||
}
|
||||
}
|
||||
|
||||
private async void CancellationRequested()
|
||||
private void CancellationRequested()
|
||||
{
|
||||
foreach (var client in _loadedClients)
|
||||
{
|
||||
await SaveClientAsync(client);
|
||||
client.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<YouTubeClientItem>> GetClientsAsync(string search, int offset = 0, int limit = 10, CancellationToken cancellationToken = default)
|
||||
public async Task<ListResult<YouTubeClientItem>> GetClientsAsync(string? search, int offset = 0, int limit = 10, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_libraryService == null)
|
||||
{
|
||||
|
||||
58
Manager.YouTube/Interpreter/JavaScriptEngineManager.cs
Normal file
58
Manager.YouTube/Interpreter/JavaScriptEngineManager.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Text;
|
||||
using DotBased.Monads;
|
||||
|
||||
namespace Manager.YouTube.Interpreter;
|
||||
|
||||
public static class JavaScriptEngineManager
|
||||
{
|
||||
private static readonly PlayerEngineCollection Engines = [];
|
||||
|
||||
public static async Task<Result<PlayerEngine>> GetPlayerEngine(string playerUrl)
|
||||
{
|
||||
if (string.IsNullOrEmpty(playerUrl))
|
||||
{
|
||||
return ResultError.Fail("player url is empty or null!");
|
||||
}
|
||||
|
||||
var version = GetScriptVersion(playerUrl);
|
||||
|
||||
if (Engines.TryGetValue(version, out var engine))
|
||||
{
|
||||
return engine;
|
||||
}
|
||||
|
||||
var playerJsSourceResult = await DownloadPlayerScriptAsync(playerUrl);
|
||||
if (!playerJsSourceResult.IsSuccess)
|
||||
{
|
||||
return playerJsSourceResult.Error ?? ResultError.Fail("Download player script failed!");
|
||||
}
|
||||
|
||||
return new PlayerEngine(version, playerJsSourceResult.Value);
|
||||
}
|
||||
|
||||
private static string GetScriptVersion(string relativePlayerUrl)
|
||||
{
|
||||
var split = relativePlayerUrl.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
var v = split[2];
|
||||
var lang = split[4];
|
||||
return $"{v}-{lang}";
|
||||
}
|
||||
|
||||
private static async Task<Result<string>> DownloadPlayerScriptAsync(string relativeUrl, YouTubeClient? client = null)
|
||||
{
|
||||
var downloadRequest = new HttpRequestMessage(HttpMethod.Get, new Uri($"{NetworkService.Origin}/{relativeUrl}"));
|
||||
var downloadResponse = await NetworkService.DownloadBytesAsync(downloadRequest, client);
|
||||
if (!downloadResponse.IsSuccess)
|
||||
{
|
||||
return downloadResponse.Error ?? ResultError.Fail($"Failed to download script from url: {relativeUrl}");
|
||||
}
|
||||
|
||||
var playerJs = Encoding.UTF8.GetString(downloadResponse.Value.Data);
|
||||
if (string.IsNullOrWhiteSpace(playerJs))
|
||||
{
|
||||
return ResultError.Fail("Script value is empty!");
|
||||
}
|
||||
|
||||
return playerJs;
|
||||
}
|
||||
}
|
||||
42
Manager.YouTube/Interpreter/PlayerEngine.cs
Normal file
42
Manager.YouTube/Interpreter/PlayerEngine.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using DotBased.Logging;
|
||||
using Jint;
|
||||
|
||||
namespace Manager.YouTube.Interpreter;
|
||||
|
||||
public class PlayerEngine
|
||||
{
|
||||
public string Version { get; set; }
|
||||
public Engine JsEngine { get; set; }
|
||||
private ILogger Logger { get; set; }
|
||||
|
||||
public PlayerEngine(string version, string script)
|
||||
{
|
||||
if (string.IsNullOrEmpty(version))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(version));
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(script))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(script));
|
||||
}
|
||||
|
||||
Logger = LogService.RegisterLogger(typeof(PlayerEngine), version);
|
||||
Version = version;
|
||||
JsEngine = new Engine().Execute(script).SetValue("log", new Action<object>(obj =>
|
||||
{
|
||||
var logStr = obj.ToString();
|
||||
if (string.IsNullOrEmpty(logStr))
|
||||
{
|
||||
return;
|
||||
}
|
||||
Logger.Information(logStr);
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
public void InitializePlayer()
|
||||
{
|
||||
JsEngine.Execute("createPlayer");
|
||||
}
|
||||
}
|
||||
11
Manager.YouTube/Interpreter/PlayerEngineCollection.cs
Normal file
11
Manager.YouTube/Interpreter/PlayerEngineCollection.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Manager.YouTube.Interpreter;
|
||||
|
||||
public class PlayerEngineCollection : KeyedCollection<string, PlayerEngine>
|
||||
{
|
||||
protected override string GetKeyForItem(PlayerEngine item)
|
||||
{
|
||||
return item.Version;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DotBased" Version="1.0.0" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.12.2" />
|
||||
<PackageReference Include="Jint" Version="4.4.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -35,6 +35,9 @@ public class ClientState : AdditionalJsonData
|
||||
[JsonPropertyName("SERVER_VERSION")]
|
||||
public string? ServerVersion { get; set; }
|
||||
|
||||
[JsonPropertyName("PLAYER_JS_URL")]
|
||||
public string? PlayerJsUrl { get; set; }
|
||||
|
||||
[JsonPropertyName("INNERTUBE_CONTEXT")]
|
||||
public InnerTubeContext? InnerTubeContext { get; set; }
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Manager.YouTube.Models.Innertube;
|
||||
|
||||
public class ColorInfo
|
||||
{
|
||||
[JsonPropertyName("primaries")]
|
||||
public string Primaries { get; set; } = "";
|
||||
[JsonPropertyName("transferCharacteristics")]
|
||||
public string TransferCharacteristics { get; set; } = "";
|
||||
[JsonPropertyName("matrixCoefficients")]
|
||||
public string MatrixCoefficients { get; set; } = "";
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Manager.YouTube.Models.Innertube;
|
||||
|
||||
public class Range
|
||||
{
|
||||
[JsonPropertyName("start")]
|
||||
public uint Start { get; set; }
|
||||
[JsonPropertyName("end")]
|
||||
public uint End { get; set; }
|
||||
}
|
||||
@@ -1,10 +1,18 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Manager.YouTube.Util.Converters;
|
||||
|
||||
namespace Manager.YouTube.Models.Innertube;
|
||||
|
||||
public class StreamingData
|
||||
{
|
||||
public DateTime FetchedUtc { get; set; } = DateTime.UtcNow;
|
||||
[JsonPropertyName("expiresInSeconds")]
|
||||
public int ExpiresInSeconds { get; set; }
|
||||
[JsonPropertyName("serverAbrStreamingUrl")]
|
||||
[JsonConverter(typeof(JsonUrlEscapeConverter))]
|
||||
public string ServerAbrStreamingUrl { get; set; } = "";
|
||||
[JsonPropertyName("formats")]
|
||||
public List<StreamingFormat> Formats { get; set; } = [];
|
||||
[JsonPropertyName("adaptiveFormats")]
|
||||
public List<StreamingFormat> AdaptiveFormats { get; set; } = [];
|
||||
}
|
||||
@@ -1,30 +1,59 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Manager.YouTube.Models.Innertube;
|
||||
|
||||
public class StreamingFormat
|
||||
{
|
||||
[JsonPropertyName("itag")]
|
||||
public int Itag { get; set; }
|
||||
public string Url { get; set; } = "";
|
||||
[JsonPropertyName("url")]
|
||||
public string? Url { get; set; }
|
||||
[JsonPropertyName("mimeType")]
|
||||
public string MimeType { get; set; } = "";
|
||||
[JsonPropertyName("bitrate")]
|
||||
public uint Bitrate { get; set; }
|
||||
[JsonPropertyName("width")]
|
||||
public uint? Width { get; set; }
|
||||
[JsonPropertyName("height")]
|
||||
public uint? Height { get; set; }
|
||||
[JsonPropertyName("initRange")]
|
||||
public Range? InitRange { get; set; }
|
||||
[JsonPropertyName("indexRange")]
|
||||
public Range? IndexRange { get; set; }
|
||||
[JsonPropertyName("lastModified")]
|
||||
public long LastModified { get; set; }
|
||||
[JsonPropertyName("contentLength")]
|
||||
public long ContentLength { get; set; }
|
||||
[JsonPropertyName("quality")]
|
||||
public string Quality { get; set; } = "";
|
||||
[JsonPropertyName("xtags")]
|
||||
public string? Xtags { get; set; }
|
||||
[JsonPropertyName("fps")]
|
||||
public uint Fps { get; set; }
|
||||
[JsonPropertyName("qualityLabel")]
|
||||
public string QualityLabel { get; set; } = "";
|
||||
[JsonPropertyName("projectionType")]
|
||||
public string ProjectionType { get; set; } = "";
|
||||
[JsonPropertyName("averagebitrate")]
|
||||
public uint? AverageBitrate { get; set; }
|
||||
[JsonPropertyName("highReplication")]
|
||||
public bool? HighReplication { get; set; }
|
||||
[JsonPropertyName("colorInfo")]
|
||||
public ColorInfo? ColorInfo { get; set; }
|
||||
[JsonPropertyName("audioQuality")]
|
||||
public string? AudioQuality { get; set; } = "";
|
||||
[JsonPropertyName("approxDurationMs")]
|
||||
public long ApproxDurationMs { get; set; }
|
||||
[JsonPropertyName("audioSampleRate")]
|
||||
public int? AudioSampleRate { get; set; }
|
||||
[JsonPropertyName("audioChannels")]
|
||||
public int? AudioChannels { get; set; }
|
||||
[JsonPropertyName("loudnessDb")]
|
||||
public double? LoudnessDb { get; set; }
|
||||
[JsonPropertyName("isDrc")]
|
||||
public bool? IsDrc { get; set; }
|
||||
[JsonPropertyName("signatureCipher")]
|
||||
public string? SignatureCipher { get; set; }
|
||||
[JsonPropertyName("qualityOrdinal")]
|
||||
public string QualityOrdinal { get; set; } = "";
|
||||
}
|
||||
@@ -1,8 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Manager.YouTube.Models.Innertube;
|
||||
|
||||
public class WebImage
|
||||
{
|
||||
[JsonPropertyName("width")]
|
||||
public int Width { get; set; }
|
||||
[JsonPropertyName("height")]
|
||||
public int Height { get; set; }
|
||||
[JsonPropertyName("url")]
|
||||
public string Url { get; set; } = "";
|
||||
}
|
||||
@@ -4,6 +4,6 @@ namespace Manager.YouTube.Models.Parser;
|
||||
|
||||
public class YouTubeVideoData
|
||||
{
|
||||
public JsonObject? YouTubePlayerData { get; set; }
|
||||
public JsonObject? YouTubeInitialData { get; set; }
|
||||
public JsonNode? YouTubePlayerData { get; set; }
|
||||
public JsonNode? YouTubeInitialData { get; set; }
|
||||
}
|
||||
13
Manager.YouTube/Models/Playlist/PlaylistVideo.cs
Normal file
13
Manager.YouTube/Models/Playlist/PlaylistVideo.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Manager.YouTube.Models.Innertube;
|
||||
|
||||
namespace Manager.YouTube.Models.Playlist;
|
||||
|
||||
public class PlaylistVideo
|
||||
{
|
||||
public required string VideoId { get; set; }
|
||||
public List<WebImage> Thumbnails { get; set; } = [];
|
||||
public required string Title { get; set; }
|
||||
public required string Author { get; set; }
|
||||
public long LengthSeconds { get; set; }
|
||||
public bool IsPlayable { get; set; }
|
||||
}
|
||||
19
Manager.YouTube/Models/YouTubePlaylist.cs
Normal file
19
Manager.YouTube/Models/YouTubePlaylist.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Manager.YouTube.Models.Playlist;
|
||||
|
||||
namespace Manager.YouTube.Models;
|
||||
|
||||
public class YouTubePlaylist
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required string Title { get; set; }
|
||||
public required string Description { get; set; }
|
||||
public required string Owner { get; set; }
|
||||
public required string OwnerId { get; set; }
|
||||
public bool NoIndex { get; set; }
|
||||
public bool Unlisted { get; set; }
|
||||
public bool CanReorder { get; set; }
|
||||
public bool IsEditable { get; set; }
|
||||
public List<PlaylistVideo> Videos { get; set; } = [];
|
||||
|
||||
public string? ContinuationToken { get; set; }
|
||||
}
|
||||
@@ -8,7 +8,7 @@ public static class NetworkService
|
||||
public const string Origin = "https://www.youtube.com";
|
||||
private static readonly HttpClient HttpClient = new();
|
||||
|
||||
public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client, bool skipAuthenticationHeader = false)
|
||||
public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client, bool skipAuthenticationHeader = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
request.Headers.Add("Origin", Origin);
|
||||
request.Headers.UserAgent.ParseAdd(client.UserAgent);
|
||||
@@ -19,8 +19,8 @@ public static class NetworkService
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.HttpClient.SendAsync(request);
|
||||
var contentString = await response.Content.ReadAsStringAsync();
|
||||
var response = await client.HttpClient.SendAsync(request, cancellationToken);
|
||||
var contentString = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return ResultError.Fail(contentString);
|
||||
|
||||
@@ -74,8 +74,8 @@ public static class HtmlParser
|
||||
{
|
||||
return new YouTubeVideoData
|
||||
{
|
||||
YouTubePlayerData = parsedPlayerInitialData?.AsObject(),
|
||||
YouTubeInitialData = parsedInitialData?.AsObject()
|
||||
YouTubePlayerData = parsedPlayerInitialData,
|
||||
YouTubeInitialData = parsedInitialData
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -12,45 +12,58 @@ public static class JsonParser
|
||||
.Select(image => new WebImage { Width = image.GetProperty("width").GetInt32(), Height = image.GetProperty("height").GetInt32(), Url = image.GetProperty("url").GetString() ?? "" })
|
||||
.ToList();
|
||||
|
||||
public static string ExtractTextOrHtml(JsonElement element)
|
||||
public static string ExtractTextOrHtml(JsonNode? node)
|
||||
{
|
||||
if (node is not JsonObject nodeObj)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
// Case 1: Simple text (no formatting)
|
||||
if (element.TryGetProperty("simpleText", out var simpleText))
|
||||
return simpleText.GetString() ?? string.Empty;
|
||||
if (nodeObj.TryGetPropertyValue("simpleText", out var simpleText))
|
||||
return simpleText?.GetValue<string>() ?? string.Empty;
|
||||
|
||||
// Case 2: Runs (formatted text segments)
|
||||
if (element.TryGetProperty("runs", out var runs) && runs.ValueKind == JsonValueKind.Array)
|
||||
if (nodeObj.TryGetPropertyValue("runs", out var runs) && runs != null && runs.GetValueKind() == JsonValueKind.Array)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
foreach (var run in runs.EnumerateArray())
|
||||
foreach (var runNode in runs.AsArray())
|
||||
{
|
||||
var text = run.GetProperty("text").GetString() ?? string.Empty;
|
||||
if (runNode is not JsonObject run)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var text = runNode["text"]?.GetValue<string>() ?? string.Empty;
|
||||
var formatted = System.Net.WebUtility.HtmlEncode(text);
|
||||
|
||||
var bold = run.TryGetProperty("bold", out var boldProp) && boldProp.GetBoolean();
|
||||
var italic = run.TryGetProperty("italic", out var italicProp) && italicProp.GetBoolean();
|
||||
var underline = run.TryGetProperty("underline", out var underlineProp) && underlineProp.GetBoolean();
|
||||
var strikethrough = run.TryGetProperty("strikethrough", out var strikeProp) && strikeProp.GetBoolean();
|
||||
var bold = run.TryGetPropertyValue("bold", out var boldNode) && boldNode is JsonValue bv && bv.GetValue<bool>();
|
||||
|
||||
var italic = run.TryGetPropertyValue("italic", out var italicNode) && italicNode is JsonValue iv && iv.GetValue<bool>();
|
||||
|
||||
var underline = run.TryGetPropertyValue("underline", out var underlineNode) && underlineNode is JsonValue uv && uv.GetValue<bool>();
|
||||
|
||||
var strikethrough = run.TryGetPropertyValue("strikethrough", out var strikeNode) && strikeNode is JsonValue sv && sv.GetValue<bool>();
|
||||
|
||||
if (bold) formatted = $"<b>{formatted}</b>";
|
||||
if (italic) formatted = $"<i>{formatted}</i>";
|
||||
if (underline) formatted = $"<u>{formatted}</u>";
|
||||
if (strikethrough) formatted = $"<s>{formatted}</s>";
|
||||
|
||||
if (run.TryGetProperty("navigationEndpoint", out var nav) &&
|
||||
nav.TryGetProperty("url", out var urlProp))
|
||||
if (run.TryGetPropertyValue("navigationEndpoint", out var nav) && nav is JsonObject navObj &&
|
||||
navObj.TryGetPropertyValue("url", out var urlProp))
|
||||
{
|
||||
var url = urlProp.GetString();
|
||||
var url = urlProp?.GetValue<string>();
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
formatted = $"<a href=\"{url}\">{formatted}</a>";
|
||||
}
|
||||
|
||||
if (run.TryGetProperty("emoji", out var emoji) && emoji.ValueKind == JsonValueKind.Object)
|
||||
if (run.TryGetPropertyValue("emoji", out var emoji) && emoji is JsonObject emojiObj)
|
||||
{
|
||||
if (emoji.TryGetProperty("url", out var emojiUrl))
|
||||
if (emojiObj.TryGetPropertyValue("url", out var emojiUrl))
|
||||
{
|
||||
var src = emojiUrl.GetString();
|
||||
var src = emojiUrl?.GetValue<string>();
|
||||
if (!string.IsNullOrEmpty(src))
|
||||
formatted = $"<img src=\"{src}\" alt=\"{text}\" class=\"emoji\" />";
|
||||
}
|
||||
@@ -65,9 +78,14 @@ public static class JsonParser
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public static List<WebImage> ExtractWebImages(JsonElement element)
|
||||
public static List<WebImage> ExtractWebImages(JsonNode? node)
|
||||
{
|
||||
var thumbnailsArray = element.GetProperty("thumbnail").GetProperty("thumbnails");
|
||||
return thumbnailsArray.Deserialize<List<WebImage>>() ?? [];
|
||||
if (node == null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var thumbnailsArray = node["thumbnails"];
|
||||
return thumbnailsArray?.Deserialize<List<WebImage>>() ?? [];
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public static class VideoJsonParser
|
||||
|
||||
public static Result<YouTubeVideo> ParseVideoData(YouTubeVideoData videoData)
|
||||
{
|
||||
if (videoData.YouTubeInitialData == null || videoData.YouTubeInitialData.Count == 0)
|
||||
if (videoData.YouTubePlayerData == null)
|
||||
{
|
||||
return ResultError.Fail("No initial video data found!");
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public static class VideoJsonParser
|
||||
YouTubeVideo? video;
|
||||
try
|
||||
{
|
||||
video = videoData.YouTubeInitialData.Deserialize<YouTubeVideo>(VideoParserOptions);
|
||||
video = videoData.YouTubePlayerData.Deserialize<YouTubeVideo>(VideoParserOptions);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
125
Manager.YouTube/Util/Cipher/CipherDecoder.cs
Normal file
125
Manager.YouTube/Util/Cipher/CipherDecoder.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System.Collections.Frozen;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Manager.YouTube.Util.Cipher.Operations;
|
||||
|
||||
namespace Manager.YouTube.Util.Cipher;
|
||||
|
||||
public partial class CipherDecoder
|
||||
{
|
||||
public required string Version { get; init; }
|
||||
public readonly IReadOnlySet<ICipherOperation> Operations;
|
||||
|
||||
private CipherDecoder(IEnumerable<ICipherOperation> operations)
|
||||
{
|
||||
Operations = operations.ToFrozenSet();
|
||||
if (Operations.Count == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(operations), "No decipher operations given.");
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<CipherDecoder> CreateAsync(string relativeUrl, string version, YouTubeClient? client = null)
|
||||
{
|
||||
var operations = await GetCipherOperations(relativeUrl, client);
|
||||
var decoder = new CipherDecoder(operations)
|
||||
{
|
||||
Version = version
|
||||
};
|
||||
return decoder;
|
||||
}
|
||||
|
||||
public string Decipher(string? signatureCipher)
|
||||
{
|
||||
if (string.IsNullOrEmpty(signatureCipher))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
var urlBuilder = new StringBuilder();
|
||||
|
||||
var indexStart = signatureCipher.IndexOf("s=", StringComparison.Ordinal);
|
||||
var indexEnd = signatureCipher.IndexOf("&", StringComparison.Ordinal);
|
||||
var signature = signatureCipher.Substring(indexStart, indexEnd);
|
||||
|
||||
indexStart = signatureCipher.IndexOf("&sp", StringComparison.Ordinal);
|
||||
indexEnd = signatureCipher.IndexOf("&url", StringComparison.Ordinal);
|
||||
var spParam = signatureCipher.Substring(indexStart, indexEnd - indexStart);
|
||||
|
||||
indexStart = signatureCipher.IndexOf("&url", StringComparison.Ordinal);
|
||||
var videoUrl = signatureCipher[indexStart..];
|
||||
|
||||
|
||||
signature = signature[(signature.IndexOf('=') + 1)..];
|
||||
spParam = spParam[(spParam.IndexOf('=') + 1)..];
|
||||
videoUrl = videoUrl[(videoUrl.IndexOf('=') + 1)..];
|
||||
if (string.IsNullOrWhiteSpace(signature))
|
||||
{
|
||||
throw new InvalidOperationException("Invalid signature.");
|
||||
}
|
||||
var signatureDeciphered = Operations.Aggregate(signature, (acc, op) => op.Decipher(acc));
|
||||
|
||||
urlBuilder.Append(videoUrl);
|
||||
urlBuilder.Append($"&{spParam}=");
|
||||
urlBuilder.Append(signatureDeciphered);
|
||||
return urlBuilder.ToString();
|
||||
}
|
||||
|
||||
private static async Task<IEnumerable<ICipherOperation>> GetCipherOperations(string relativeUrl, YouTubeClient? client = null)
|
||||
{
|
||||
var downloadRequest = new HttpRequestMessage(HttpMethod.Get, new Uri($"{NetworkService.Origin}/{relativeUrl}"));
|
||||
var downloadResponse = await NetworkService.DownloadBytesAsync(downloadRequest, client);
|
||||
if (!downloadResponse.IsSuccess)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var playerJs = Encoding.UTF8.GetString(downloadResponse.Value.Data);
|
||||
if (string.IsNullOrWhiteSpace(playerJs))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var functionBody = FunctionBodyRegex().Match(playerJs).Groups[0].ToString();
|
||||
var definitionBody = DefinitionBodyRegex().Match(functionBody).Groups[1].Value;
|
||||
var decipherDefinition = Regex.Match(playerJs, $@"var\s+{definitionBody}=\{{(\w+:function\(\w+(,\w+)?\)\{{(.*?)\}}),?\}};", RegexOptions.Singleline).Groups[0].ToString();
|
||||
|
||||
SortedSet<ICipherOperation> operations = [];
|
||||
foreach (var statement in functionBody.Split(';'))
|
||||
{
|
||||
// Get the name of the function called in this statement
|
||||
var calledFuncName = StatementFunctionNameRegex().Match(statement).Groups[1].Value;
|
||||
if (string.IsNullOrWhiteSpace(calledFuncName))
|
||||
continue;
|
||||
|
||||
if (Regex.IsMatch(decipherDefinition, $@"{Regex.Escape(calledFuncName)}:\bfunction\b\([a],b\).(\breturn\b)?.?\w+\."))
|
||||
{
|
||||
var index = int.Parse(OperationIndexRegex().Match(statement).Groups[1].Value);
|
||||
operations.Add(new CipherSlice(index));
|
||||
}
|
||||
else if (Regex.IsMatch(decipherDefinition, $@"{Regex.Escape(calledFuncName)}:\bfunction\b\(\w+\,\w\).\bvar\b.\bc=a\b"))
|
||||
{
|
||||
var index = int.Parse(OperationIndexRegex().Match(statement).Groups[1].Value);
|
||||
operations.Add(new CipherSwap(index));
|
||||
}
|
||||
else if (Regex.IsMatch(decipherDefinition, $@"{Regex.Escape(calledFuncName)}:\bfunction\b\(\w+\)"))
|
||||
{
|
||||
operations.Add(new CipherReverse());
|
||||
}
|
||||
}
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
|
||||
[GeneratedRegex(@"([A-Za-z_$][A-Za-z0-9_$]*)=function\([A-Za-z_$][A-Za-z0-9_$]*\)\{\s*([A-Za-z_$][A-Za-z0-9_$]*)=\2\.split\(\x22\x22\);[\s\S]*?return\s+\2\.join\(\x22\x22\)\s*\}")]
|
||||
private static partial Regex FunctionBodyRegex();
|
||||
[GeneratedRegex("([\\$_\\w]+).\\w+\\(\\w+,\\d+\\);")]
|
||||
private static partial Regex DefinitionBodyRegex();
|
||||
|
||||
|
||||
[GeneratedRegex(@"\(\w+,(\d+)\)")]
|
||||
private static partial Regex OperationIndexRegex();
|
||||
[GeneratedRegex(@"\w+(?:.|\[)(\""?\w+(?:\"")?)\]?\(")]
|
||||
private static partial Regex StatementFunctionNameRegex();
|
||||
}
|
||||
11
Manager.YouTube/Util/Cipher/CipherDecoderCollection.cs
Normal file
11
Manager.YouTube/Util/Cipher/CipherDecoderCollection.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Manager.YouTube.Util.Cipher;
|
||||
|
||||
public class CipherDecoderCollection : KeyedCollection<string, CipherDecoder>
|
||||
{
|
||||
protected override string GetKeyForItem(CipherDecoder item)
|
||||
{
|
||||
return item.Version;
|
||||
}
|
||||
}
|
||||
47
Manager.YouTube/Util/Cipher/CipherManager.cs
Normal file
47
Manager.YouTube/Util/Cipher/CipherManager.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using DotBased.Logging;
|
||||
using DotBased.Monads;
|
||||
using Manager.YouTube.Models.Innertube;
|
||||
|
||||
namespace Manager.YouTube.Util.Cipher;
|
||||
|
||||
public static class CipherManager
|
||||
{
|
||||
private static readonly CipherDecoderCollection LoadedCiphers = [];
|
||||
private static readonly ILogger Logger = LogService.RegisterLogger(typeof(CipherManager));
|
||||
|
||||
public static async Task<Result<CipherDecoder>> GetDecoderAsync(ClientState clientState, YouTubeClient? client = null)
|
||||
{
|
||||
var relativePlayerJsUrl = clientState.PlayerJsUrl;
|
||||
if (string.IsNullOrEmpty(relativePlayerJsUrl))
|
||||
{
|
||||
return ResultError.Fail("Could not get player js url.");
|
||||
}
|
||||
var version = GetCipherVersion(relativePlayerJsUrl);
|
||||
|
||||
Logger.Debug($"Getting cipher decoder for version: {version}");
|
||||
if (LoadedCiphers.TryGetValue(version, out var cipher))
|
||||
{
|
||||
return cipher;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var decoder = await CipherDecoder.CreateAsync(relativePlayerJsUrl, version, client);
|
||||
LoadedCiphers.Add(decoder);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error(e, "Could not create cipher decoder. Version: {DecoderVersion}", version);
|
||||
}
|
||||
|
||||
return ResultError.Fail($"Could not create cipher decoder for {relativePlayerJsUrl} (v: {version})");
|
||||
}
|
||||
|
||||
private static string GetCipherVersion(string relativePlayerUrl)
|
||||
{
|
||||
var split = relativePlayerUrl.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
var v = split[2];
|
||||
var lang = split[4];
|
||||
return $"{v}_{lang}";
|
||||
}
|
||||
}
|
||||
18
Manager.YouTube/Util/Cipher/Operations/CipherReverse.cs
Normal file
18
Manager.YouTube/Util/Cipher/Operations/CipherReverse.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Manager.YouTube.Util.Cipher.Operations;
|
||||
|
||||
public class CipherReverse : ICipherOperation
|
||||
{
|
||||
public string Decipher(string cipherSignature)
|
||||
{
|
||||
var buffer = new StringBuilder(cipherSignature.Length);
|
||||
|
||||
for (var i = cipherSignature.Length - 1; i >= 0; i--)
|
||||
{
|
||||
buffer.Append(cipherSignature[i]);
|
||||
}
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
}
|
||||
6
Manager.YouTube/Util/Cipher/Operations/CipherSlice.cs
Normal file
6
Manager.YouTube/Util/Cipher/Operations/CipherSlice.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Manager.YouTube.Util.Cipher.Operations;
|
||||
|
||||
public class CipherSlice(int indexToSlice) : ICipherOperation
|
||||
{
|
||||
public string Decipher(string cipherSignature) => cipherSignature[indexToSlice..];
|
||||
}
|
||||
12
Manager.YouTube/Util/Cipher/Operations/CipherSwap.cs
Normal file
12
Manager.YouTube/Util/Cipher/Operations/CipherSwap.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Manager.YouTube.Util.Cipher.Operations;
|
||||
|
||||
public class CipherSwap(int indexToSwap) : ICipherOperation
|
||||
{
|
||||
public string Decipher(string cipherSignature) => new StringBuilder(cipherSignature)
|
||||
{
|
||||
[0] = cipherSignature[indexToSwap],
|
||||
[indexToSwap] = cipherSignature[0]
|
||||
}.ToString();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Manager.YouTube.Util.Cipher.Operations;
|
||||
|
||||
public interface ICipherOperation
|
||||
{
|
||||
string Decipher(string cipherSignature);
|
||||
}
|
||||
27
Manager.YouTube/Util/Converters/JsonUrlEscapeConverter.cs
Normal file
27
Manager.YouTube/Util/Converters/JsonUrlEscapeConverter.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Manager.YouTube.Util.Converters;
|
||||
|
||||
public partial class JsonUrlEscapeConverter : JsonConverter<string>
|
||||
{
|
||||
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var url = reader.GetString();
|
||||
if (string.IsNullOrWhiteSpace(url))
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
return UrlPatternRegex().IsMatch(url) ? Uri.UnescapeDataString(url) : url;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value);
|
||||
}
|
||||
|
||||
[GeneratedRegex("^(https?|ftp)://", RegexOptions.IgnoreCase | RegexOptions.Compiled, "nl-NL")]
|
||||
private static partial Regex UrlPatternRegex();
|
||||
}
|
||||
39
Manager.YouTube/Util/Converters/NumericJsonConverter.cs
Normal file
39
Manager.YouTube/Util/Converters/NumericJsonConverter.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Manager.YouTube.Util.Converters;
|
||||
|
||||
public class NumericJsonConverter<T> : JsonConverter<T> where T : struct, IConvertible
|
||||
{
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Number)
|
||||
{
|
||||
// Direct numeric value
|
||||
return (T)Convert.ChangeType(reader.GetDouble(), typeof(T));
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
var str = reader.GetString();
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
throw new JsonException("Empty string cannot be converted to a number.");
|
||||
|
||||
return (T)Convert.ChangeType(str, typeof(T));
|
||||
}
|
||||
|
||||
throw new JsonException($"Unexpected token {reader.TokenType} for type {typeof(T)}.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JsonException($"Error converting value to {typeof(T)}.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteNumberValue(Convert.ToDouble(value));
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
using DotBased.Logging;
|
||||
using Manager.YouTube.Models;
|
||||
@@ -10,50 +12,74 @@ namespace Manager.YouTube.Util.Converters;
|
||||
public class YouTubeVideoJsonConverter : JsonConverter<YouTubeVideo>
|
||||
{
|
||||
private readonly ILogger _logger = LogService.RegisterLogger<YouTubeVideoJsonConverter>();
|
||||
private readonly JsonSerializerOptions _serializerOptions = new()
|
||||
{
|
||||
Converters = {
|
||||
new NumericJsonConverter<int>(),
|
||||
new NumericJsonConverter<uint>(),
|
||||
new NumericJsonConverter<long>(),
|
||||
new NumericJsonConverter<double>(),
|
||||
new NumericJsonConverter<decimal>() },
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public override YouTubeVideo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
using var document = JsonDocument.ParseValue(ref reader);
|
||||
var root = document.RootElement;
|
||||
var node = JsonNode.Parse(ref reader);
|
||||
if (node == null)
|
||||
{
|
||||
throw new SerializationException("Failed to parse JSON reader.");
|
||||
}
|
||||
|
||||
var playabilityStatus = root.GetProperty("playabilityStatus");
|
||||
var streamingData = root.GetProperty("streamingData");
|
||||
var videoDetails = root.GetProperty("videoDetails");
|
||||
var playerConfigJson = root.GetProperty("playerConfig");
|
||||
var microformat = root.GetProperty("microformat").GetProperty("playerMicroformatRenderer");
|
||||
var rootObject = node.AsObject();
|
||||
|
||||
var thumbnails = JsonParser.ExtractWebImages(videoDetails.GetProperty("thumbnail"));
|
||||
thumbnails.AddRange(JsonParser.ExtractWebImages(microformat.GetProperty("thumbnail")));
|
||||
var playabilityStatus = rootObject["playabilityStatus"];
|
||||
var streamingDataJson = rootObject["streamingData"];
|
||||
var videoDetails = rootObject["videoDetails"];
|
||||
var playerConfigJson = rootObject["playerConfig"];
|
||||
var microformat = rootObject["microformat"]?["playerMicroformatRenderer"];
|
||||
|
||||
var videoId = videoDetails?["videoId"]?.GetValue<string>() ?? microformat?["externalVideoId"]?.GetValue<string>();
|
||||
if (string.IsNullOrEmpty(videoId))
|
||||
{
|
||||
throw new SerializationException("Failed to get videoId");
|
||||
}
|
||||
|
||||
var thumbnails = JsonParser.ExtractWebImages(videoDetails?["thumbnail"]);
|
||||
thumbnails.AddRange(JsonParser.ExtractWebImages(microformat?["thumbnail"]));
|
||||
|
||||
var streamingData = streamingDataJson.Deserialize<StreamingData>(_serializerOptions);
|
||||
var playerConfig = ExtractPlayerConfig(playerConfigJson);
|
||||
|
||||
var video = new YouTubeVideo
|
||||
{
|
||||
VideoId = videoDetails.GetProperty("videoId").GetString() ?? "",
|
||||
Title = JsonParser.ExtractTextOrHtml(microformat.GetProperty("title")),
|
||||
Description = JsonParser.ExtractTextOrHtml(microformat.GetProperty("description")),
|
||||
ViewCount = videoDetails.GetProperty("viewCount").GetInt32(),
|
||||
LikeCount = videoDetails.GetProperty("likeCount").GetInt32(),
|
||||
ChannelId = videoDetails.GetProperty("channelId").GetString() ?? "",
|
||||
Author = JsonParser.ExtractTextOrHtml(videoDetails.GetProperty("author")),
|
||||
PlayabilityStatus = playabilityStatus.GetProperty("status").GetString() ?? "",
|
||||
LengthSeconds = videoDetails.GetProperty("lengthSeconds").GetInt32(),
|
||||
Keywords = videoDetails.GetProperty("keywords").EnumerateArray().Select(v => v.GetString()).Cast<string>().ToArray(),
|
||||
IsOwnerViewing = videoDetails.GetProperty("isOwnerViewing").GetBoolean(),
|
||||
AllowRating = videoDetails.GetProperty("allowRating").GetBoolean(),
|
||||
IsCrawlable = videoDetails.GetProperty("isCrawlable").GetBoolean(),
|
||||
IsPrivate = videoDetails.GetProperty("isPrivate").GetBoolean(),
|
||||
IsUnpluggedCorpus = videoDetails.GetProperty("isUnpluggedCorpus").GetBoolean(),
|
||||
IsLive = videoDetails.GetProperty("isLiveContent").GetBoolean(),
|
||||
IsFamilySave = microformat.GetProperty("isFamilySave").GetBoolean(),
|
||||
AvailableCountries = microformat.GetProperty("availableCountries").EnumerateArray().Select(v => v.GetString()).Cast<string>().ToArray(),
|
||||
IsUnlisted = microformat.GetProperty("isUnlisted").GetBoolean(),
|
||||
HasYpcMetadata = microformat.GetProperty("hasYpcMetadata").GetBoolean(),
|
||||
PublishDate = microformat.GetProperty("publishDate").GetDateTime(),
|
||||
UploadDate = microformat.GetProperty("uploadDate").GetDateTime(),
|
||||
IsShortsEligible = microformat.GetProperty("isShortsEligible").GetBoolean(),
|
||||
Category = microformat.GetProperty("category").GetString() ?? "",
|
||||
StreamingData = streamingData.Deserialize<StreamingData>(),
|
||||
VideoId = videoId,
|
||||
Title = JsonParser.ExtractTextOrHtml(microformat?["title"]),
|
||||
Description = JsonParser.ExtractTextOrHtml(microformat?["description"]),
|
||||
ViewCount = long.TryParse(microformat?["viewCount"]?.GetValue<string>(), out var viewCountParsed) ? viewCountParsed : -1,
|
||||
LikeCount = long.TryParse(microformat?["likeCount"]?.GetValue<string>(), out var likeCountParsed) ? likeCountParsed : -1,
|
||||
ChannelId = videoDetails?["channelId"]?.GetValue<string>() ?? "",
|
||||
Author = videoDetails?["author"]?.GetValue<string>() ?? "",
|
||||
PlayabilityStatus = playabilityStatus?["status"]?.GetValue<string>() ?? "",
|
||||
LengthSeconds = long.TryParse(videoDetails?["lengthSeconds"]?.GetValue<string>(), out var lengthSecondsParsed) ? lengthSecondsParsed : -1,
|
||||
Keywords = videoDetails?["keywords"]?.AsArray().Select(v => v?.GetValue<string>() ?? "").ToArray() ?? [],
|
||||
IsOwnerViewing = videoDetails?["isOwnerViewing"]?.GetValue<bool>() ?? false,
|
||||
AllowRating = videoDetails?["allowRating"]?.GetValue<bool>() ?? false,
|
||||
IsCrawlable = videoDetails?["isCrawlable"]?.GetValue<bool>() ?? false,
|
||||
IsPrivate = videoDetails?["isPrivate"]?.GetValue<bool>() ?? false,
|
||||
IsUnpluggedCorpus = videoDetails?["isUnpluggedCorpus"]?.GetValue<bool>() ?? false,
|
||||
IsLive = videoDetails?["isLiveContent"]?.GetValue<bool>() ?? false,
|
||||
IsFamilySave = microformat?["isFamilySave"]?.GetValue<bool>() ?? false,
|
||||
AvailableCountries = microformat?["availableCountries"]?.AsArray().Select(v => v?.GetValue<string>() ?? "").ToArray() ?? [],
|
||||
IsUnlisted = microformat?["isUnlisted"]?.GetValue<bool>() ?? false,
|
||||
HasYpcMetadata = microformat?["hasYpcMetadata"]?.GetValue<bool>() ?? false,
|
||||
PublishDate = DateTime.TryParse(microformat?["publishDate"]?.GetValue<string>(), out var parsedPublishDate) ? parsedPublishDate : DateTime.MinValue,
|
||||
UploadDate = DateTime.TryParse(microformat?["uploadDate"]?.GetValue<string>(), out var parsedUploadDate) ? parsedUploadDate : DateTime.MinValue,
|
||||
IsShortsEligible = microformat?["isShortsEligible"]?.GetValue<bool>() ?? false,
|
||||
Category = microformat?["category"]?.GetValue<string>() ?? "",
|
||||
StreamingData = streamingData,
|
||||
Thumbnails = thumbnails,
|
||||
PlayerConfig = ExtractPlayerConfig(playerConfigJson)
|
||||
PlayerConfig = playerConfig
|
||||
};
|
||||
|
||||
return video;
|
||||
@@ -64,23 +90,25 @@ public class YouTubeVideoJsonConverter : JsonConverter<YouTubeVideo>
|
||||
throw new NotImplementedException("Converter only supports reading.");
|
||||
}
|
||||
|
||||
private PlayerConfig? ExtractPlayerConfig(JsonElement element)
|
||||
private PlayerConfig? ExtractPlayerConfig(JsonNode? playerConfigNode)
|
||||
{
|
||||
if (playerConfigNode == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var playerConfigObj = playerConfigNode.AsObject();
|
||||
var playerConfig = new PlayerConfig
|
||||
{
|
||||
AudioLoudnessDb = element.GetProperty("audioConfig").GetProperty("loudnessDb").GetDouble(),
|
||||
AudioPerceptualLoudnessDb = element.GetProperty("audioConfig").GetProperty("perceptualLoudnessDb").GetDouble(),
|
||||
AudioEnablePerFormatLoudness = element.GetProperty("audioConfig").GetProperty("enablePerFormatLoudness")
|
||||
.GetBoolean(),
|
||||
MaxBitrate = element.GetProperty("streamSelectionConfig").GetProperty("maxBitrate").GetUInt32(),
|
||||
MaxReadAheadMediaTimeMs = element.GetProperty("mediaCommonConfig").GetProperty("dynamicReadaheadConfig")
|
||||
.GetProperty("maxReadAheadMediaTimeMs").GetUInt32(),
|
||||
MinReadAheadMediaTimeMs = element.GetProperty("mediaCommonConfig").GetProperty("dynamicReadaheadConfig")
|
||||
.GetProperty("minReadAheadMediaTimeMs").GetUInt32(),
|
||||
ReadAheadGrowthRateMs = element.GetProperty("mediaCommonConfig").GetProperty("dynamicReadaheadConfig")
|
||||
.GetProperty("readAheadGrowthRateMs").GetUInt32(),
|
||||
AudioLoudnessDb = playerConfigObj["audioConfig"]?["loudnessDb"]?.GetValue<double>() ?? 0,
|
||||
AudioPerceptualLoudnessDb = playerConfigObj["audioConfig"]?["perceptualLoudnessDb"]?.GetValue<double>() ?? 0,
|
||||
AudioEnablePerFormatLoudness = playerConfigObj["audioConfig"]?["enablePerFormatLoudness"]?.GetValue<bool>() ?? false,
|
||||
MaxBitrate = uint.TryParse(playerConfigObj["streamSelectionConfig"]?["maxBitrate"]?.GetValue<string>(), out var parsedMaxBitrate) ? parsedMaxBitrate : 0,
|
||||
MaxReadAheadMediaTimeMs = playerConfigObj["mediaCommonConfig"]?["dynamicReadaheadConfig"]?["maxReadAheadMediaTimeMs"]?.GetValue<uint>() ?? 0,
|
||||
MinReadAheadMediaTimeMs = playerConfigObj["mediaCommonConfig"]?["dynamicReadaheadConfig"]?["minReadAheadMediaTimeMs"]?.GetValue<uint>() ?? 0,
|
||||
ReadAheadGrowthRateMs = playerConfigObj["mediaCommonConfig"]?["dynamicReadaheadConfig"]?["readAheadGrowthRateMs"]?.GetValue<uint>() ?? 0,
|
||||
};
|
||||
return playerConfig;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ 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;
|
||||
@@ -14,20 +15,23 @@ namespace Manager.YouTube;
|
||||
public sealed class YouTubeClient : IDisposable
|
||||
{
|
||||
public string Id { get; private set; } = "";
|
||||
public string? UserAgent { get; set; }
|
||||
public bool IsAnonymous { get; }
|
||||
public string UserAgent { get; private set; }
|
||||
public bool IsAnonymous { get; private set; }
|
||||
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)
|
||||
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)
|
||||
{
|
||||
@@ -47,10 +51,13 @@ public sealed class YouTubeClient : IDisposable
|
||||
/// </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, null will create a new logger.</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection? cookies, string userAgent)
|
||||
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection? cookies, string userAgent, ILogger? logger = null)
|
||||
{
|
||||
var client = new YouTubeClient(cookies, userAgent);
|
||||
logger ??= LogService.RegisterLogger<YouTubeClient>();
|
||||
|
||||
var client = new YouTubeClient(cookies, userAgent, logger);
|
||||
var clientInitializeResult = await client.FetchClientDataAsync();
|
||||
if (!clientInitializeResult.IsSuccess)
|
||||
{
|
||||
@@ -60,112 +67,55 @@ public sealed class YouTubeClient : IDisposable
|
||||
return client;
|
||||
}
|
||||
|
||||
private HttpClientHandler GetHttpClientHandler()
|
||||
public void SetUserAgent(string userAgent)
|
||||
{
|
||||
var clientHandler = new HttpClientHandler
|
||||
if (string.IsNullOrWhiteSpace(userAgent))
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
||||
UseCookies = true,
|
||||
CookieContainer = CookieContainer
|
||||
};
|
||||
return clientHandler;
|
||||
_logger.Warning("UserAgent cannot be null or empty!");
|
||||
return;
|
||||
}
|
||||
UserAgent = userAgent;
|
||||
}
|
||||
|
||||
internal async Task<Result> FetchClientDataAsync()
|
||||
public async Task<Result<YouTubeVideo>> GetVideoByIdAsync(string videoId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (State is not { LoggedIn: true })
|
||||
if (string.IsNullOrWhiteSpace(videoId))
|
||||
{
|
||||
var state = await GetClientStateAsync();
|
||||
if (!state.IsSuccess)
|
||||
{
|
||||
return state;
|
||||
}
|
||||
return ResultError.Fail("Video id is empty!");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, new Uri($"{NetworkService.Origin}/watch?v={videoId}"));
|
||||
|
||||
var videoResponse = await NetworkService.MakeRequestAsync(request, this, true, cancellationToken);
|
||||
if (!videoResponse.IsSuccess && !string.IsNullOrWhiteSpace(videoResponse.Value))
|
||||
{
|
||||
var datasyncResult = await GetDatasyncIdsAsync();
|
||||
if (!datasyncResult.IsSuccess)
|
||||
{
|
||||
return datasyncResult;
|
||||
return videoResponse.Error ?? ResultError.Fail("Request failed!");
|
||||
}
|
||||
|
||||
foreach (var id in datasyncResult.Value)
|
||||
var html = videoResponse.Value;
|
||||
|
||||
var stateResult = GetClientStateFromHtml(html);
|
||||
var state = stateResult.Value;
|
||||
if (!stateResult.IsSuccess && State != null)
|
||||
{
|
||||
if (DatasyncIds.Contains(id))
|
||||
continue;
|
||||
DatasyncIds.Add(id);
|
||||
}
|
||||
state = State;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Id))
|
||||
var htmlParseResult = HtmlParser.GetVideoDataFromHtml(html);
|
||||
if (!htmlParseResult.IsSuccess)
|
||||
{
|
||||
var accountInfoResult = await GetCurrentAccountIdAsync();
|
||||
if (!accountInfoResult.IsSuccess)
|
||||
{
|
||||
return accountInfoResult;
|
||||
return htmlParseResult.Error ?? ResultError.Fail("Failed to parse HTML video data!");
|
||||
}
|
||||
|
||||
Id = accountInfoResult.Value;
|
||||
var videoParseResult = VideoJsonParser.ParseVideoData(htmlParseResult.Value);
|
||||
if (!videoParseResult.IsSuccess)
|
||||
{
|
||||
return videoParseResult;
|
||||
}
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
//await DecipherSignaturesAsync(videoParseResult.Value, state);
|
||||
|
||||
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();
|
||||
return videoParseResult.Value;
|
||||
}
|
||||
|
||||
public async Task<Result<InnertubeChannel>> GetChannelByIdAsync(string channelId)
|
||||
@@ -254,6 +204,132 @@ public sealed class YouTubeClient : IDisposable
|
||||
HttpClient.Dispose();
|
||||
}
|
||||
|
||||
private async Task<Result> FetchClientDataAsync()
|
||||
{
|
||||
if (State is not { LoggedIn: true })
|
||||
{
|
||||
var stateResult = await GetClientStateAsync();
|
||||
if (!stateResult.IsSuccess)
|
||||
{
|
||||
return stateResult;
|
||||
}
|
||||
}
|
||||
|
||||
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 HttpClientHandler GetHttpClientHandler()
|
||||
{
|
||||
var clientHandler = new HttpClientHandler
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
||||
UseCookies = true,
|
||||
CookieContainer = CookieContainer
|
||||
};
|
||||
return clientHandler;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (State is { LoggedIn: false })
|
||||
{
|
||||
_logger.Warning("Client is not logged in!");
|
||||
return ResultError.Fail("Client login failed!");
|
||||
}
|
||||
|
||||
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 = GetClientStateFromHtml(html);
|
||||
if (clientStateResult is { IsSuccess: false, Error: not null })
|
||||
{
|
||||
return clientStateResult.Error;
|
||||
}
|
||||
|
||||
State = clientStateResult.Value;
|
||||
IsAnonymous = !State.LoggedIn;
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
private Result<ClientState> GetClientStateFromHtml(string html)
|
||||
{
|
||||
var clientStateResult = HtmlParser.GetStateJson(html);
|
||||
if (clientStateResult is { IsSuccess: false, Error: not null })
|
||||
{
|
||||
return clientStateResult.Error;
|
||||
}
|
||||
|
||||
ClientState? state;
|
||||
try
|
||||
{
|
||||
state = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value.Item1);
|
||||
if (state != null)
|
||||
{
|
||||
state.IsPremiumUser = clientStateResult.Value.Item2;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return ResultError.Error(e, "Error while parsing JSON!");
|
||||
}
|
||||
|
||||
return state == null ? ResultError.Fail("Unable to parse client state!") : state;
|
||||
}
|
||||
|
||||
private async Task<Result<string>> GetCurrentAccountIdAsync()
|
||||
{
|
||||
if (State is not { LoggedIn: true })
|
||||
@@ -309,43 +385,48 @@ public sealed class YouTubeClient : IDisposable
|
||||
return ResultError.Fail("Failed to get datasyncIds! Client not logged in.");
|
||||
}
|
||||
|
||||
public async Task<Result<YouTubeVideo>> GetVideoByIdAsync(string videoId, CancellationToken cancellationToken = default)
|
||||
/*private async Task DecipherSignaturesAsync(YouTubeVideo video, ClientState state)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(videoId))
|
||||
var streamingData = video.StreamingData;
|
||||
if (streamingData == null)
|
||||
{
|
||||
return ResultError.Fail("Video id is empty!");
|
||||
_logger.Debug("No streaming data available, skipping decipher.");
|
||||
return;
|
||||
}
|
||||
|
||||
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))
|
||||
if (string.IsNullOrWhiteSpace(state.PlayerJsUrl))
|
||||
{
|
||||
return response.Error ?? ResultError.Fail("Request failed!");
|
||||
_logger.Warning("No player js url found.");
|
||||
}
|
||||
|
||||
var html = response.Value;
|
||||
|
||||
var stateResult = SetClientStateFromHtml(html);
|
||||
if (!stateResult.IsSuccess)
|
||||
var formatsWithCipher = streamingData.Formats.Concat(streamingData.AdaptiveFormats).Where(x => !string.IsNullOrWhiteSpace(x.SignatureCipher)).ToList();
|
||||
if (formatsWithCipher.Count == 0)
|
||||
{
|
||||
//TODO: Log warning: failed to update client state!
|
||||
_logger.Debug("Skipping signature decipher, no signatures found to decipher.");
|
||||
}
|
||||
|
||||
var htmlParseResult = HtmlParser.GetVideoDataFromHtml(html);
|
||||
if (!htmlParseResult.IsSuccess)
|
||||
if (string.IsNullOrWhiteSpace(streamingData.ServerAbrStreamingUrl))
|
||||
{
|
||||
return htmlParseResult.Error ?? ResultError.Fail("Failed to parse HTML video data!");
|
||||
_logger.Warning("No ABR streaming url available.");
|
||||
}
|
||||
|
||||
var videoParseResult = VideoJsonParser.ParseVideoData(htmlParseResult.Value);
|
||||
if (!videoParseResult.IsSuccess)
|
||||
var abrStreamUri = new Uri(streamingData.ServerAbrStreamingUrl);
|
||||
var queries = HttpUtility.ParseQueryString(abrStreamUri.Query);
|
||||
var nSig = queries.Get("n");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(nSig))
|
||||
{
|
||||
return videoParseResult;
|
||||
_logger.Warning("No N signature found.");
|
||||
}
|
||||
|
||||
//TODO: decipher stream urls
|
||||
/*var jsEngineResult = await JavaScriptEngineManager.GetPlayerEngine(state.PlayerJsUrl ?? "");
|
||||
if (!jsEngineResult.IsSuccess)
|
||||
{
|
||||
_logger.Warning(jsEngineResult.Error?.Description ?? "Failed to get player script engine.");
|
||||
return;
|
||||
}
|
||||
|
||||
return videoParseResult.Value;
|
||||
}
|
||||
var engine = jsEngineResult.Value;
|
||||
engine.InitializePlayer();#1#
|
||||
}*/
|
||||
}
|
||||
Reference in New Issue
Block a user