-
Notifications
You must be signed in to change notification settings - Fork 0
/
curate.js
67 lines (60 loc) · 2.27 KB
/
curate.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
var request = require('request');
var exhibition = {
"title": "",
"url": "",
"desc": ""
};
exports.exhibition = exhibition;
exports.get_exhibition = function(callback) { request(
{ url: 'https://api.paris.fr/api/data/2.2/QueFaire/get_events/?token=4f3ff60a873f39938f7f00c43dad49e9e94a83a044f539de962299bbc09630d2&categories=exposition&tags=&start=&end=0&offset=&limit=' },
function (error, response, body) {
if (!error && response.statusCode === 200) {
select_exhibition_to_tweet(body);
callback(null, exhibition);
} else {
console.log('error while calling Paris.fr API', error);
console.log('statusCode:', response && response.statusCode);
}
}
);
}
function select_exhibition_to_tweet(body) {
var exhibitions = JSON.parse(body).data;
var exhibitions_ending_soon_ = exhibitions_ending_soon(exhibitions, deadline_by(21));
var exhibition_to_tweet = exhibitions_ending_soon_[0];
//tests
//console.log('last event date format:', typeof exhibitions[0].evenements.realDateEnd);
//console.log('test_deadline_in_21_days', deadline_by(21));
//console.log('test_first_exhibition_closing', close_before(exhibitions[0], deadline_by(150)));
//console.log('test_exhibitions_closing_in_21_days', exhibitions_ending_soon(exhibitions, deadline_by(21)));
//console.log('exhibition to tweet', exhibition_to_tweet);
exhibition = exhibition_summary(exhibition_to_tweet);
console.log('chosen exhibition tweet content', exhibition_summary(exhibition_to_tweet));
}
function exhibition_summary(exhibition) {
return {
"title": exhibition.title,
"url": exhibition.shortenedUrl,
"desc": exhibition.leadText
};
}
function deadline_by(nb_days) {
var today = new Date ();
var deadline = new Date();
deadline.setDate(today.getDate() + nb_days);
return deadline;
}
function exhibitions_ending_soon(all_exhibitions, deadline) {
var exhibitions_ending_soon = [];
all_exhibitions.forEach(function(exhibition) {
if (close_before(exhibition, deadline)) {
exhibitions_ending_soon.push(exhibition);
}
}
);
return exhibitions_ending_soon;
}
function close_before(exhibition, deadline) {
exhibition_closing_date = new Date (exhibition.evenements.realDateEnd);
return exhibition_closing_date <= deadline;
}