-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
99 lines (82 loc) · 2.4 KB
/
index.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
/**
* Entry point of the program.
*
* Authors : Corentin Forler, Pierre Sibut-Bourde, 2021.
*/
const { randomChoice } = require('./random-choice');
const { getDeputeAsObject } = require('./fetch-informations-depute');
const { deputeToTweets } = require('./stringify-depute');
const { API } = require('./api');
const now = new Date();
const target = new Date();
target.setUTCHours(10, 0, 0);
const dt = ((+now) - (+target)) / 1000;
const shouldRun = Math.abs(dt) <= (5 * 60);
const dryRun = (process.argv[2] === 'dry-run');
function isDeputeValid(depute) {
return (depute.mandats.length > 0);
}
async function main(dryRun = false, nRetries = 3) {
if (dryRun) {
console.log('dry run...');
} else {
console.log('running...');
}
const id = await randomChoice(dryRun);
if (id == null) {
console.error('id is null or undefined');
return process.exit(13);
}
console.log('id:', id);
const depute = await getDeputeAsObject(id);
// const imageData = (await got(depute.imageUrl)).body; // TODO: check exists
if (!isDeputeValid(depute)) {
if (nRetries <= 1) {
console.error('failed (too many attempts)');
return process.exit(11);
} else {
return await main(dryRun, nRetries - 1); // loop
}
}
const tweetTexts = deputeToTweets(depute);
const tweets = tweetTexts.map(text => ({ text }));
if (!dryRun) {
const api = API();
await api.tweetThread(tweets);
} else {
console.log(depute);
console.log(tweets);
}
console.log('done.');
}
if (shouldRun || dryRun) {
main(dryRun).catch(err => {
console.error('ERROR');
console.error(err);
process.exit(12);
});
}
/*
Fix on branch and try beforehand :
const now = changeTimezone(new Date(), 'Europe/Paris');
const time = now.getHours().toString().padStart(2,'0') + ':' + now.getMinutes().toString().padStart(2,'0');
const shouldRun = time > '10:57' && time < '11:03';
const dryRun = (process.argv[2] === 'dry-run');
if (shouldRun || dryRun) {
if (dryRun) {
console.log(`shouldRun = ${shouldRun}`);
}
main(dryRun).catch(err => {
console.error('ERROR');
console.error(err);
process.exit(12);
});
}
// https://stackoverflow.com/a/53652131
function changeTimezone(date, ianatz) {
const invdate = new Date(date.toLocaleString('en-US', { timeZone: ianatz }));
const diff = date.getTime() - invdate.getTime();
return new Date(date.getTime() - diff);
}
and delete lines 64-70
*/