[REWORK] Changes saving client and channel info

This commit is contained in:
max
2025-09-18 02:01:45 +02:00
parent 9e173258ed
commit 5250b9f3f9
10 changed files with 201 additions and 153 deletions

View File

@@ -29,24 +29,39 @@ public partial class Channels : ComponentBase
return;
}
var clientPrep = (ClientChannel)result.Data;
if (clientPrep?.YouTubeClient == null)
var clientChannel = (ClientChannel)result.Data;
if (clientChannel?.YouTubeClient == null)
{
Snackbar.Add("No YouTube client received.", Severity.Error);
return;
}
var savedResult = await ClientService.SaveClientAsync(clientPrep.YouTubeClient, clientPrep.Channel);
if (!savedResult.IsSuccess)
{
Snackbar.Add($"Failed to store client: {savedResult.Error?.Description ?? "Unknown!"}", Severity.Error);
}
else
var savedClientResult = await ClientService.SaveClientAsync(clientChannel.YouTubeClient);
if (savedClientResult.IsSuccess)
{
if (_table != null)
{
await _table.ReloadServerData();
}
Snackbar.Add($"Client {clientPrep.Channel?.Handle ?? clientPrep.YouTubeClient.Id} saved!", Severity.Success);
Snackbar.Add($"Client {clientChannel.Channel?.Handle ?? clientChannel.YouTubeClient.Id} saved!", Severity.Success);
ClientService.AddClient(clientChannel.YouTubeClient);
}
else
{
Snackbar.Add($"Failed to store client: {savedClientResult.Error?.Description ?? "Unknown!"}", Severity.Error);
}
if (clientChannel.Channel == null)
{
Snackbar.Add("No channel information received!", Severity.Warning);
}
else
{
var saveChannelResult = await LibraryService.SaveChannelAsync(clientChannel.Channel);
if (!saveChannelResult.IsSuccess)
{
Snackbar.Add("Failed to save channel information", Severity.Warning);
}
}
}
}

View File

@@ -8,9 +8,11 @@ namespace Manager.App.Services;
public interface ILibraryService
{
public Task<Result> FetchChannelImagesAsync(Channel channel);
public Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel);
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
public Task<Result> SaveChannelAsync(ChannelEntity channel, CancellationToken cancellationToken = default);
public Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default);
public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default);
public Task<ListResult<ChannelEntity>> GetChannelsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default);

View File

@@ -32,14 +32,14 @@ public class LibraryService : ILibraryService
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirChannels));
}
public async Task<Result> FetchChannelImagesAsync(Channel channel)
public async Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel)
{
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync();
await AddWebImagesAsync(context, channel.AvatarImages, channel.Id, "avatars", LibraryConstants.FileTypes.ChannelAvatar, LibraryConstants.Directories.SubDirChannels);
await AddWebImagesAsync(context, channel.BannerImages, channel.Id, "banners", LibraryConstants.FileTypes.ChannelBanner, LibraryConstants.Directories.SubDirChannels);
await AddWebImagesAsync(context, innertubeChannel.AvatarImages, innertubeChannel.Id, "avatars", LibraryConstants.FileTypes.ChannelAvatar, LibraryConstants.Directories.SubDirChannels);
await AddWebImagesAsync(context, innertubeChannel.BannerImages, innertubeChannel.Id, "banners", LibraryConstants.FileTypes.ChannelBanner, LibraryConstants.Directories.SubDirChannels);
if (!context.ChangeTracker.HasChanges())
{
@@ -101,6 +101,43 @@ public class LibraryService : ILibraryService
}
}
public async Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default)
{
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var updateEntity = false;
var dbClient = context.ClientAccounts.FirstOrDefault(c => c.Id == client.Id);
if (dbClient == null)
{
dbClient = client;
}
else
{
updateEntity = true;
dbClient.HttpCookies = client.HttpCookies;
dbClient.UserAgent = client.UserAgent;
}
if (updateEntity)
{
context.ClientAccounts.Update(dbClient);
}
else
{
context.ClientAccounts.Add(dbClient);
}
var savedResult= await context.SaveChangesAsync(cancellationToken);
return savedResult <= 0 ? ResultError.Fail("Could not save changes!") : Result.Success();
}
catch (Exception e)
{
return ResultError.Error(e);
}
}
public async Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(id))
@@ -111,7 +148,11 @@ public class LibraryService : ILibraryService
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var channel = await context.Channels.Include(c => c.ClientAccount).FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
var channel = await context.Channels
.Include(c => c.ClientAccount)
.ThenInclude(p => p!.HttpCookies)
.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
if (channel == null)
{
return ResultError.Fail("Channel not found!");
@@ -125,18 +166,53 @@ public class LibraryService : ILibraryService
}
}
public async Task<Result> SaveChannelAsync(ChannelEntity channel, CancellationToken cancellationToken = default)
public async Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default)
{
try
{
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
if (context.Channels.Any(c => c.Id == channel.Id))
var imagesResult = await FetchChannelImagesAsync(innertubeChannel);
if (!imagesResult.IsSuccess)
{
context.Channels.Update(channel);
return ResultError.Fail("Failed to fetch channel images!");
}
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var channelResult = await GetChannelByIdAsync(innertubeChannel.Id, cancellationToken);
ChannelEntity? channelEntity;
try
{
if (channelResult.IsSuccess)
{
channelEntity = channelResult.Value;
channelEntity.Name = innertubeChannel.ChannelName;
channelEntity.Handle = innertubeChannel.Handle;
channelEntity.Description = innertubeChannel.Description;
}
else
{
context.Channels.Add(channel);
channelEntity = new ChannelEntity
{
Id = innertubeChannel.Id,
Name = innertubeChannel.ChannelName,
Handle = innertubeChannel.Handle,
Description = innertubeChannel.Description
};
}
}
catch (Exception e)
{
return ResultError.Error(e);
}
if (context.Channels.Any(c => c.Id == innertubeChannel.Id))
{
context.Channels.Update(channelEntity);
}
else
{
context.Channels.Add(channelEntity);
}
var changed = await context.SaveChangesAsync(cancellationToken);
@@ -144,7 +220,7 @@ public class LibraryService : ILibraryService
}
catch (Exception e)
{
return ResultError.Error(e);
return HandleException(e);
}
}

