-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (73 loc) · 1.8 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
package main
import (
"embed"
"fmt"
"html/template"
"io"
"io/fs"
"log"
"net/http"
"os"
"github.com/brleinad/laclong/laclong"
"github.com/robfig/cron/v3"
)
//go:embed static/*
var static embed.FS
func main() {
downloadCsvTicks()
c := cron.New()
c.AddFunc("@daily", downloadCsvTicks)
c.Start()
fSys, err := fs.Sub(static, "static")
if err != nil {
log.Fatal("Failed to serve static ", err)
}
fs := http.FileServer(http.FS(fSys))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", serveTemplate)
log.Print("Listening on :8080...")
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
func getContestants() []string {
return []string{
"200222284/daniel-rodas-bautista",
"200039487/louis-thomas-schreiber",
"112474537/francis-lessard",
"201468827/alexandre-tessier",
}
}
func serveTemplate(w http.ResponseWriter, r *http.Request) {
data := laclong.GetLacLongChallenges(getContestants())
tmpl := template.Must(template.ParseFS(static, "static/*"))
err := tmpl.ExecuteTemplate(w, "index.html", data)
if err != nil {
log.Fatal("Failed to execute template")
}
}
func downloadCsvTicks() {
for _, contestant := range getContestants() {
downloadCsvTicksForContestant(contestant)
}
}
func downloadCsvTicksForContestant(contestantId string) {
URL := fmt.Sprintf("https://www.mountainproject.com/user/%s/tick-export", contestantId)
ticksResponse, err := http.Get(URL)
if err != nil {
log.Fatal("There was an error")
}
fileName := laclong.GetFileName(contestantId)
file, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
defer ticksResponse.Body.Close()
size, err := io.Copy(file, ticksResponse.Body)
if err != nil {
log.Fatal(err)
}
defer file.Close()
log.Printf("Downloaded a file %s with size %d\n", fileName, size)
}