Added a Downloader for AoC puzzle inputs
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
if (args.Length < 3)
|
||||
{
|
||||
Console.WriteLine("Usage: AdventOfCode-Downloader.exe <year> <day> <session-cookie>");
|
||||
return;
|
||||
}
|
||||
|
||||
int year, day;
|
||||
if (!(int.TryParse(args[0], out year) && year <= DateTime.Now.Year && year > 2015) || !(int.TryParse(args[1], out day) && day is >= 1 and <= 25))
|
||||
{
|
||||
Console.WriteLine("Invalid year or date argument.\nYear must be between 2015 and current year and day must be between 1 and 25");
|
||||
return;
|
||||
}
|
||||
|
||||
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "AoC-PuzzleInputs",
|
||||
year.ToString(), "Prod", $"day{day:00}.txt");
|
||||
if(!Directory.Exists(Path.GetDirectoryName(path)))
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
|
||||
var url = new Uri($"https://adventofcode.com/{year}/day/{day}/input");
|
||||
using var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("Cookie", $"session={args[2]}");
|
||||
var responseMessage = await client.GetAsync(url);
|
||||
if (responseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
var inputString = await responseMessage.Content.ReadAsStringAsync();
|
||||
await File.WriteAllTextAsync(path, inputString);
|
||||
}
|
||||
else
|
||||
{
|
||||
var inputString = await responseMessage.Content.ReadAsStringAsync();
|
||||
Console.WriteLine($"Response status code does not indicate Success: {responseMessage.StatusCode} ({responseMessage.StatusCode.ToString()})\nError Message: " + inputString);
|
||||
}
|
||||
Reference in New Issue
Block a user