View File

@@ -1,23 +1,19 @@
using System.Net;
using DotBased.Logging;
using DotBased.Monads;
using Manager.App.Models.Library;
using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
using Manager.YouTube.Models.Innertube;
namespace Manager.App.Services.System;
public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientService> logger)
: ExtendedBackgroundService(nameof(ClientService), "Managing YouTube clients", logger, TimeSpan.FromMinutes(10))
{
private readonly List<ClientChannel> _loadedClients = [];
private CancellationToken _cancellationToken;
private readonly YouTubeClientCollection _loadedClients = [];
private ILibraryService? _libraryService;
protected override Task InitializeAsync(CancellationToken stoppingToken)
{
_cancellationToken = stoppingToken;
stoppingToken.Register(CancellationRequested);
using var scope = scopeFactory.CreateScope();
_libraryService = scope.ServiceProvider.GetRequiredService<ILibraryService>();
@@ -25,17 +21,79 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
return Task.CompletedTask;
}
protected override Task ExecuteServiceAsync(CancellationToken stoppingToken)
protected override async Task ExecuteServiceAsync(CancellationToken stoppingToken)
{
return Task.CompletedTask;
LogEvent($"Saving {_loadedClients.Count} loaded client(s)");
foreach (var client in _loadedClients)
{
await SaveClientAsync(client, cancellationToken: stoppingToken);
}
}
private void CancellationRequested()
private async void CancellationRequested()
{
// Clear up
foreach (var client in _loadedClients)
{
await SaveClientAsync(client);
client.Dispose();
}
}
public async Task<Result> SaveClientAsync(YouTubeClient client, Channel? channelInfo = null, CancellationToken cancellationToken = default)
public async Task<Result> AddClientByIdAsync(string id, CancellationToken stoppingToken = default)
{
if (_libraryService == null)
{
return ResultError.Fail("Library service is not initialized!.");
}
var clientResult = await _libraryService.GetChannelByIdAsync(id, stoppingToken);
if (!clientResult.IsSuccess)
{
return clientResult;
}
var clientAcc = clientResult.Value.ClientAccount;
if (clientAcc == null)
{
return ResultError.Fail("Client account is not initialized!.");
}
var cookieCollection = new CookieCollection();
foreach (var httpCookie in clientAcc.HttpCookies)
{
var cookie = new Cookie
{
Name = httpCookie.Name,
Value = httpCookie.Value,
Domain = httpCookie.Domain,
Path = httpCookie.Path,
Secure = httpCookie.Secure,
HttpOnly = httpCookie.HttpOnly,
Expires = httpCookie.ExpiresUtc ?? DateTime.MinValue
};
cookieCollection.Add(cookie);
}
var ytClientResult = await YouTubeClient.CreateAsync(cookieCollection, clientAcc.UserAgent ?? "");
if (!ytClientResult.IsSuccess)
{
return ytClientResult;
}
AddClient(ytClientResult.Value);
return Result.Success();
}
public void AddClient(YouTubeClient client)
{
if (_loadedClients.Contains(client))
{
return;
}
_loadedClients.Add(client);
}
public async Task<Result> SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default)
{
if (_libraryService == null)
{
@@ -48,121 +106,7 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
}
if (channelInfo != null)
{
var imagesResult = await _libraryService.FetchChannelImagesAsync(channelInfo);
if (!imagesResult.IsSuccess)
{
logger.LogWarning("Failed to fetch channel images!");
}
}
var channelResult = await _libraryService.GetChannelByIdAsync(client.Id, cancellationToken);
ChannelEntity? channelEntity;
try
{
if (channelResult.IsSuccess)
{
channelEntity = channelResult.Value;
UpdateChannelEntity(client, channelEntity, channelInfo);
}
else
{
channelEntity = CreateNewChannelFromClient(client, channelInfo);
}
}
catch (Exception e)
{
LogEvent("Failed to save client: " + e.Message, LogSeverity.Warning);
return ResultError.Error(e);
}
var saveResult = await _libraryService.SaveChannelAsync(channelEntity, cancellationToken);
var saveResult = await _libraryService.SaveClientAsync(new ClientAccountEntity { Id = client.Id, UserAgent = client.UserAgent }, cancellationToken);
return saveResult;
}
private void UpdateChannelEntity(YouTubeClient client, ChannelEntity entity, Channel? channelInfo)
{
if (channelInfo != null)
{
entity.Name = channelInfo.ChannelName;
entity.Handle = channelInfo.Handle;
entity.Description = channelInfo.Description;
}
var clientAcc = entity.ClientAccount;
if (clientAcc != null)
{
clientAcc.UserAgent = clientAcc.UserAgent;
var currentCookies = client.CookieContainer.GetAllCookies();
foreach (var cookieEntity in clientAcc.HttpCookies.ToList())
{
var cookie = currentCookies[cookieEntity.Name];
if (cookie == null)
{
clientAcc.HttpCookies.Remove(cookieEntity);
continue;
}
if (!cookie.Domain.Equals(cookieEntity.Domain, StringComparison.InvariantCultureIgnoreCase))
{
continue;
}
cookieEntity.Value = cookie.Value;
cookieEntity.Path = cookie.Path;
cookieEntity.Secure = cookie.Secure;
cookieEntity.HttpOnly = cookie.HttpOnly;
cookieEntity.ExpiresUtc = cookie.Expires == DateTime.MinValue ? null : cookie.Expires;
}
}
}
private ChannelEntity CreateNewChannelFromClient(YouTubeClient client, Channel? channelInfo)
{
if (channelInfo == null)
{
throw new ArgumentNullException(nameof(channelInfo), "Channel information is required to store new client/account.");
}
var cookies = new List<HttpCookieEntity>();
foreach (var cookieObj in client.CookieContainer.GetAllCookies())
{
if (cookieObj is not Cookie cookie)
{
continue;
}
var cookieEntity = new HttpCookieEntity
{
ClientId = client.Id,
Name = cookie.Name,
Value = cookie.Value,
Domain = cookie.Domain,
Path = cookie.Path,
Secure = cookie.Secure,
HttpOnly = cookie.HttpOnly,
ExpiresUtc = cookie.Expires == DateTime.MinValue ? null : cookie.Expires
};
cookies.Add(cookieEntity);
}
var clientAcc = new ClientAccountEntity
{
Id = client.Id,
UserAgent = client.UserAgent,
HttpCookies = cookies
};
var channel = new ChannelEntity
{
Id = channelInfo.Id,
Name = channelInfo.ChannelName,
Handle = channelInfo.Handle,
Description = channelInfo.Description,
ClientAccount = clientAcc
};
return channel;
}
}

