-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrass.go
66 lines (53 loc) · 1.35 KB
/
grass.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
package main
import (
"github.com/PuerkitoBio/goquery"
"log"
"net/http"
"fmt"
"time"
"strconv"
)
func getContribution(username string,t time.Time)int{
doc := getDocument(username)
sprint := fmt.Sprintf("rect[data-date=\"%s\"]", dayString(t))
s := doc.Find(sprint)
data,_ := s.Attr("data-count")
count ,_ := strconv.Atoi(data)
return count
}
func getYearContributions(username string)[]Grass{
layout := "2006-01-02"
doc := getDocument(username)
fmt.Println(len(doc.Find("rect").Nodes))
grasses := make([]Grass, len(doc.Find("rect").Nodes))
doc.Find("rect").Each(func(i int, s *goquery.Selection) {
dateStr,_ := s.Attr("data-date")
t, _ := time.Parse(layout, dateStr)
grasses[i].CreatedAt = t
data,_ := s.Attr("data-count")
count ,_ := strconv.Atoi(data)
grasses[i].DataCount = count
})
return grasses
}
func getDocument(username string)*goquery.Document{
url := fmt.Sprintf("https://github.com/users/%s/contributions",username)
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
return doc
}
func dayString(t time.Time)string{
const layout = "2006-01-02"
return t.Format(layout)
}