forked from 29decibel/digestor
/
github.go
60 lines (49 loc) · 1.24 KB
/
github.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
package main
import (
"bytes"
"github.com/moovweb/gokogiri"
"github.com/moovweb/gokogiri/css"
"github.com/moovweb/gokogiri/xml"
"html/template"
)
const (
githubHost = "http://github.com"
)
func githubMarkup() string {
body := body("https://github.com/trending")
doc, _ := gokogiri.ParseHtml(body)
xpath := css.Convert("div.leaderboard-list-content", css.GLOBAL)
links, _ := doc.Search(xpath)
var newLinks []interface{}
for _, l := range links {
result := ""
repos, _ := l.Search(css.Convert(".repository-name", css.LOCAL))
desc, _ := l.Search(css.Convert(".repo-leaderboard-description", css.LOCAL))
if len(repos) > 0 {
absoluteURL(repos[0])
result += repos[0].String()
}
if len(desc) > 0 {
result += desc[0].String()
}
newLinks = append(newLinks, template.HTML(result))
}
tmpl, err := template.New("github-trending").Parse(`
<ul>
{{range .links}}
<li>{{.}}</li>
{{end}}
</ul>
`)
checkErr(err, "template error")
var results bytes.Buffer
tmpl.Execute(&results, map[string]interface{}{
"links": newLinks,
})
return string(results.Bytes())
}
// make node's href absolute
func absoluteURL(node xml.Node) {
url := node.Attribute("href").Value()
node.SetAttr("href", githubHost+url)
}