-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (134 loc) · 3.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/joho/godotenv"
)
func main() {
// Set the -d and -y flags to get the day and year for the challenges
dayFlag := flag.String("d", "", "The day number")
yearFlag := flag.String("y", "", "The year")
// Set the flag variables
flag.Parse()
//Print the help text if not set...
if *dayFlag == "" || *yearFlag == "" {
fmt.Println("You must enter an input. The details are below")
flag.PrintDefaults()
os.Exit(1)
}
if *dayFlag == "all" {
for i := 1; i < 26; i++ {
err := writeFiles(*yearFlag, fmt.Sprint(i))
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
time.Sleep(200 * time.Nanosecond)
}
} else {
err := writeFiles(*yearFlag, *dayFlag)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
}
}
func writeFiles(year string, day string) error {
fmt.Println("Getting the details for AoC", year, "Day", day, "...")
// Create the directory for the file...
err := os.MkdirAll(fmt.Sprintf("./%s/day%s/", year, day), os.ModePerm)
if err != nil {
return err
}
file, err := os.Create(fmt.Sprintf("./%s/day%s/day%s.md", year, day, day))
if err != nil {
return err
}
markdown, err := getInstructions(year, day)
if err != nil {
return err
}
file.WriteString(markdown)
defer file.Close()
// Now get the Input text
inputFile, err := os.Create(fmt.Sprintf("./%s/day%s/input.txt", year, day))
if err != nil {
return err
}
inputText, err := getInput(year, day)
if err != nil {
return err
}
inputFile.WriteString(inputText)
defer inputFile.Close()
return nil
}
func requestData(url string) (*http.Response, error) {
// Grab the session ID from the .env file...
sessionID := loadSessionVariable()
// setup the request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Panic("Unable to create the request")
}
req.AddCookie(&http.Cookie{Name: "session", Value: sessionID})
client := &http.Client{}
response, err := client.Do(req)
if response.StatusCode != 200 {
return nil, fmt.Errorf("status code: %s", fmt.Sprint(response.StatusCode))
}
if err != nil {
log.Panic("The request failed. Error -", err.Error())
}
return response, nil
}
func loadSessionVariable() string {
err := godotenv.Load(".env")
if err != nil {
log.Panic("Cannot load session ID")
}
return os.Getenv("SESSION_ID")
}
func getInstructions(year string, day string) (string, error) {
// Setup the request...
url := fmt.Sprintf("https://adventofcode.com/%s/day/%s", year, day)
response, err := requestData(url)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return "", fmt.Errorf("status code: %s", fmt.Sprint(response.StatusCode))
}
bodyBytes, _ := io.ReadAll(response.Body)
body := string(bodyBytes)
// body := fmt.Sprintf("# Day %s\n\n %s", day, url)
converter := md.NewConverter("", true, nil)
markdown, err := converter.ConvertString(body)
if err != nil {
return "", err
}
return markdown, nil
}
func getInput(year string, day string) (string, error) {
// Setup the request...
url := fmt.Sprintf("https://adventofcode.com/%s/day/%s/input", year, day)
response, err := requestData(url)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return "", fmt.Errorf("status code: %s", fmt.Sprint(response.StatusCode))
}
bodyBytes, _ := io.ReadAll(response.Body)
body := string(bodyBytes)
// body := fmt.Sprintf("# Day %s Input\n\n %s", day, url)
return body, nil
}