| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- const req = require('request'),
- { JSDOM } = require('jsdom'),
- https = require('https');
- const SIGNS = JSON.parse(process.env.SIGNS),
- CHANNEL = "#jenkins-up";
- (function() {
- function upperCaseFirstLetter(str) {
- return str.substr(0, 1).toUpperCase() +str.substr(1).toLowerCase();
- }
- function horoscopesToAttachments(horoscopes) {
- return horoscopes.map(i => {
- return {
- title: i.sign,
- title_link: i.source,
- text: i.text,
- thumb_url: i.image,
- fields: [{
- title: "Personnes",
- value: i.people.join(", "),
- short: false
- }]
- };
- }).sort((a, b) => a.title.localeCompare(b.title));
- }
- function horoscopesToSingleAttachment(horoscopes) {
- const signsEmojis = {
- balance: ":libra:",
- belier: ":aries:",
- taureau: ":taurus:",
- gemeaux: ":gemini:",
- cancer: ":cancer:",
- lion: ":leo:",
- vierge: ":virgo:",
- scorpion: ":scorpius:",
- sagittaire: ":sagittarius:",
- capricorne: ":capricorn:",
- verseau: ":aquarius:",
- poisson: ":pisces:"
- };
- return [{
- text: horoscopes.sort((a, b) => a.sign.localeCompare(b.sign)).map(i => {
- return signsEmojis[i.sign.toLowerCase()] +" *<" +i.source +"|" +i.sign +">*\n" +
- i.text +'\n' +
- "_" +i.people.join(", ") +"_";
- }).join("\n\n")
- }];
- }
- function sendMsg(horoscopes) {
- const req = https.request({
- hostname: 'team-009.slack.com',
- port: 443,
- path: '/services/hooks/jenkins-ci?token=' +process.env.JENKINS_TOKEN,
- method: 'POST',
- }, (res) => {
- console.log(`statusCode: ${res.statusCode}`);
- res.on('data', d => console.log(new String(d)));
- });
- var data = {
- channel: CHANNEL,
- text: "Bien le bonjour. Voici les horoscopes de la semaine.",
- attachments: horoscopesToSingleAttachment(horoscopes)
- };
- req.write(JSON.stringify(data));
- req.end();
- }
- function getHoroscope(sign, people) {
- return new Promise((res, rej) => {
- const url = "http://www.elle.fr/Astro/Horoscope/Hebdo/" +sign;
- req.get(url, (err, _, body) => {
- if (err) {
- console.error("Cannot get horoscope for sign " +sign, err);
- res({ sign: sign });
- return;
- }
- var dom = new JSDOM(body);
- res({
- sign: sign,
- source: url,
- people: people,
- image: "http://knacki.info/zodiac/" +sign.toLowerCase() +".png",
- text: dom.window.document.querySelector(".zone-resultat").textContent
- });
- });
- });
- }
- Promise.all(Object.keys(SIGNS).map(i => getHoroscope(upperCaseFirstLetter(i), SIGNS[i]))).then(results => sendMsg(results));
- })();
|