144 lines
4.7 KiB
C#
144 lines
4.7 KiB
C#
using System.Net;
|
|
using DotBased.Monads;
|
|
using Manager.App.Models.Library;
|
|
using Manager.Data.Entities.LibraryContext;
|
|
using Manager.YouTube;
|
|
|
|
namespace Manager.App.Services.System;
|
|
|
|
public class ClientManager(IServiceScopeFactory scopeFactory, HostedServiceConnector serviceConnector) : BackgroundService
|
|
{
|
|
private readonly List<YouTubeClient> _clients = [];
|
|
private CancellationToken _cancellationToken;
|
|
private ILibraryService? _libraryService;
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
serviceConnector.RegisterService(this);
|
|
_cancellationToken = stoppingToken;
|
|
stoppingToken.Register(CancellationRequested);
|
|
using var scope = scopeFactory.CreateScope();
|
|
_libraryService = scope.ServiceProvider.GetRequiredService<ILibraryService>();
|
|
}
|
|
|
|
private void CancellationRequested()
|
|
{
|
|
// Clear up
|
|
}
|
|
|
|
public async Task<Result<ClientPrep>> PrepareClient()
|
|
{
|
|
|
|
return ResultError.Fail("Not implemented!");
|
|
}
|
|
|
|
/*public async Task<Result> 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<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 = client.Id,
|
|
Name = client.External.Channel?.ChannelName,
|
|
Handle = client.External.Channel?.Handle,
|
|
Description = client.External.Channel?.Description,
|
|
ClientAccount = clientAcc
|
|
};
|
|
return channel;
|
|
}*/
|
|
|
|
public async Task<Result<YouTubeClient>> LoadClientByIdAsync(string id)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
{
|
|
return ResultError.Fail("Client ID is empty!");
|
|
}
|
|
|
|
|
|
return ResultError.Fail("Not implemented");
|
|
}
|
|
} |