-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhnconf.go
195 lines (167 loc) · 4.13 KB
/
hnconf.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"html/template"
"math"
"os"
"path"
"sort"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/kardianos/osext"
"github.com/termie/go-shutil"
)
const (
ROOT_URL string = "https://news.ycombinator.com"
TARGET_DIR string = "/var/www/hn"
)
type NewsItem struct {
Title string
Link string
Points int
Comments int
CommentsLink string
}
func (ni *NewsItem) Score() float64 {
n := float64(ni.Points + ni.Comments)
pos := float64(ni.Points)
if n == 0 {
return 0
}
z := 1.96 // we assume a 95% confidence interval
phat := 1.0 * pos / n
res := (phat + z*z/(2*n) - z*math.Sqrt((phat*(1-phat)+z*z/(4*n))/n)) / (1 + z*z/n)
return res
}
type Items []*NewsItem
func (m Items) Len() int {
return len(m)
}
func (m Items) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m Items) Less(i, j int) bool {
return m[i].Score() < m[j].Score()
}
func Scrape() ([]*NewsItem, error) {
doc, err := goquery.NewDocument(ROOT_URL)
if err != nil {
return nil, err
}
res := []*NewsItem{}
doc.Find(".athing").Each(func(i int, s *goquery.Selection) {
el := s.Find(".title a")
// If there's more than one found, reduce it to the first element
if el.Size() > 1 {
el = el.Slice(0, 1)
}
title := el.Text()
if err != nil {
fmt.Printf("error grabbing html: %s\n", err)
return
}
link, exists := s.Find(".title a").Attr("href")
if !exists {
link = ""
}
if strings.HasPrefix(link, "item?") {
link = ROOT_URL + "/" + link
}
item := &NewsItem{Title: title, Link: link}
res = append(res, item)
//fmt.Printf("%v - %v\n", title, link)
})
doc.Find(".subtext").Each(func(i int, s *goquery.Selection) {
pString := s.Find(".score").Text()
cString := s.Find("a").Last().Text()
cString = strings.TrimSpace(strings.Replace(cString, "|", "", -1))
cLink, exists := s.Find("a").Last().Attr("href")
if !exists {
cLink = ""
} else {
cLink = ROOT_URL + "/" + cLink
}
points := 0
comments := 0
if pString != "" {
pSt := strings.Fields(pString)[0]
points, err = strconv.Atoi(pSt)
if err != nil {
points = 0
}
}
if cString != "" && cString != "discuss" && cString != "hide" {
cSt := strings.Fields(cString)[0]
comments, err = strconv.Atoi(cSt)
if err != nil {
comments = 0
}
}
item := res[i]
item.Points = points
item.Comments = comments
item.CommentsLink = cLink
})
sort.Sort(sort.Reverse(Items(res)))
return res, nil
}
func main() {
newsItems, err := Scrape()
if err != nil {
fmt.Println(err)
}
funcMap := template.FuncMap{
"fdate": DateFmt,
}
extDir, _ := osext.ExecutableFolder()
tmplPath := path.Join(extDir, "../src/github.com/ejamesc/go-hn-confidence", "template.html")
t := template.Must(template.New("template.html").Funcs(funcMap).ParseFiles(tmplPath))
filepath := path.Join(TARGET_DIR, "index.html")
file, err := os.OpenFile(filepath, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0755)
if err != nil {
fmt.Println(err)
}
presenter := struct {
Items []*NewsItem
LastGen time.Time
}{newsItems, time.Now()}
err = t.Execute(file, presenter)
if err != nil {
fmt.Println(err)
}
staticPath := path.Join(extDir, "../src/github.com/ejamesc/go-hn-confidence", "static")
// CopyTree demands that the destination folder not exist
// If it does, we delete it
outDir := path.Join(TARGET_DIR, "static")
_, err = os.Stat(outDir)
if err == nil {
err = os.RemoveAll(outDir)
if err != nil {
fmt.Println(err)
}
} else if err != nil && !os.IsNotExist(err) {
fmt.Println(err)
}
// CopyTree options:
// Symlinks - if true, symbolic links copied, if false symlinked files copied
// IgnoreDanglingSymlinks - supress error thrown when symlink links to missing file
// Optional CopyFunction
// Optional Ignore function
options := &shutil.CopyTreeOptions{
Symlinks: false,
IgnoreDanglingSymlinks: true,
CopyFunction: shutil.Copy,
Ignore: nil,
}
err = shutil.CopyTree(staticPath, outDir, options)
if err != nil {
fmt.Println(err)
}
}
// Helpers
func DateFmt(tt time.Time) string {
const layout = "3:04pm, 2 January 2006"
return tt.Format(layout)
}