horoscope.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const req = require('request'),
  2. { JSDOM } = require('jsdom'),
  3. https = require('https');
  4. const SIGNS = {
  5. "balance": [ "Pierre", "Paul" ],
  6. "gemeaux": [ "Jacque" ]
  7. },
  8. CHANNEL = "#jenkins-up";
  9. (function() {
  10. function upperCaseFirstLetter(str) {
  11. return str.substr(0, 1).toUpperCase() +str.substr(1).toLowerCase();
  12. }
  13. function horoscopesToAttachments(horoscopes) {
  14. return horoscopes.map(i => {
  15. return {
  16. title: i.sign,
  17. title_link: i.source,
  18. text: i.text,
  19. thumb_url: i.image,
  20. fields: [{
  21. title: "Personnes",
  22. value: i.people.join(", "),
  23. short: false
  24. }]
  25. };
  26. }).sort((a, b) => a.title.localeCompare(b.title));
  27. }
  28. function horoscopesToSingleAttachment(horoscopes) {
  29. const SIGNS = {
  30. balance: ":libra:",
  31. belier: ":aries:",
  32. taureau: ":taurus:",
  33. gemeaux: ":gemini:",
  34. cancer: ":cancer:",
  35. lion: ":leo:",
  36. vierge: ":virgo:",
  37. scorpion: ":scorpius:",
  38. sagittaire: ":sagittarius:",
  39. capricorne: ":capricorn:",
  40. verseau: ":aquarius:",
  41. poisson: ":pisces:"
  42. };
  43. return [{
  44. text: horoscopes.sort((a, b) => a.sign.localeCompare(b.sign)).map(i => {
  45. return SIGNS[i.sign.toLowerCase()] +" *<" +i.source +"|" +i.sign +">*\n" +
  46. i.text +'\n' +
  47. "_" +i.people.join(", ") +"_";
  48. }).join("\n\n")
  49. }];
  50. }
  51. function sendMsg(horoscopes) {
  52. const req = https.request({
  53. hostname: 'team-009.slack.com',
  54. port: 443,
  55. path: '/services/hooks/jenkins-ci?token=' +process.env.JENKINS_TOKEN,
  56. method: 'POST',
  57. }, (res) => {
  58. console.log(`statusCode: ${res.statusCode}`);
  59. res.on('data', d => console.log(new String(d)));
  60. });
  61. var data = {
  62. channel: CHANNEL,
  63. text: "Bien le bonjour. Voici les horoscopes de la semaine.",
  64. attachments: horoscopesToSingleAttachment(horoscopes)
  65. };
  66. req.write(JSON.stringify(data));
  67. req.end();
  68. }
  69. function getHoroscope(sign, people) {
  70. return new Promise((res, rej) => {
  71. const url = "http://www.elle.fr/Astro/Horoscope/Hebdo/" +sign;
  72. req.get(url, (err, _, body) => {
  73. if (err) {
  74. console.error("Cannot get horoscope for sign " +sign, err);
  75. res({ sign: sign });
  76. return;
  77. }
  78. var dom = new JSDOM(body);
  79. res({
  80. sign: sign,
  81. source: url,
  82. people: people,
  83. image: "http://knacki.info/zodiac/" +sign.toLowerCase() +".png",
  84. text: dom.window.document.querySelector(".zone-resultat").textContent
  85. });
  86. });
  87. });
  88. }
  89. Promise.all(Object.keys(SIGNS).map(i => getHoroscope(upperCaseFirstLetter(i), SIGNS[i]))).then(results => sendMsg(results));
  90. })();