-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.js
More file actions
257 lines (239 loc) · 8.28 KB
/
Client.js
File metadata and controls
257 lines (239 loc) · 8.28 KB
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
"use strict";
import DSBAPI from 'dsbapi';
import Promise from 'bluebird';
import request from 'request';
import progress from 'request-progress';
import cheerio from 'cheerio';
import 'datejs';
import {EventEmitter} from 'events';
import percentage from 'percentage-calc';
class DSBClient extends EventEmitter {
/**
*
* @param username {String}
* @param password {String}
* @param [cookiejar] {String}
*/
constructor(username, password, cookiejar) {
super();
this.username = username;
this.password = password;
this.cookiejar = cookiejar;
this.progress = 0;
if (this.cookiejar) {
this.api = new DSBAPI(this.username, this.password, this.cookiejar);
} else {
this.api = new DSBAPI(this.username, this.password);
}
const self = this;
this.api.on('progress', function (p) {
self.progress = percentage.of(p, 50);
self.emit('progress', self.progress);
});
this.fetch = this.fetch.bind(this);
this._filter = this._filter.bind(this);
this._processed_timetable = this._processed_timetable.bind(this);
}
/**
*
* @return {Promise.<TResult>}
*/
fetch() {
const self = this;
self.progress = 0;
return this.api.getData().then(self._filter);
}
/**
*
* @param data {Object}
* @private
*/
_filter(data) {
const self = this;
return new Promise((resolve, reject) => {
if (!data) return reject(new Error("Returned data is null or undefined."));
if (data["Resultcode"] !== 0) return reject(new Error("Data result code isn't 0. Code: " + data["Resultcode"]));
if (!data["ResultMenuItems"]) return reject(new Error("No field ResultMenuItems on returned data."));
if (!Array.isArray(data["ResultMenuItems"])) return reject(new Error("ResultMenuItems isn't an array."));
if (data["ResultMenuItems"].length === 0) return reject(new Error("ResultMenuItems length is 0."));
const ResultMenuItems = data["ResultMenuItems"];
const Inhalte = DSBClient._fromArrayByKeyAndValue(ResultMenuItems, "Title", "Inhalte");
if (Inhalte === false) return reject(new Error("Can't find {Title:'Inhalte'} in 'ResultMenuItems'."));
if (!Inhalte["Childs"]) return reject(new Error("'Childs' in 'Inhalte' is null or undefined."));
if (!Array.isArray(Inhalte["Childs"])) return reject(new Error("'Childs' in 'Inhalte' isn't an array."));
if (Inhalte["Childs"].length === 0) return reject(new Error("The length of 'Childs' in 'Inhalte' is 0."));
let InhalteChilds = Inhalte["Childs"];
return Promise.map(InhalteChilds, function (child) {
return DSBClient._RootResolver(child);
}).then(Childs => {
return Promise.map(Childs, (child, index) => {
InhalteChilds[index]['Childs'] = child;
delete InhalteChilds[index]['Root'];
delete InhalteChilds[index]['NewCount'];
delete InhalteChilds[index]['SaveLastState'];
delete InhalteChilds[index]['Index'];
return Promise.resolve();
}).then(() => {
return Promise.resolve(InhalteChilds);
});
}).then(NewInhalte => {
let fdata = {};
return Promise.map(NewInhalte, (method) => {
if (!method) return Promise.resolve();
if (method['MethodName'] === "timetable") {
return self._processed_timetable(method['Childs']).then(tdata => {
fdata[tdata.kind] = tdata.data;
});
} else if (method['MethodName'] === "tiles") {
return DSBClient._processed_tiles(method['Childs']).then(tdata => {
fdata[tdata.kind] = tdata.data;
});
} else if (method['MethodName'] === "news") {
return DSBClient._processed_news(method['Childs']).then(tdata => {
fdata[tdata.kind] = tdata.data;
});
} else {
return Promise.resolve();
}
}).then(() => {
return Promise.resolve(fdata);
});
}).then(data => {
self.progress = 100;
self.emit('progress', 100);
resolve(data);
});
});
}
/**
*
* @param array {Array}
* @param key {String}
* @param value {String}
* @return {Object|boolean}
* @private
*/
static _fromArrayByKeyAndValue(array, key, value) {
for (let i = 0; i < array.length; i++) {
if (array[i][key] === value) return array[i];
}
return false;
}
/**
*
* @param object
* @return {Promise}
* @private
*/
static _RootResolver(object) {
return new Promise((resolve, reject) => {
if (!object) return reject(new Error("Given parameter is null or undefined."));
if (typeof object !== 'object') return reject(new Error("Given parameter is not an object."));
if (!object['Root']) return reject(new Error("'Root' filed of given object is null or undefined."));
if (typeof object['Root'] !== 'object') return reject(new Error("'Root' field of given object isn't an object."));
if (!object['Root']['Childs']) return reject(new Error("'Childs' field in given object 'Root' is null or undefined."));
if (!Array.isArray(object['Root']['Childs'])) return reject(new Error("'Childs' field in given object 'Root' is not an array."));
resolve(object['Root']['Childs']);
});
}
_processed_timetable(timetables, setProgress, inCount) {
const self = this;
self._timetables = timetables.length;
self._progressTrack = Array.apply(null, Array(self._timetables)).map(Number.prototype.valueOf,0);
self._currentTimetable = 0;
self._everyTimetable = 50 / self._timetables;
self.progress = 50;
function track() {
self.progress = 50 + (self._currentTimetable * self._everyTimetable) + percentage.of(self._progressTrack[self._currentTimetable], self._everyTimetable);
self.emit('progress', self.progress);
}
function pro(p) {
self._progressTrack[self._currentTimetable] = p;
track();
}
return Promise.mapSeries(timetables, (table) => {
if (!table['Childs']) return Promise.reject(new Error("Timetable has no child array."));
if (!Array.isArray(table['Childs'])) return Promise.reject(new Error("Timetable child field is not an array."));
if (table['Childs'].length === 0) return Promise.reject(new Error("Timetable child array length is 0."));
if (typeof table['Childs'][0] !== 'object' || !table['Childs'][0]['Detail']) return Promise.reject(new Error("Corrupted timetable."));
return new Promise((resolve, reject) => {
progress(request({
uri: table['Childs'][0]['Detail']
}, function (error, response, body) {
if (error || response.statusCode !== 200) {
return reject(new Error("Response code was not 200."));
} else {
pro(100);
self._currentTimetable += 1;
const $ = cheerio.load(body);
const MonTitle = $(".mon_title");
if (!MonTitle.text()) return Promise.reject(new Error("Can not find 'mon_title' in timetable."));
if (MonTitle.text().match(/\d*\.\d*\.\d*/).length === 0) return Promise.reject(new Error("Can not find date of timetable."));
return new Promise((resolve, reject) => {
try {
const date = Date.parse(MonTitle.text().match(/\d*\.\d*\.\d*/)[0]);
resolve({
src: table['Childs'][0]['Detail'],
date: Date.parse(MonTitle.text().match(/\d*\.\d*\.\d*/)[0]),
refreshed: Date.parse(table['Childs'][0]['Date'])
});
} catch (e) {
reject(new Error("Can't parse date of timetable."));
}
}).then(resolve).catch(reject);
}
})).on('progress', function (state) {
pro(state.percent * 100);
});
});
}).then(timetables => {
return Promise.resolve({
kind: "timetables",
data: timetables
});
});
}
static _processed_news(newspl) {
return Promise.map(newspl, (news) => {
return Promise.resolve({
date: news["Date"],
title: news["Title"],
text: news["Detail"]
});
}).then(data => {
return Promise.resolve({
kind: "news",
data: data
});
});
}
static _processed_tiles(tiles) {
return Promise.map(tiles, (tile) => {
if (!tile || !tile["Childs"] || !Array.isArray(tile["Childs"])) return Promise.reject(new Error("Corrupted tile."));
return Promise.map(tile["Childs"], (tile2) => {
return Promise.resolve({
date: tile2["Date"],
title: tile2["Title"],
detail: tile2["Detail"]
});
});
}).then(data => {
return Promise.resolve({
kind: "tiles",
data: data.combine()
});
});
}
}
export default DSBClient;
Array.prototype.combine = function () {
let x = [];
for (let i = 0; i < this.length; i++) {
if (Array.isArray(this[i])) {
for (let ii = 0; ii < this[i].length; ii++) {
x.push(this[i][ii]);
}
}
}
return x;
};