mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-06 04:08:27 +02:00
added scripts and 2020 folder
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/scripts/fetchers"
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// determine day to fetch
|
||||
day, year, cookie := fetchers.ParseFlags()
|
||||
fmt.Println("fetching for day", day)
|
||||
|
||||
// make the request
|
||||
url := fmt.Sprintf("https://adventofcode.com/%d/day/%d", year, day)
|
||||
body := fetchers.GetWithAOCCookie(url, cookie)
|
||||
|
||||
// parse the dang html
|
||||
prompt := parseHTML(body)
|
||||
|
||||
// write to file
|
||||
filename := filepath.Join(util.Dirname(), "../../../", fmt.Sprintf("%d/day%02d-prompt.md", year, day))
|
||||
fetchers.WriteToFile(filename, []byte(prompt))
|
||||
|
||||
fmt.Println("Wrote prompt to file: ", filename)
|
||||
|
||||
fmt.Println("Done!")
|
||||
}
|
||||
|
||||
// uses dfsHTML function once to get the class=day-desc html nodes, then parse
|
||||
// the text inside of them
|
||||
func parseHTML(htmlIn []byte) (promptOnly string) {
|
||||
strBuilder := strings.Builder{}
|
||||
node, _ := html.Parse(bytes.NewReader(htmlIn))
|
||||
|
||||
dayDescNodes := dfsHTML(node, cbFindDayDescClass)
|
||||
dayDescNodesMap := map[*html.Node]bool{}
|
||||
|
||||
for _, ddNode := range dayDescNodes {
|
||||
dayDescNodesMap[ddNode.(*html.Node)] = true
|
||||
dfsHTML(ddNode.(*html.Node), cbParseHTMLText(&strBuilder, dayDescNodesMap))
|
||||
}
|
||||
|
||||
return strBuilder.String()
|
||||
}
|
||||
|
||||
// function takes in a node and a callback that is run on each node
|
||||
// callback returns a slice of interfaces which are returned by dfs
|
||||
func dfsHTML(node *html.Node, cb func(*html.Node) []interface{}) []interface{} {
|
||||
var sli []interface{}
|
||||
for child := node.FirstChild; child != nil; child = child.NextSibling {
|
||||
if cb(child) != nil {
|
||||
sli = append(sli, child)
|
||||
}
|
||||
sli = append(sli, dfsHTML(child, cb)...)
|
||||
}
|
||||
return sli
|
||||
}
|
||||
|
||||
func cbFindDayDescClass(node *html.Node) []interface{} {
|
||||
for _, attr := range node.Attr {
|
||||
if attr.Key == "class" && attr.Val == "day-desc" {
|
||||
fmt.Println("day-desc node found!")
|
||||
return []interface{}{node}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func cbParseHTMLText(builder *strings.Builder, dayDescNodesMap map[*html.Node]bool) func(*html.Node) []interface{} {
|
||||
return func(node *html.Node) []interface{} {
|
||||
if node.Type == html.TextNode {
|
||||
builder.WriteString(node.Data)
|
||||
}
|
||||
if node.Parent != nil && dayDescNodesMap[node.Parent] {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user