-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
140 lines (125 loc) · 4.2 KB
/
api.js
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
'use strict';
const scrape = require('./scrape/app.js');
const dataParser = require('./dataParser.js');
const _search = html => {
if(!dataParser.isValidSearch(html)){
throw 'Invalid search';
}
return dataParser.getSearchData(html);
};
const getScheduleByScheduleUrl = url => {
const s = scrape(url);
return new Promise((resolve, reject) => {
if(s.isUrlTypeHtml()){
s.getHtml(url)
.then(html => dataParser.getSearchId(html))
.then(id => {
const jsonUrl = s.replaceHtmlUrlWithJson();
s.getHtml(jsonUrl)
.then(jsonString => JSON.parse(jsonString))
.then(parsedJson => resolve(dataParser.buildSchedule(parsedJson, id)))
.catch(reject);
})
.catch(reject);
}else{
s.getHtml(url)
.then(jsonString => JSON.parse(jsonString))
.then(parsedJson => resolve(dataParser.buildSchedule(parsedJson)))
.catch(reject);
}
});
};
/**
* [gets types name and types value in array]
* @param {[string]} url [timeedit url]
* @return {[promise]} [array of object with all types]
*/
const getAllTypes = url => {
const s = scrape(url);
return new Promise((resolve, reject) => {
s.getHtml(s.getTypeURL())
.then(html => resolve(dataParser.getTypes(html)))
.catch(reject);
});
};
/**
* [check if x exsits]
* @param {[object]} scraper [initilized scraper object]
* @param {[string / array of strings]} id [text for what you want to search for]
* @return {[promise]} [search result]
*/
const search = scraper => id =>
new Promise((resolve, reject) => {
scraper.getHtml(scraper.getSearchURL(id))
.then(html => _search(html))
.then(searchData => resolve(searchData))
.catch(reject);
});
/**
* [schedule over multible days]
* @param {[object]} _getSchedule [initilized _getSchedule object]
* @param {[string / array of strings]} id [name of thing]
* @return {[promise]} [schedule over multible days]
*/
const getSchedule = _getSchedule => id =>
new Promise((resolve, reject) => {
_getSchedule(id)
.then(data => JSON.parse(data))
.then(parsedData => resolve(dataParser.buildSchedule(parsedData, id)))
.catch(reject);
});
/**
* [gets todays schedule]
* @param {[object]} _getSchedule [initilized _getSchedule object]
* @param {[string / array of strings]} id [name of a thing]
* @return {[promise]} [todays schedule]
*/
const getScheduleByDate = _getSchedule => (id, date = new Date()) =>
new Promise((resolve, reject) => {
_getSchedule(id)
.then(data => JSON.parse(data))
.then(parsedData => resolve(dataParser.buildScheduleByDate(parsedData, id, date)))
.catch(reject);
});
/**
* [gets todays schedule]
* @param {[object]} _getSchedule [initilized _getSchedule object]
* @param {[string / array of strings]} id [name of a thing]
* @return {[promise]} [todays schedule]
*/
const getTodaysSchedule = _getSchedule => id =>
getScheduleByDate(_getSchedule)(id);
/**
* [gets schedule for multible days un parsed]
* @param {[object]} scraper [initilized scraper object]
* @param {[string / array of strings]} id [name of a thing]
* @return {[promise]} [unpased schedule over multible days]
*/
const _getSchedule = scraper => id =>
new Promise((resolve, reject) => {
scraper.getHtml(scraper.getSearchURL(id))
.then(html => _search(html))
.then(searchData => searchData.map(a => a.id))
.then(dataIds => scraper.getHtml(scraper.getScheduleURL(dataIds)))
.then(jsonString => resolve(jsonString))
.catch(reject);
});
const getScheduleByItemId = scraper => itemId =>
new Promise((resolve, reject) => {
scraper.getHtml(scraper.getScheduleURL(itemId))
.then(jsonString => resolve(jsonString))
.catch(reject);
});
const api = (url, types) => {
const scraper = scrape(url, types);
return {
getSchedule: getSchedule(_getSchedule(scraper)),
getScheduleByDate: getScheduleByDate(_getSchedule(scraper)),
getTodaysSchedule: getTodaysSchedule(_getSchedule(scraper)),
search: search(scraper),
getScheduleByItemId: getScheduleByItemId(scraper),
getAllTypes,
getScheduleByScheduleUrl
};
};
module.exports = api;