using System.Net; using DotBased.Logging; using DotBased.Monads; using Manager.App.Models.Library; using Manager.Data.Entities.LibraryContext; using Manager.YouTube; namespace Manager.App.Services.System; public class ClientService(IServiceScopeFactory scopeFactory, ILogger logger, BackgroundServiceManager serviceManager) : ExtendedBackgroundService("ClientService", "Managing YouTube clients", logger, serviceManager, TimeSpan.FromMilliseconds(100)) { private readonly List _clients = []; private CancellationToken _cancellationToken; private ILibraryService? _libraryService; protected override async Task InitializeAsync(CancellationToken stoppingToken) { _cancellationToken = stoppingToken; stoppingToken.Register(CancellationRequested); using var scope = scopeFactory.CreateScope(); _libraryService = scope.ServiceProvider.GetRequiredService(); LogEvent("Initializing service..."); //Pause(); } protected override async Task ExecuteServiceAsync(CancellationToken stoppingToken) { LogEvent("Sending event..."); LogEvent("Sending warning event...", LogSeverity.Warning); LogEvent("Sending error event...", LogSeverity.Error); } private void CancellationRequested() { // Clear up } public async Task> PrepareClient() { return ResultError.Fail("Not implemented!"); } /*public async Task SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(client.Id)) { return ResultError.Fail("Client does not have an ID, cannot save to library database!"); } var channelResult = await libraryService.GetChannelByIdAsync(client.Id, cancellationToken); ChannelEntity? channel; if (channelResult.IsSuccess) { channel = channelResult.Value; UpdateChannelEntity(channel, client); } else { channel = CreateNewChannelFromClient(client); } var saveResult = await libraryService.SaveChannelAsync(channel, cancellationToken); return saveResult; }*/ /*private void UpdateChannelEntity(ChannelEntity channel, YouTubeClient client) { channel.Name = client.External.Channel?.ChannelName; channel.Handle = client.External.Channel?.Handle; channel.Description = client.External.Channel?.Description; var clientAcc = channel.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) { var cookies = new List(); 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 = client.Id, Name = client.External.Channel?.ChannelName, Handle = client.External.Channel?.Handle, Description = client.External.Channel?.Description, ClientAccount = clientAcc }; return channel; }*/ public async Task> LoadClientByIdAsync(string id) { if (string.IsNullOrWhiteSpace(id)) { return ResultError.Fail("Client ID is empty!"); } return ResultError.Fail("Not implemented"); } }