87 lines
2.5 KiB
C#
Raw Normal View History

using CommandLine;
using AoC.API;
using System.IO;
using System.Reflection;
const string levelFilePathTemplate = "{0}/inputs/{1}/{2}/input.txt";
// find git repo base path
DirectoryInfo? gitParentDirectory = new FileInfo(
Path.GetDirectoryName(Assembly.GetAssembly(typeof(Options)).Location)).Directory;
while(gitParentDirectory != null && !gitParentDirectory.ToString().EndsWith("LevelInputProvider"))
{
gitParentDirectory = gitParentDirectory?.Parent;
}
string gitBasePath = gitParentDirectory?.ToString() + Path.DirectorySeparatorChar + "LevelInputProvider";
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
if (string.IsNullOrEmpty(o.Cookie))
{
var env_cookie = System.Environment.GetEnvironmentVariable("SESSION_COOKIE");
if(string.IsNullOrEmpty(env_cookie))
{
throw new Exception("A session cookie must be provided!");
}
o.Cookie = env_cookie;
}
EntryPoint(o);
});
bool InputAlreadyExists(int year, int day)
{
return File.Exists(string.Format(levelFilePathTemplate, gitBasePath, year, day));
}
void EnsureDirectoryExists(string filePath)
{
FileInfo fi = new FileInfo(filePath);
if (!fi.Directory.Exists)
{
System.IO.Directory.CreateDirectory(fi.DirectoryName);
}
}
void EntryPoint(Options o)
{
var currentYear = DateTime.Now.Year;
var currentMonth = DateTime.Now.Month;
var currentDay = DateTime.Now.Day;
if(currentMonth != 12)
{
throw new Exception("Tsk Tsk, it isn't even December yet!");
}
for(int i = 1; i <= currentDay; i++)
{
Console.WriteLine("Attempt to get input " + i + " for year " + currentYear + "");
if(InputAlreadyExists(currentYear, i))
{
Console.WriteLine("Input " + i + " already exists!");
continue;
}
var client = new Session(o.Cookie, currentYear, i);
var inputTextTask = client.GetInputTextAsync();
string inputText = inputTextTask.Result;
var fileName = string.Format(levelFilePathTemplate, gitBasePath, currentYear, i);
EnsureDirectoryExists(fileName);
using(var sw = new StreamWriter(fileName))
{
sw.Write(inputText);
}
}
Console.WriteLine("Finished input fetching!");
}
public class Options
{
[Option('c', "cookie", Required = false, HelpText = "The AoC session cookie to use.")]
public string Cookie { get; set; } = "";
}