diff --git a/.gitea/workflows/update_level_inputs.yml b/.gitea/workflows/update_level_inputs.yml new file mode 100644 index 0000000..de1ebc7 --- /dev/null +++ b/.gitea/workflows/update_level_inputs.yml @@ -0,0 +1,32 @@ +name: Update Level Inputs +permissions: + contents: write + +env: + SKIP_MAKE_SETUP_CHECK: 'true' + +on: + schedule: + - cron: '15 8 1-25 12 *' + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + run: + needs: release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up dotnet + uses: actions/setup-dotnet@v4 + with: + dotnet-version: '9.0.X' + - name: Update inputs and publish + env: + SESSION_COOKIE: ${{ secrets.AOC_SESSION_COOKIE }} + run: | + dotnet run --project LevelInputUpdateRunner/LevelInputUpdateRunner.csproj + git add ./LevelInputProvider/input/* + git commit -m "feat(inputs): add current inputs, ref: NOISSUE" + make release diff --git a/LevelInputUpdateRunner/LevelInputUpdateRunner.csproj b/LevelInputUpdateRunner/LevelInputUpdateRunner.csproj new file mode 100644 index 0000000..da349ce --- /dev/null +++ b/LevelInputUpdateRunner/LevelInputUpdateRunner.csproj @@ -0,0 +1,15 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + + diff --git a/LevelInputUpdateRunner/Program.cs b/LevelInputUpdateRunner/Program.cs new file mode 100644 index 0000000..17b8de9 --- /dev/null +++ b/LevelInputUpdateRunner/Program.cs @@ -0,0 +1,87 @@ +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(args) + .WithParsed(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; } = ""; +} \ No newline at end of file