[CHANGE] Cookie import by netscape cookie txt format
This commit is contained in:
22
Manager.YouTube/Constants/CookieConstants.cs
Normal file
22
Manager.YouTube/Constants/CookieConstants.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
namespace Manager.YouTube.Constants;
|
||||
|
||||
public static class CookieConstants
|
||||
{
|
||||
public static readonly IReadOnlyCollection<string> RequiredCookiesNames = new HashSet<string>
|
||||
{
|
||||
"SID",
|
||||
"SIDCC",
|
||||
"HSID",
|
||||
"SSID",
|
||||
"APISID",
|
||||
"SAPISID",
|
||||
"__Secure-1PAPISID",
|
||||
"__Secure-1PSID",
|
||||
"__Secure-1PSIDCC",
|
||||
"__Secure-1PSIDTS",
|
||||
"__Secure-3PAPISID",
|
||||
"__Secure-3PSID",
|
||||
"__Secure-3PSIDCC",
|
||||
"__Secure-3PSIDTS"
|
||||
};
|
||||
}
|
||||
53
Manager.YouTube/Parsers/CookieTxtParser.cs
Normal file
53
Manager.YouTube/Parsers/CookieTxtParser.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Net;
|
||||
|
||||
namespace Manager.YouTube.Parsers;
|
||||
|
||||
public static class CookieTxtParser
|
||||
{
|
||||
public static async Task<CookieCollection> ParseAsync(Stream stream, HashSet<string>? allowedCookies = null)
|
||||
{
|
||||
var cookieCollection = new CookieCollection();
|
||||
|
||||
using var reader = new StreamReader(stream);
|
||||
while (await reader.ReadLineAsync() is { } line)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line) || line.StartsWith('#'))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var lineParts = line.Split('\t');
|
||||
if (lineParts.Length < 7)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var domain = lineParts[0];
|
||||
//var includeSubdomains = lineParts[1].Equals("TRUE", StringComparison.OrdinalIgnoreCase);
|
||||
var path = lineParts[2];
|
||||
var secure = lineParts[3].Equals("TRUE", StringComparison.OrdinalIgnoreCase);
|
||||
var unixExpiry = long.TryParse(lineParts[4], out var exp) ? exp : 0;
|
||||
var name = lineParts[5];
|
||||
var value = lineParts[6];
|
||||
|
||||
if (!allowedCookies?.Contains(name) ?? false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var cookie = new Cookie(name, value, path, domain)
|
||||
{
|
||||
Secure = secure
|
||||
};
|
||||
|
||||
if (unixExpiry is > 0 and < int.MaxValue)
|
||||
{
|
||||
cookie.Expires = DateTimeOffset.FromUnixTimeSeconds(unixExpiry).DateTime;
|
||||
}
|
||||
|
||||
cookieCollection.Add(cookie);
|
||||
}
|
||||
|
||||
return cookieCollection;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user