49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using System.Net;
|
|
using DotBased.Monads;
|
|
using Manager.Data.Entities.LibraryContext;
|
|
using Manager.YouTube;
|
|
|
|
namespace Manager.App.Services.System;
|
|
|
|
public class ClientManager : BackgroundService
|
|
{
|
|
private readonly List<YouTubeClient> _clients = [];
|
|
private CancellationToken _cancellationToken;
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_cancellationToken = stoppingToken;
|
|
stoppingToken.Register(CancellationRequested);
|
|
}
|
|
|
|
private void CancellationRequested()
|
|
{
|
|
// Clear up
|
|
}
|
|
|
|
public async Task<Result<YouTubeClient>> LoadClient(ClientAccountEntity accountEntity)
|
|
{
|
|
if (_cancellationToken.IsCancellationRequested)
|
|
{
|
|
return ResultError.Fail("Service is shutting down.");
|
|
}
|
|
|
|
var container = new CookieContainer();
|
|
|
|
if (accountEntity.HttpCookies.Count != 0)
|
|
{
|
|
var cookieColl = new CookieCollection();
|
|
foreach (var cookieEntity in accountEntity.HttpCookies)
|
|
{
|
|
cookieColl.Add(new Cookie(cookieEntity.Name, cookieEntity.Value, cookieEntity.Domain));
|
|
}
|
|
|
|
container.Add(cookieColl);
|
|
}
|
|
|
|
var ytClient = new YouTubeClient(container, accountEntity.UserAgent ?? "");
|
|
await ytClient.GetStateAsync();
|
|
|
|
return ytClient;
|
|
}
|
|
} |