-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
178 lines (142 loc) · 4.12 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
)
// TopLevel of the output
type TopLevel struct {
Deliveries []Deliveries `json:"deliveries,omitempty"`
}
// Deliveries from the outpu
type Deliveries struct {
Params Params `json:"params,omitempty"`
}
// Params from the output
type Params struct {
Matrix []Matrix `json:"matrix,omitempty"`
}
// Matrix from the output
type Matrix struct {
Day string `json:"day"`
Hours []Hours `json:"hours,omitempty"`
}
// Hours from the output
type Hours struct {
Available bool `json:"available"`
Hour string `json:"hour"`
}
func notifySlack(day string, hour string, available bool, webhook string) {
client := &http.Client{
Timeout: 60 * time.Second,
}
// payload to HAProxy Data Plane API
requestBody, err := json.Marshal(map[string]string{
"text": "Slot available! " + day + " " + hour + " valandą",
})
if err != nil {
panic(err)
}
request, err := http.NewRequest("POST", webhook, bytes.NewBuffer(requestBody))
if err != nil {
panic(err)
}
request.Header.Add("Content-Type", "application/json")
response, err := client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
// Get the response body as a string
dataInBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
pageContent := string(dataInBytes)
fmt.Println(pageContent)
}
func main() {
email := flag.String("email", "", "login email address")
password := flag.String("password", "", "login password")
webhook := flag.String("webhook", "", "Slack Webhook URL")
flag.Parse()
data := url.Values{}
data.Set("rememberMe", "true")
data.Set("email", *email)
data.Set("password", *password)
formData := strings.NewReader(data.Encode())
cookieJar, _ := cookiejar.New(nil)
client := &http.Client{
Timeout: 60 * time.Second,
Jar: cookieJar,
}
request, err := http.NewRequest("POST", "https://www.barbora.lt/api/eshop/v1/user/login", formData)
if err != nil {
panic(err)
}
expiration := time.Now().Add(365 * 24 * time.Hour)
regionCookie := http.Cookie{Name: "region", Value: "barbora.lt", Expires: expiration}
request.AddCookie(®ionCookie)
request.Header.Add("Authorization", "Basic YXBpa2V5OlNlY3JldEtleQ==")
request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0")
request.Header.Add("Pragma", "no-cache")
request.Header.Add("Cache-Control", "no-cache")
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
request.Header.Add("Connection", "keep-alive")
response, err := client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
// Get the response body as a string
dataInBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
pageContent := string(dataInBytes)
fmt.Println(pageContent)
// ------------------------------------------------------------------------------------------------------------------------- //
request, err = http.NewRequest("GET", "https://www.barbora.lt/api/eshop/v1/cart/deliveries", nil)
if err != nil {
panic(err)
}
request.AddCookie(®ionCookie)
request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0")
request.Header.Add("Pragma", "no-cache")
request.Header.Add("Cache-Control", "no-cache")
request.Header.Add("Authorization", "Basic YXBpa2V5OlNlY3JldEtleQ==")
response, err = client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
decoder := json.NewDecoder(response.Body)
var val TopLevel
err = decoder.Decode(&val)
if err != nil {
log.Fatal(err)
}
err = decoder.Decode(&val)
for _, param := range val.Deliveries {
for _, matrix := range param.Params.Matrix {
fmt.Println(matrix.Day)
for _, hour := range matrix.Hours {
fmt.Println("Delivery time:", hour.Hour)
fmt.Println("Available:", hour.Available)
if hour.Available == true {
notifySlack(matrix.Day, hour.Hour, hour.Available, *webhook)
}
fmt.Println()
}
fmt.Println("+------------------------------+")
}
}
}