-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobiapi.go
163 lines (147 loc) · 3.76 KB
/
mobiapi.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
package mobiapi
import (
"crypto/tls"
"encoding/json"
"errors"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
)
type MobiAPI struct {
client http.Client
domain string
name string
uid int
signedin bool
OnLostConnection func()
}
func (api *MobiAPI) request(method string, path string, body string) (*http.Response, *goquery.Document, error) {
var resp *http.Response
var doc *goquery.Document
purl, err := url.Parse("https://" + api.domain + "/dziennik/" + path)
if err != nil {
return resp, doc, err
}
req, err := http.NewRequest(method, purl.String(), strings.NewReader(body))
if err != nil {
return resp, doc, err
}
if method == "POST" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
resp, err = api.client.Do(req)
if err != nil {
return resp, doc, err
}
doc, err = goquery.NewDocumentFromResponse(resp)
if err != nil {
return resp, doc, err
}
_, api.signedin = doc.Find("body").Attr("uid")
if !api.signedin && api.OnLostConnection != nil {
api.OnLostConnection()
}
return resp, doc, nil
}
// Check if domain is accessible and if it is, use it. If the domain doesn't contain any dots it will be treated as a mobidziennik.pl subdomain.
func (api *MobiAPI) SetDomain(domain string) error {
if !strings.Contains(domain, ".") {
domain = domain + ".mobidziennik.pl"
}
api.domain = domain
resp, _, err := api.request("GET", "", "")
if err != nil {
return err
}
if resp.StatusCode != 200 || resp.Request.URL.Path == "/zlyadres.php" {
return errors.New("Inaccessible")
}
return nil
}
// Get user's name
func (api *MobiAPI) GetName() string {
return api.name
}
// Get UID
func (api *MobiAPI) GetUID() int {
return api.uid
}
// Set proxy server to use and whenever to allow invalid TLS certificates. Useful for development
func (api *MobiAPI) SetupProxy(proxyurl string, noverifytls bool) error {
if proxyurl == "" {
api.client.Transport = &http.Transport{}
} else {
parsedurl, err := url.Parse(proxyurl)
if err != nil {
return err
}
api.client.Transport = &http.Transport{
Proxy: http.ProxyURL(parsedurl),
TLSClientConfig: &tls.Config{InsecureSkipVerify: noverifytls},
}
if _, err := api.client.Get("https://www.mobidziennik.pl"); err != nil {
api.client.Transport = &http.Transport{}
return err
}
}
return nil
}
// Check if still signed in
func (api *MobiAPI) LoggedIn(noprecache bool) bool {
if noprecache {
resp, err := api.client.Get("https://" + api.domain + "/helper/sprawdzzalogowanie?uid=" + strconv.Itoa(api.GetUID()) + "&extendSession=0")
if err != nil {
return false
}
str := make([]byte, 128)
n, err := resp.Body.Read(str)
if err != nil {
return false
}
data := map[string]int{}
json.Unmarshal(str[:n], &data)
if v, ok := data["zalogowany"]; ok && v == 1 {
return true
}
return false
}
return api.signedin
}
// Does a random request to extend session
func (api *MobiAPI) ExtendSession() error {
_, _, err := api.request("POST", "", "")
return err
}
// Logout from mobiDziennik
func (api *MobiAPI) Logout() (bool, error) {
resp, _, err := api.request("GET", "wyloguj", "")
if err != nil {
return false, err
}
if resp.Request.Response.StatusCode == 302 {
api = nil
return true, nil
}
return false, nil
}
// Create new instance of MobiAPI.
func New(domain string) (*MobiAPI, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
api := &MobiAPI{
client: http.Client{
Jar: jar,
},
}
if len(domain) > 0 {
if err := api.SetDomain(domain); err != nil {
return api, err
}
}
return api, nil
}