From d94be4df73885467bd68971c8a15c28e490a00f5 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sat, 30 Nov 2019 22:58:20 -0500 Subject: [PATCH] initial commit --- .gitignore | 137 +++++++++++++++++++++++++++++++++++++++++++++ cmd/getaoc/main.go | 48 ++++++++++++++++ getaoc.go | 36 ++++++++++++ go.mod | 3 + 4 files changed, 224 insertions(+) create mode 100644 .gitignore create mode 100644 cmd/getaoc/main.go create mode 100644 getaoc.go create mode 100644 go.mod diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53f95f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,137 @@ +# Created by .ignore support plugin (hsz.mobi) +### Vim template +# Swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +### macOS template +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Go template +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +.idea diff --git a/cmd/getaoc/main.go b/cmd/getaoc/main.go new file mode 100644 index 0000000..aebede4 --- /dev/null +++ b/cmd/getaoc/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "flag" + "fmt" + "os" + "strconv" + + "code.chrissexton.org/cws/getaoc" +) + +var year = flag.Int("year", 2019, "event year") +var sessId = flag.String("session", "", "session id") +var file = flag.String("file", "input.txt", "filename (- for stdout)") + +const base = "https://adventofcode.com" + +func main() { + flag.Parse() + if flag.NArg() < 1 { + fmt.Fprintln(os.Stderr, "Error: Must provide a day argument.") + return + } + day, _ := strconv.Atoi(flag.Arg(0)) + + sess, ok := os.LookupEnv("AOC_SESSION") + if !ok && *sessId == "" { + fmt.Fprintln(os.Stderr, "Error: Must provide a session id.") + return + } + + if *sessId != "" { + sess = *sessId + } + + body := getaoc.GetInput(sess, *year, day) + + if *file == "-" { + fmt.Println(body) + } else { + f, err := os.Create(*file) + if err != nil { + panic(err) + } + defer f.Close() + fmt.Fprint(f, body) + } +} diff --git a/getaoc.go b/getaoc.go new file mode 100644 index 0000000..ebb5e36 --- /dev/null +++ b/getaoc.go @@ -0,0 +1,36 @@ +package getaoc + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" +) + +const base = "https://adventofcode.com" + +func GetInput(session string, year, day int) string { + url := fmt.Sprintf("%s/%d/day/%d/input", base, year, day) + + r, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + panic(err) + } + r.AddCookie(&http.Cookie{ + Name: "session", + Value: session, + Path: "", + Domain: ".adventofcode.com", + Secure: false, + HttpOnly: false, + SameSite: 0, + }) + c := http.Client{} + resp, err := c.Do(r) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + body, _ := ioutil.ReadAll(resp.Body) + return string(body) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b3e3b10 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module code.chrissexton.org/cws/getaoc + +go 1.13