[CHANGE] Cookie import by netscape cookie txt format
This commit is contained in:
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