This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsb.go
238 lines (205 loc) · 5.75 KB
/
dsb.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
// Package dsb lets you access content from DSBmobile in golang.
package dsb
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
)
// constants
const (
defaultBundleID = "de.heinekingmedia.dsbmobile"
defaultWebservice = "https://www.dsbmobile.de/JsonHandler.ashx/GetData"
defaultAppVersion = "2.5.9"
defaultLang = "de"
defaultDevice = "Nexus 4"
defaultOsVersion = "27 8.1.0"
defaultUserAgent = "dsb-go"
success = 0
)
// request types
const (
UnknownRequest = iota
DataRequest
MailRequest
FeedbackRequest
SubjectsRequest
)
// NewAccount creates a new account interface
func NewAccount(username string, password string) Account {
return Account{
username: username,
password: password,
BundleID: defaultBundleID,
Webservice: defaultWebservice,
AppVersion: defaultAppVersion,
Lang: defaultLang,
Device: defaultDevice,
OsVersion: defaultOsVersion,
UserAgent: defaultUserAgent,
}
}
// GetData returns all available information of the account
func (account *Account) GetData() (*Response, error) {
JSONdata, err := json.Marshal(map[string]interface{}{
"UserId": account.username,
"UserPw": account.password,
"Abos": []string{},
"AppVersion": account.AppVersion,
"Language": account.Lang,
"OsVersion": account.OsVersion,
"AppId": uuid.New().String(),
"Device": account.Device,
"PushId": "",
"BundleId": account.BundleID,
"Date": time.Now(),
"LastUpdate": time.Now(),
})
if err != nil {
return nil, errors.Wrap(err, "json encode failed")
}
// gzip encode
var gzipBuffer bytes.Buffer
gzipWriter := gzip.NewWriter(&gzipBuffer)
if _, err = gzipWriter.Write(JSONdata); err != nil {
return nil, errors.Wrap(err, "gzip encode failed")
}
gzipWriter.Close()
// base64 encode
base64data := base64.StdEncoding.EncodeToString(gzipBuffer.Bytes())
JSONdata, err = json.Marshal(map[string]interface{}{
"req": map[string]interface{}{
"Data": base64data,
"DataType": DataRequest,
},
})
if err != nil {
return nil, errors.Wrap(err, "2nd json encode failed")
}
// build request
req, err := http.NewRequest("POST", account.Webservice, bytes.NewReader(JSONdata))
if err != nil {
return nil, errors.Wrap(err, "could not create request")
}
// set headers
req.Header.Add("bundle_id", account.BundleID)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Referer", "https://www.dsbmobile.de/default.aspx")
req.Header.Set("User-Agent", account.UserAgent)
// send request
httpClient := &http.Client{
Timeout: time.Second * 10,
}
res, err := httpClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "request failed")
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request failed, status: %s (%d)", res.Status, res.StatusCode)
}
// decode json
var data struct {
Data string `json:"d"`
}
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, errors.Wrap(err, "json unmarshal failed")
}
if data.Data == "" {
return nil, errors.New("got empty response")
}
// base64 decode
decodedBase64, err := base64.StdEncoding.DecodeString(data.Data)
if err != nil {
return nil, errors.Wrap(err, "base64 decode failed")
}
// gzip decode
gzipReader, err := gzip.NewReader(bytes.NewReader(decodedBase64))
if err != nil {
return nil, errors.Wrap(err, "gzip reader failed")
}
defer gzipReader.Close()
// for debugging
//jsonStr, _ := ioutil.ReadAll(gzipReader)
//fmt.Printf("%s\n\n", jsonStr)
// decode json
var response Response
if err := json.NewDecoder(gzipReader).Decode(&response); err != nil {
return nil, errors.Wrap(err, "json unmarshal of response failed")
}
if response.StatusCode != success {
return nil, fmt.Errorf("API request failed: %s (%d)", response.Status, response.StatusCode)
}
return &response, nil
}
// GetCategoryByIndex returns the category with the given index
func (data *Response) GetCategoryByIndex(index int) *Category {
for _, category := range data.Categorys {
if category.Index == index {
return &category
}
}
return nil
}
// GetCategoryByTitle returns the category with the given name
func (data *Response) GetCategoryByTitle(title string) *Category {
for _, category := range data.Categorys {
if category.Title == title {
return &category
}
}
return nil
}
// GetContent returns the category containing the main content
func (account *Account) GetContent() (*Category, error) {
data, err := account.GetData()
if err != nil {
return nil, err
}
return data.GetContent(), nil
}
// GetContent returns the category containing the main content
func (data *Response) GetContent() *Category {
return data.GetCategoryByIndex(0)
}
// GetMenuByMethod returns the menu with the given method
func (category *Category) GetMenuByMethod(method string) *Menu {
for _, menu := range category.Menus {
if menu.Method == method {
return &menu
}
}
return nil
}
// GetTimetables returns all timetables
func (category *Category) GetTimetables() []MenuItem {
timetables := category.GetMenuByMethod("timetable")
if timetables != nil {
return timetables.Root.Childs
}
return []MenuItem{}
}
// GetNews returns all news
func (category *Category) GetNews() []MenuItem {
news := category.GetMenuByMethod("news")
if news != nil {
return news.Root.Childs
}
return []MenuItem{}
}
// GetTiles returns all news
func (category *Category) GetTiles() *Menu {
return category.GetMenuByMethod("tiles")
}
// GetDetail returns the detail property of a timetable
func (menuItem *MenuItem) GetDetail() string {
return menuItem.Childs[0].Detail
}
// GetURL returns the URL of a timetable. Alias for GetDetail()
func (menuItem *MenuItem) GetURL() string {
return menuItem.GetDetail()
}