-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (78 loc) · 2.17 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
const Twit = require("twit");
const axios = require("axios");
require("dotenv").config();
const T = new Twit({
consumer_key: process.env.API_KEY,
consumer_secret: process.env.API_KEY_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
});
const URL = "https://www.nostalgie.fr/chansons-diffusees.json?yesterday=0";
const DEBUG = true;
let formattedSong = "";
let songs = [];
let times = 0;
const tweet = (text, debug = true) => {
if (debug) {
console.log(text);
return;
}
T.post("statuses/update", {
status: text,
function(err, data, response) {
console.log(data);
},
});
};
const formatTitle = (song, from, to) => {
return song.charAt(0).toUpperCase() + song.slice(from, to).toLowerCase();
};
const getTime = () => {
const d = new Date();
const hr = d.getHours();
const min = d.getMinutes();
if (min < 10) {
min = "0" + min;
}
return hr + ":" + (min < 10 ? "0" + min : min);
};
const app = () => {
axios
.get(URL)
.then(({ data }) => {
for (let song of data) {
if (song.artist === "DANIEL BALAVOINE") {
times += 1;
if (song.title === "JE NE SUIS PAS UN HEROS C") {
formattedSong = `"${formatTitle(song.title, 1, -2)}"`;
} else {
formattedSong = `"${formatTitle(song.title, 1)}"`;
}
songs.push(formattedSong);
}
}
const uniqueSongs = songs.filter((value, index, self) => {
return self.indexOf(value) === index;
});
if (uniqueSongs.length > 1) {
uniqueSongs.splice(uniqueSongs.length - 1, 0, "et");
}
if (songs.length > 0) {
tweet(
`Aujourd'hui et jusqu'à ${getTime()}, Daniel Balavoine est passé ${times} fois sur radio Nostalgie @nostalgiefm avec ${
uniqueSongs.length === 1 ? "la chanson" : "les chansons"
} ${uniqueSongs.join(" ")}`,
DEBUG
);
} else {
tweet(
`Daniel Balavoine n'est pas encore passé sur radio Nostalgie @nostalgiefm aujourd'hui`,
DEBUG
);
}
})
.catch((error) => {
console.log(error);
});
};
app();