|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "github.com/joho/godotenv" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +//go:embed template.go |
| 18 | +var template string |
| 19 | + |
| 20 | +func main() { |
| 21 | + err := godotenv.Load() |
| 22 | + if err != nil { |
| 23 | + log.Fatal("Error loading .env file") |
| 24 | + } |
| 25 | + |
| 26 | + var day, year int |
| 27 | + var cookie string |
| 28 | + |
| 29 | + today := time.Now() |
| 30 | + flag.IntVar(&day, "day", today.Day(), "day number to fetch, 1-25") |
| 31 | + flag.IntVar(&year, "year", today.Year(), "day of year to fetch") |
| 32 | + flag.StringVar(&cookie, "cookie", os.Getenv("AOC_SESSION"), "session cookie from website") |
| 33 | + flag.Parse() |
| 34 | + |
| 35 | + url := fmt.Sprintf("https://adventofcode.com/%d/day/%d/input", year, day) |
| 36 | + prefixedDay := fmt.Sprintf("day_%02d", day) |
| 37 | + |
| 38 | + fmt.Printf("Sending request to %s...\n", url) |
| 39 | + response, err := sendGetRequest(url, cookie) |
| 40 | + if err != nil { |
| 41 | + log.Fatal(err) |
| 42 | + } |
| 43 | + stringResponse := string(response) |
| 44 | + stringResponse = strings.TrimRight(stringResponse, "\n") |
| 45 | + fmt.Println("Received response!") |
| 46 | + |
| 47 | + err = writeToFile(filepath.Join(prefixedDay, "input.txt"), stringResponse) |
| 48 | + if err != nil { |
| 49 | + log.Fatal("Error writing to file:", err) |
| 50 | + } |
| 51 | + |
| 52 | + err = writeToFile(filepath.Join(prefixedDay, "main.go"), template) |
| 53 | + if err != nil { |
| 54 | + log.Fatal("Error writing to file:", err) |
| 55 | + } |
| 56 | + |
| 57 | + fmt.Println() |
| 58 | + fmt.Printf("Successfully created all template files for day %d of Advent of Code %d\n", day, year) |
| 59 | + fmt.Printf("You can view task description here: https://adventofcode.com/%d/day/%d\n", year, day) |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +func sendGetRequest(url, cookie string) ([]byte, error) { |
| 64 | + client := &http.Client{} |
| 65 | + req, err := http.NewRequest("GET", url, nil) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + req.AddCookie(&http.Cookie{ |
| 71 | + Name: "session", |
| 72 | + Value: cookie, |
| 73 | + }) |
| 74 | + |
| 75 | + response, err := client.Do(req) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + defer func() { |
| 81 | + _ = response.Body.Close() |
| 82 | + }() |
| 83 | + |
| 84 | + body, err := io.ReadAll(response.Body) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + return body, nil |
| 90 | +} |
| 91 | + |
| 92 | +func writeToFile(filePath, content string) error { |
| 93 | + if _, err := os.Stat(filePath); err == nil { |
| 94 | + fmt.Println("file already exists:", filePath) |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + dir := filepath.Dir(filePath) |
| 99 | + if err := os.MkdirAll(dir, os.ModePerm); err != nil { |
| 100 | + return fmt.Errorf("creating directory: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + file, err := os.Create(filePath) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + defer func() { |
| 108 | + _ = file.Close() |
| 109 | + }() |
| 110 | + |
| 111 | + if _, err := file.WriteString(content); err != nil { |
| 112 | + return fmt.Errorf("writing to file: %w", err) |
| 113 | + } |
| 114 | + return nil |
| 115 | +} |
0 commit comments