/
shopper.go
114 lines (105 loc) · 2.73 KB
/
shopper.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
package internal
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strings"
"golang.org/x/crypto/ssh/terminal"
)
type Shopper struct {
http.Client
}
func NewShopperWithCookies(cookies []*http.Cookie) (*Shopper, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
u, err := url.Parse(BaldorHost)
if err != nil {
return nil, err
}
cookieJar.SetCookies(u, cookies)
return &Shopper{http.Client{Jar: cookieJar}}, nil
}
func NewShopperWithAuthentication(email, pass string) (*Shopper, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
shopper := &Shopper{http.Client{Jar: cookieJar}}
if _, err := shopper.PostForm(URLNewLogin, url.Values{
"EmailLoginForm[email]": {email},
"EmailLoginForm[password]": {pass},
"EmailLoginForm[rememberMe]": {"1"},
"yt0": {"SIGN IN"},
}); err != nil {
return nil, err
}
if _, err := shopper.Get(URLCart); err != nil {
return nil, err
}
return shopper, nil
}
func (s *Shopper) AddToCart(productKey, unit string) (int, error) {
v := url.Values{
"ShoppingCartModel[key]": {productKey},
"ShoppingCartModel[unit]": {unit},
"ShoppingCartModel[quantity]": {"1"},
"qty": {"1"},
}
resp, err := s.PostForm(URLAddToCart, v)
if err != nil {
return resp.StatusCode, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return http.StatusInternalServerError, err
}
if strings.Contains(string(b), "error") {
// The response always returns a status 200, even when there is an
// error.
return http.StatusInternalServerError, fmt.Errorf("Error adding to cart: %q; values = %v", string(b), v)
}
log.Printf("Success! %q\n", string(b))
return http.StatusOK, nil
}
func BaldorCookie(j http.CookieJar) (*http.Cookie, error) {
u, err := url.Parse(BaldorHost)
if err != nil {
return nil, err
}
cookie := &http.Cookie{}
for _, c := range j.Cookies(u) {
if c.Name == "provided_access" || c.Name == "PHPSESSID" || c.Name == "SESSION" {
continue
}
log.Printf("BALDORFOOD_COOKIE_NAME=%q\n", c.Name)
log.Printf("BALDORFOOD_COOKIE_VALUE=%q\n", c.Value)
cookie.Name = c.Name
cookie.Value = c.Value
}
if cookie.Name == "" {
return nil, fmt.Errorf("Authentication cookie not found")
}
return cookie, nil
}
func ReadEmailAndPassword() (string, string, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Enter email: ")
email, err := reader.ReadString('\n')
if err != nil {
return "", "", err
}
fmt.Printf("Enter password: ")
pass, err := terminal.ReadPassword(0)
if err != nil {
log.Fatal(err)
}
return strings.TrimSuffix(email, "\n"), strings.TrimSuffix(string(pass), "\n"), nil
}