132 lines
3.8 KiB
C#
132 lines
3.8 KiB
C#
using System.Net;
|
|
using Manager.YouTube;
|
|
using Microsoft.AspNetCore.Components;
|
|
using MudBlazor;
|
|
|
|
namespace Manager.App.Components.Dialogs
|
|
{
|
|
public partial class AccountDialog : ComponentBase
|
|
{
|
|
[CascadingParameter] private IMudDialogInstance? MudDialog { get; set; }
|
|
[Parameter] public string DefaultUserAgent { get; set; } = "";
|
|
|
|
public YouTubeClient Client { get; set; } = new();
|
|
|
|
private bool _isLoading;
|
|
|
|
private bool _cookieTextValid;
|
|
private string _cookieText = "";
|
|
private string _cookieDomain = ".youtube.com";
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
Client.UserAgent = DefaultUserAgent;
|
|
base.OnInitialized();
|
|
}
|
|
|
|
private void ParseCookies()
|
|
{
|
|
try
|
|
{
|
|
var cookies = ParseCookieHeader(_cookieText, _cookieDomain);
|
|
Client.CookieContainer.Add(cookies);
|
|
_cookieText = string.Empty;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
SnackbarService.Add($"Parsing cookies failed: {e.Message}", Severity.Error);
|
|
}
|
|
StateHasChanged();
|
|
}
|
|
|
|
private static string? ValidateCookieText(string text)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
return "Cookies are required";
|
|
|
|
var pairs = text.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (var pair in pairs)
|
|
{
|
|
if (!pair.Contains('=')) return "Invalid.";
|
|
var key = pair[..pair.IndexOf('=')].Trim();
|
|
if (string.IsNullOrEmpty(key)) return "Invalid.";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static CookieCollection ParseCookieHeader(string cookieHeader, string domain = "")
|
|
{
|
|
var collection = new CookieCollection();
|
|
|
|
if (string.IsNullOrWhiteSpace(cookieHeader))
|
|
return collection;
|
|
|
|
var cookies = cookieHeader.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
foreach (var cookieStr in cookies)
|
|
{
|
|
var parts = cookieStr.Split('=', 2);
|
|
if (parts.Length == 2)
|
|
{
|
|
var name = parts[0].Trim();
|
|
var value = parts[1].Trim();
|
|
|
|
var cookie = new Cookie(name, value)
|
|
{
|
|
Expires = DateTime.Now.AddDays(1),
|
|
Path = "/",
|
|
};
|
|
|
|
if (!string.IsNullOrEmpty(domain))
|
|
cookie.Domain = domain;
|
|
|
|
collection.Add(cookie);
|
|
}
|
|
}
|
|
|
|
return collection;
|
|
}
|
|
|
|
private bool CanValidate()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Client.UserAgent) || Client.CookieContainer.Count <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private bool CanSave()
|
|
{
|
|
if (Client.External.State == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(Client.Id))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return Client.SapisidCookie != null && Client.External.State.LoggedIn;
|
|
}
|
|
|
|
private async Task ValidateAccount()
|
|
{
|
|
_isLoading = true;
|
|
var result = await Client.BuildClientAsync();
|
|
if (!result.IsSuccess)
|
|
{
|
|
SnackbarService.Add(result.Error?.Description ?? "Error validating account.", Severity.Error);
|
|
}
|
|
_isLoading = false;
|
|
}
|
|
|
|
private void OnSave()
|
|
{
|
|
MudDialog?.Close(DialogResult.Ok(Client));
|
|
}
|
|
}
|
|
} |