-
Notifications
You must be signed in to change notification settings - Fork 0
/
cesi.go
155 lines (127 loc) · 2.66 KB
/
cesi.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
package cesi
import (
"crypto/tls"
"fmt"
"github.com/kataras/golog"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path"
"strings"
"time"
)
type Options struct {
Email string
Password string
UserAgent string
Debug bool
}
type Session struct {
ID string
}
type Cesi struct {
client *http.Client
username string
password string
useragent string
debug bool
User *User
}
func New(o *Options) *Cesi {
if o.Debug {
golog.SetLevel("debug")
}
if o.UserAgent == "" {
o.UserAgent = "go-cesi v0.1"
}
c := newHttpClient()
ces := &Cesi{
useragent: o.UserAgent,
username: o.Email,
password: o.Password,
client: c,
debug: o.Debug,
}
ces.User = &User{
cesi: ces,
Session: &Session{},
Establishments: []*Establishment{},
}
return ces
}
func (c *Cesi) Get(url string) (*http.Response, error) {
req, err := c.newRequest("GET", url, nil)
if err != nil {
return nil, err
}
return c.client.Do(req)
}
func (c *Cesi) Post(url string, body io.Reader) (*http.Response, error) {
req, err := c.newRequest("POST", url, body)
if err != nil {
return nil, err
}
return c.client.Do(req)
}
func (c *Cesi) PostForm(url string, data url.Values) (*http.Response, error) {
req, err := c.newRequest("POST", url, strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
return c.client.Do(req)
}
func (c *Cesi) newRequest(method, url string, body io.Reader) (*http.Request, error) {
golog.Debug("[NewRequest] " + method + " " + url)
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", c.useragent)
return req, nil
}
func newHttpClient() *http.Client {
return &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
golog.Debug("Redirect: " + req.URL.String())
return nil
},
Jar: newCookieJar(),
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
func newCookieJar() http.CookieJar {
cookieJar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
return cookieJar
}
func saveResponse(r io.ReadCloser) {
if _, err := os.Stat("debug"); os.IsNotExist(err) {
err := os.Mkdir("debug", 0755)
if err != nil {
panic(err)
}
}
name := fmt.Sprintf("debug-%d.html", time.Now().Unix())
out, err := os.Create("debug/" + name)
if err != nil {
panic(err)
}
defer out.Close()
_, err = io.Copy(out, r)
if err != nil {
panic(err)
}
wd, err := os.Getwd()
if err != nil {
panic(err)
}
p := path.Join(wd, "debug", name)
golog.Debug("Debug file saved: " + p)
}