Compare commits

...

6 Commits

Author SHA1 Message Date
max
3d20c116da [CHANGE] Adding playlist functionality 2025-11-02 21:43:06 +01:00
max
a849b7524d [CHANGE] Added video info page 2025-11-02 21:22:56 +01:00
max
bf957436f0 [CHANGE] Disabled signature challenge. Need to run js. 2025-11-02 19:45:41 +01:00
max
16343c9a56 [CHANGE] Testing js runtime script 2025-11-02 00:02:09 +01:00
max
4c04378080 [CHANGE] Added jint. Reworking decipher functionality 2025-10-28 19:08:59 +01:00
max
b5c701b971 [CHANGE] Reworked cipher stuff 2025-10-24 21:16:08 +02:00
19 changed files with 566 additions and 44 deletions

View 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>

View 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;
}
}

View File

@@ -3,14 +3,15 @@
@inject ISnackbar Snackbar
@inject ClientService ClientService
@inject NavigationManager NavigationManager
<MudText>Video data</MudText>
<MudStack Spacing="2">
<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="GetDataAsync">Get data</MudButton>
<MudButton OnClick="NavigateToVideo">Get data</MudButton>
</MudStack>

View File

@@ -16,7 +16,7 @@ public partial class DevelopmentVideo : ComponentBase
return !searchResults.IsSuccess ? [] : searchResults.Value;
}
private async Task GetDataAsync(MouseEventArgs obj)
private void NavigateToVideo(MouseEventArgs obj)
{
if (_selectedClient == null)
{
@@ -35,22 +35,6 @@ public partial class DevelopmentVideo : ComponentBase
Snackbar.Add("Video ID needs to have an length of 11 chars!", Severity.Warning);
}
var clientResult = await ClientService.LoadClientByIdAsync(_selectedClient.Id);
if (!clientResult.IsSuccess)
{
Snackbar.Add(clientResult.Error?.Description ?? $"Failed to get client with id: {_selectedClient.Id}", Severity.Error);
return;
}
var ytClient = clientResult.Value;
var videoResult = await ytClient.GetVideoByIdAsync(_videoId);
if (!videoResult.IsSuccess)
{
Snackbar.Add(videoResult.Error?.Description ?? $"Failed to load video: {_videoId}", Severity.Error);
return;
}
var ytVideo = videoResult.Value;
Snackbar.Add($"Loaded video {ytVideo.Title}", Severity.Success);
NavigationManager.NavigateTo($"/video/{_videoId}?clientId={_selectedClient.Id}");
}
}

View File

@@ -9,4 +9,7 @@
<MudTabPanel Text="Video">
<DevelopmentVideo />
</MudTabPanel>
<MudTabPanel Text="Cipher">
<CipherDev />
</MudTabPanel>
</MudTabs>

View 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>
}

View 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;
}
}

View File

@@ -144,9 +144,9 @@ 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)
.ThenInclude(p => p!.HttpCookies)
.Include(f => f.Files)
.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);

View File

@@ -31,11 +31,10 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
}
}
private async void CancellationRequested()
private void CancellationRequested()
{
foreach (var client in _loadedClients)
{
await SaveClientAsync(client);
client.Dispose();
}
}

View 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;
}
}

View 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");
}
}

View 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;
}
}

View File

@@ -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>

View File

@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using Manager.YouTube.Util.Converters;
namespace Manager.YouTube.Models.Innertube;
@@ -8,6 +9,7 @@ public class StreamingData
[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; } = [];

View 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; }
}

View 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; }
}

View File

@@ -112,7 +112,7 @@ public partial class CipherDecoder
}
[GeneratedRegex(@"(\w+)=function\(\w+\){(\w+)=\2\.split\(\x22{2}\);.*?return\s+\2\.join\(\x22{2}\)}")]
[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();

View 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();
}

View File

@@ -9,7 +9,6 @@ using Manager.YouTube.Models;
using Manager.YouTube.Models.Innertube;
using Manager.YouTube.Parsers;
using Manager.YouTube.Parsers.Json;
using Manager.YouTube.Util.Cipher;
namespace Manager.YouTube;
@@ -114,7 +113,7 @@ public sealed class YouTubeClient : IDisposable
return videoParseResult;
}
await DecipherSignatures(videoParseResult.Value, state);
//await DecipherSignaturesAsync(videoParseResult.Value, state);
return videoParseResult.Value;
}
@@ -175,10 +174,10 @@ public sealed class YouTubeClient : IDisposable
public async Task<Result> RotateCookiesPageAsync(string origin = NetworkService.Origin, int ytPid = 1)
{
/*if (IsAnonymous)
if (IsAnonymous)
{
return ResultError.Fail("Anonymous clients cannot rotate cookies!");
}*/
}
if (string.IsNullOrWhiteSpace(origin))
{
@@ -386,7 +385,7 @@ public sealed class YouTubeClient : IDisposable
return ResultError.Fail("Failed to get datasyncIds! Client not logged in.");
}
private async Task DecipherSignatures(YouTubeVideo video, ClientState state)
/*private async Task DecipherSignaturesAsync(YouTubeVideo video, ClientState state)
{
var streamingData = video.StreamingData;
if (streamingData == null)
@@ -394,25 +393,40 @@ public sealed class YouTubeClient : IDisposable
_logger.Debug("No streaming data available, skipping decipher.");
return;
}
if (string.IsNullOrWhiteSpace(state.PlayerJsUrl))
{
_logger.Warning("No player js url found.");
}
var formatsWithCipher = streamingData.Formats.Concat(streamingData.AdaptiveFormats).Where(x => !string.IsNullOrWhiteSpace(x.SignatureCipher)).ToList();
if (formatsWithCipher.Count == 0)
{
_logger.Debug("Skipping decipher, no signatures found to decipher.");
_logger.Debug("Skipping signature decipher, no signatures found to decipher.");
}
if (string.IsNullOrWhiteSpace(streamingData.ServerAbrStreamingUrl))
{
_logger.Warning("No ABR streaming url available.");
}
var abrStreamUri = new Uri(streamingData.ServerAbrStreamingUrl);
var queries = HttpUtility.ParseQueryString(abrStreamUri.Query);
var nSig = queries.Get("n");
if (string.IsNullOrWhiteSpace(nSig))
{
_logger.Warning("No N signature found.");
}
/*var jsEngineResult = await JavaScriptEngineManager.GetPlayerEngine(state.PlayerJsUrl ?? "");
if (!jsEngineResult.IsSuccess)
{
_logger.Warning(jsEngineResult.Error?.Description ?? "Failed to get player script engine.");
return;
}
var decipherDecoderResult = await CipherManager.GetDecoderAsync(state, this);
if (!decipherDecoderResult.IsSuccess)
{
_logger.Warning(decipherDecoderResult.Error?.Description ?? "Failed to get the cipher decoder!");
return;
}
var decoder = decipherDecoderResult.Value;
foreach (var format in formatsWithCipher)
{
format.Url = decoder.Decipher(format.SignatureCipher);
}
}
var engine = jsEngineResult.Value;
engine.InitializePlayer();#1#
}*/
}