using System.Net; namespace Manager.YouTube.Parsers; public static class CookieTxtParser { public static async Task ParseAsync(Stream stream, HashSet? 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; } }