/
util.go
62 lines (53 loc) · 1.09 KB
/
util.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"golang.org/x/crypto/ssh/terminal"
)
var nullTime = time.Unix(0, 0)
var terminalState *terminal.State
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}
func init() {
if runtime.GOOS != "windows" {
terminalState, _ := terminal.GetState(0)
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
go func() {
<-sigint
terminal.Restore(0, terminalState)
fmt.Println("")
os.Exit(1)
}()
}
}
func mustParseResponse(resp *http.Response) *goquery.Document {
gq, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
log.Fatal(err)
}
return gq
}
func parseSignupDate(s string) time.Time {
// Dates ont he page are either "n days ago" or "day longmonth year"
var date time.Time
if strings.Contains(s, "days ago") {
days, _ := strconv.Atoi(strings.Split(s, " ")[0])
date = time.Now().Add(-24 * time.Duration(days) * time.Hour)
} else {
date, _ = time.Parse("2 January 2006", s)
}
return date
}