-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequester.js
More file actions
51 lines (43 loc) · 1.43 KB
/
requester.js
File metadata and controls
51 lines (43 loc) · 1.43 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
const request = require('request');
const urlgen = require('./url-generator');
const formatPost = (post) => {
formattedPost = {};
formattedPost.id = post.post.ID;
formattedPost.content = post.post.post_content;
formattedPost.name = post.post.post_name;
formattedPost.title = post.post.post_title;
formattedPost.rent = post.custom_fields._rentprice;
formattedPost.housingType = post.custom_fields._housingType;
formattedPost.area = post.custom_fields._area;
formattedPost.city = post.custom_fields._city;
formattedPost.from = post.custom_fields._availablefrom;
formattedPost.to = post.custom_fields._availableto;
formattedPost.yta = post.custom_fields._availableyta;
formattedPost.gemensanYta = post.custom_fields._availablegemensam;
return formattedPost;
}
const performRequest = (resolve, reject) => {
const options = {
headers: {
'user-agent': 'node.js',
'json': true
}
}
request(urlgen(), options, (error, response, body) => {
if (error) {
reject(error);
return;
}
try {
const posts = JSON.parse(body.toString('utf8'));
const formattedPosts = posts.map(formatPost);
resolve(formattedPosts);
}
catch(err) {
reject(err);
}
});
}
module.exports = () => {
return new Promise(performRequest);
}