-
Notifications
You must be signed in to change notification settings - Fork 1
/
dijnet.go
316 lines (276 loc) · 6.55 KB
/
dijnet.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package dijnet
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"github.com/PuerkitoBio/goquery"
"golang.org/x/text/encoding/htmlindex"
)
func NewService() Service {
cookieJar, _ := cookiejar.New(nil)
return Service{
client: &http.Client{
Jar: cookieJar,
},
baseURL: "https://www.dijnet.hu",
}
}
type Service struct {
client *http.Client
baseURL string
}
func isRequestOrderRight(body string) bool {
if strings.Contains(string(body), "Kérjük, csak az oldalon található gombokat és hivatkozásokat használja!") {
return false
}
return true
}
func decodeHTMLBody(body io.Reader) io.Reader {
e, _ := htmlindex.Get("iso-8859-2")
return e.NewDecoder().Reader(body)
}
type loginResponse struct {
Success bool `json:"success"`
URL string `json:"url"`
Error string `json:"error"`
}
func (s Service) login(username string, password string) (loginResponse, error) {
payload := url.Values{}
payload.Set("username", username)
payload.Set("password", password)
req, err := http.NewRequest(http.MethodPost,
s.baseURL+"/ekonto/login/login_check_ajax",
strings.NewReader(payload.Encode()),
)
if err != nil {
return loginResponse{}, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.Do(req)
if err != nil {
return loginResponse{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return loginResponse{}, fmt.Errorf("status code error: %d %s", resp.StatusCode, resp.Status)
}
var status loginResponse
err = json.NewDecoder(resp.Body).Decode(&status)
if err != nil {
return loginResponse{}, err
}
return status, nil
}
func (s Service) visitMain(mainURL string) error {
req, err := http.NewRequest("GET", s.baseURL+mainURL, nil)
if err != nil {
return err
}
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code error: %d %s", resp.StatusCode, resp.Status)
}
return nil
}
func (s Service) Login(username string, password string) error {
status, err := s.login(username, password)
if err != nil {
return err
}
if !status.Success {
return fmt.Errorf("unable to login: %s", status.Error)
}
err = s.visitMain(status.URL)
if err != nil {
return err
}
return nil
}
func (s Service) Providers() ([]string, error) {
var ret []string
resp, err := s.client.Get(s.baseURL + "/ekonto/control/szamla_search")
if err != nil {
return ret, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return ret, fmt.Errorf("status code error: %d %s", resp.StatusCode, resp.Status)
}
body, err := ioutil.ReadAll(decodeHTMLBody(resp.Body))
if err != nil {
return ret, err
}
r, _ := regexp.Compile("sopts.add\\('(.+)'\\)")
m := r.FindAllStringSubmatch(string(body), -1)
for _, e := range m {
ret = append(ret, e[1])
}
return ret, err
}
type Invoice struct {
ID string
Provider string
IssuerID string
InvoiceID string
DateOfIssue time.Time
Total int
PaymentDeadline time.Time
Payable int
Status string
}
type InvoicesQuery struct {
Provider string
IssuerID string
From time.Time
To time.Time
}
func cleanNumber(r rune) rune {
if unicode.IsNumber(r) {
return r
}
return -1
}
func (s Service) Invoices(query InvoicesQuery) ([]Invoice, error) {
var ret []Invoice
e, _ := htmlindex.Get("iso-8859-2")
provider, err := e.NewEncoder().String(query.Provider)
if err != nil {
return ret, err
}
dateLayout := "2006.01.02"
var from, to string
if !query.From.IsZero() {
from = query.From.Format(dateLayout)
}
if !query.To.IsZero() {
to = query.To.Format(dateLayout)
}
payload := url.Values{}
payload.Set("vfw_form", "szamla_search_submit")
payload.Set("vfw_coll", "szamla_search_params")
payload.Set("szlaszolgnev", provider)
payload.Set("regszolgid", query.IssuerID)
payload.Set("datumtol", from)
payload.Set("datumig", to)
req, err := http.NewRequest(http.MethodPost,
s.baseURL+"/ekonto/control/szamla_search_submit",
strings.NewReader(payload.Encode()),
)
if err != nil {
return ret, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.Do(req)
if err != nil {
return ret, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return ret, fmt.Errorf("status code error: %d %s", resp.StatusCode, resp.Status)
}
doc, err := goquery.NewDocumentFromReader(decodeHTMLBody(resp.Body))
if err != nil {
return ret, err
}
doc.Find(".sortable tr").Each(func(_ int, s *goquery.Selection) {
id, _ := s.Attr("id")
invoice := Invoice{ID: strings.Split(id, "_")[1]}
s.Find("td").Each(func(i int, s *goquery.Selection) {
switch i {
case 0:
invoice.Provider = s.Text()
case 1:
invoice.IssuerID = s.Text()
case 2:
invoice.InvoiceID = s.Text()
case 3:
invoice.DateOfIssue, _ = time.Parse(dateLayout, s.Text())
case 4:
n := strings.Map(cleanNumber, s.Text())
invoice.Total, _ = strconv.Atoi(n)
case 5:
invoice.PaymentDeadline, _ = time.Parse(dateLayout, s.Text())
case 6:
n := strings.Map(cleanNumber, s.Text())
invoice.Payable, _ = strconv.Atoi(n)
case 7:
invoice.Status = s.Text()
}
})
ret = append(ret, invoice)
})
return ret, nil
}
func (s Service) downloadFile(URL string, filename string) error {
resp, err := s.client.Get(URL)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(filename)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func (s Service) DownloadInvoice(i Invoice, pdf string, xml string) error {
resp, err := s.client.Get(
s.baseURL + "/ekonto/control/szamla_select?vfw_coll=szamla_list&vfw_rowid=" + i.ID + "&exp=K",
)
if err != nil {
return err
}
err = resp.Body.Close()
if err != nil {
return err
}
resp, err = s.client.Get(
s.baseURL + "/ekonto/control/szamla_letolt",
)
if err != nil {
return err
}
err = resp.Body.Close()
if err != nil {
return err
}
if pdf != "" {
err = s.downloadFile(s.baseURL+"/ekonto/control/szamla_pdf", pdf)
if err != nil {
return err
}
}
if xml != "" {
err = s.downloadFile(s.baseURL+"/ekonto/control/szamla_xml", xml)
if err != nil {
return err
}
}
resp, err = s.client.Get(
s.baseURL + "/ekonto/control/szamla_list",
)
if err != nil {
return err
}
err = resp.Body.Close()
if err != nil {
return err
}
return nil
}