-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.go
67 lines (60 loc) · 1.61 KB
/
common.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
package main
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/PuerkitoBio/goquery"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
)
// MBO_URL is the base url for the MINDBODY Online client portal.
const MBO_URL = "https://clients.mindbodyonline.com"
// MBOSession stores information needed to scrape MBO's website as a logged-in user.
type MBOSession struct {
Cookies []*http.Cookie
StudioID string
}
func LoadMBOSession() (mboSession MBOSession, err error) {
file, err := os.Open(fmt.Sprintf("%s/.mindbodyonline", os.Getenv("HOME")))
if err != nil {
return mboSession, fmt.Errorf("Must be logged in.")
}
defer file.Close()
dec := gob.NewDecoder(file)
err = dec.Decode(&mboSession)
if err != nil {
return mboSession, fmt.Errorf("Must be logged in.")
}
return mboSession, nil
}
// LogOutput determines where we should send logs (if anywhere).
func LogOutput() (logOutput io.Writer) {
logOutput = ioutil.Discard
if os.Getenv("MBO_LOG") != "" {
logOutput = os.Stderr
if logPath := os.Getenv("MBO_LOG_PATH"); logPath != "" {
var err error
logOutput, err = os.Create(logPath)
if err != nil {
panic(err)
}
}
}
return
}
// IsLoggedIn detects if the user is logged in. Pass it any html on a logged in page.
func IsLoggedIn(html []byte) bool {
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(html))
selection := doc.Find("#top-wel-sp")
if selection.Length() != 1 {
return false
}
return true
}
// Strip strips the result of goquery.Text(), removing leading/trailing whitespace and nbsp's
func Strip(str string) string {
return strings.Replace(strings.TrimSpace(str), " ", " ", -1)
}