isundil 6 years ago
commit
d85a6ac3ea
1 changed files with 100 additions and 0 deletions
  1. 100 0
      horoscope.js

+ 100 - 0
horoscope.js

@@ -0,0 +1,100 @@
+
+const req = require('request'),
+    { JSDOM } = require('jsdom'),
+    https = require('https');
+
+const SIGNS = {
+    "balance": [ "Pierre", "Paul" ],
+    "gemeaux": [ "Jacque" ]
+    },
+    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 SIGNS = {
+            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 SIGNS[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));
+})();
+