-
Notifications
You must be signed in to change notification settings - Fork 3
/
moonApi.go
188 lines (153 loc) · 4.48 KB
/
moonApi.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
package moonapi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
. "github.com/cstdev/moonapi/query"
"github.com/golang/glog"
"golang.org/x/net/publicsuffix"
"gopkg.in/headzoo/surf.v1"
)
// AuthToken contains the values of the cookie required to be used as authentication
// against the website.
type AuthToken struct {
Name string
Value string
}
// MoonBoardApi provides methods for interacting with the website
type MoonBoardApi interface {
Login(username string, password string) error
GetProblems(query Query) (MbResponse, error)
Auth() []AuthToken
SetAuth(authTokens []AuthToken)
}
// MoonBoard contains all the AuthTokens (cookies) required
type MoonBoard struct {
auth []AuthToken
}
const baseUrl string = "https://moonboard.com/"
const loginUrl = "Account/Login"
const getProblemsUrl = "Problems/GetProblems"
// Login takes a username and password, then attempts to use these to
// enter into the website's login form and submit it, storing the resulting
// cookies as AuthTokens
func (m MoonBoard) Login(username string, password string) error {
fmt.Printf("Hi %s\n", username)
bow := surf.NewBrowser()
err := bow.Open(baseUrl + loginUrl)
if err != nil {
glog.Info("Unable to open Login Page.")
return err
}
fm, err := bow.Form("#frmLogin")
if err != nil {
glog.Info("Unable to find Login form.")
return err
}
fm.Input("Login.Username", username)
fm.Input("Login.Password", password)
fm.Input("Login.RememberMe", "false")
if fm.Submit() != nil {
glog.Info("Failed to submit log-in")
return errors.New("Failed to submit log-in")
}
var response []AuthToken
var successResponse = false
for _, cookie := range bow.SiteCookies() {
token := AuthToken{
Name: cookie.Name,
Value: cookie.Value,
}
if cookie.Name == "_MoonBoard" {
successResponse = true
}
response = append(response, token)
}
if !successResponse {
//fmt.Printf("Response: %v", response)
return errors.New("failed to log-in, moonboard cookie not returned")
}
m.auth = response
return nil
}
func tokenToCookie(token AuthToken) *http.Cookie {
return &http.Cookie{
Name: token.Name,
Value: token.Value,
}
}
// GetProblems can be called on a session object to return all problems that
// match the provided critieria from the Query passed in.
// It requires the session to provide the
// _MoonBoard and __RequestVerificationToken AuthToken
// errors are retuned if these are missing or the session has expired.
func (m MoonBoard) GetProblems(query Query) (MbResponse, error) {
v := url.Values{}
v.Set("page", strconv.Itoa(query.Page()))
v.Add("pageSize", strconv.Itoa(query.PageSize()))
v.Add("group", "")
v.Add("sort", query.Sort())
v.Add("filter", query.Filter())
res := MbResponse{}
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
var cookies []*http.Cookie
if len(m.auth) == 0 {
return res, errors.New("Required _Moonboard or __RequestVerificationToken Auth Tokens missing")
}
containsAuth := false
for _, token := range m.auth {
if token.Name == "_MoonBoard" {
containsAuth = true
break
}
}
if !containsAuth {
return res, errors.New("Required _Moonboard or __RequestVerificationToken Auth Tokens missing")
}
cookies = append(cookies, tokenToCookie(m.auth[0]))
cookies = append(cookies, tokenToCookie(m.auth[1]))
u, _ := url.Parse(baseUrl)
jar.SetCookies(u, cookies)
bow := surf.NewBrowser()
bow.SetCookieJar(jar)
err := bow.PostForm(baseUrl+getProblemsUrl, v)
if err != nil {
return res, err
}
if strings.Contains(bow.Url().String(), "/Account/Login") {
//fmt.Println("Session Exprired")
return res, errors.New("session expired, please log in")
}
if bow.StatusCode() != 200 {
return res, errors.New("Server returned error status: " + strconv.Itoa(bow.StatusCode()))
}
response := strings.Replace(bow.Body(), """, "\"", -1)
//fmt.Printf("Response: %v \n", response)
err = json.Unmarshal([]byte(response), &res)
if err != nil {
//fmt.Println("Error on unmarshal")
return res, err
}
return res, nil
}
func (m MoonBoard) Auth() []AuthToken {
return m.auth
}
func (m MoonBoard) SetAuth(authTokens []AuthToken) {
m.auth = authTokens
}
// ProblemsAsJSON takes an array of Problem and returns
// the JSON representation of those problems.
// Errors are returned if it fails to marshal the object.
func ProblemsAsJSON(problems []Problem) (string, error) {
out, err := json.Marshal(problems)
if err != nil {
return "", err
}
return string(out), nil
}