View File

@@ -18,7 +18,7 @@ public sealed class LibraryDbContext : DbContext
public DbSet<CaptionEntity> Captions { get; set; }
public DbSet<ChannelEntity> Channels { get; set; }
public DbSet<ClientAccountEntity> Accounts { get; set; }
public DbSet<ClientAccountEntity> ClientAccounts { get; set; }
public DbSet<HttpCookieEntity> HttpCookies { get; set; }
public DbSet<MediaEntity> Media { get; set; }
public DbSet<MediaFormatEntity> MediaFormats { get; set; }

View File

@@ -16,7 +16,7 @@ public class HttpCookieEntity : DateTimeBase
public string? Domain { get; set; }
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
public string? Path { get; set; }
public DateTimeOffset? ExpiresUtc { get; set; }
public DateTime? ExpiresUtc { get; set; }
public bool Secure { get; set; }
public bool HttpOnly { get; set; }
}

View File

@@ -1,6 +1,6 @@
namespace Manager.YouTube.Models.Innertube;
public class Channel
public class InnertubeChannel
{
public required string Id { get; set; }
public bool NoIndex { get; set; }

View File

@@ -6,7 +6,7 @@ namespace Manager.YouTube.Parsers.Json;
public static class ChannelJsonParser
{
public static Result<Channel> ParseJsonToChannelData(string json)
public static Result<InnertubeChannel> ParseJsonToChannelData(string json)
{
try
{
@@ -23,7 +23,7 @@ public static class ChannelJsonParser
throw new InvalidOperationException("No channel id found.");
}
var channel = new Channel
var channel = new InnertubeChannel
{
Id = channelId,
ChannelName = channelMetadata.GetProperty("title").ToString(),

View File

@@ -148,7 +148,7 @@ public sealed class YouTubeClient : IDisposable
return Result.Success();
}
public async Task<Result<Channel>> GetChannelByIdAsync(string channelId)
public async Task<Result<InnertubeChannel>> GetChannelByIdAsync(string channelId)
{
if (State == null)
{

View File

@@ -0,0 +1,11 @@
using System.Collections.ObjectModel;
namespace Manager.YouTube;
public class YouTubeClientCollection : KeyedCollection<string, YouTubeClient>
{
protected override string GetKeyForItem(YouTubeClient item)
{
return item.Id;
}
}