Created
August 7, 2023 05:48
-
-
Save khanhkhuu/19b112683cff8e5a304c1b54e7cedb67 to your computer and use it in GitHub Desktop.
wordOfTheDay.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wordOfTheDay() { | |
// Library: 1ReeQ6WO8kKNxoaA_O0XEQ589cIrRvEBA9qcWpNqdOP17i47u6N9M5Xh0 | |
const URL = 'https://www.britannica.com/dictionary/eb/word-of-the-day'; | |
const resHtml = UrlFetchApp.fetch(URL).getContentText(); | |
const $ = Cheerio.load(resHtml); | |
const image = $('.wod_img_act > img').attr('src'); | |
const word = $('span.hw_txt').first().text(); | |
const ipa = $('span.hpron_word').first().text(); | |
const imageTitle = $('div.wod_img_tit').first().text(); | |
const text = $('div.midbs').html(); | |
const res = text | |
.replace(/ +/g, ' ') | |
.replace(/\n\s*/g, '') | |
.replace(/<div[^>]*>|<p[^>]*>/g, '') | |
.replace(/<\/div>|<\/p>/g, '\n') | |
.replace(/<strong[^>]*>/g, '<b>') | |
.replace(/<\/strong>/g, '</b>') | |
.replace(/<em[^>]*>/g, '<i>') | |
.replace(/<\/em[^>]*>/g, '</i>') | |
.replace(/<\/*ul[^>]*>/g, '') | |
.replace(/<li[^>]*>/g, '\n\t- ') | |
.replace(/<\/li>/g, '') | |
.replace(/<a\/*[^>]*>/g, '') | |
.replace(/\n+/g, '\n'); | |
const url = 'https://chat.googleapis.com/v1/spaces/AAAAqH84YyQ/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=9mX45Dt1SkUAVyNnKoIUbR0BN7qYnsYENMj1u-CwM7g'; | |
const payload = { | |
cardsV2: [ | |
{ | |
card: { | |
header: { | |
title: `<b>${word} ${ipa}</b>`, | |
subtitle: "Word of the day" | |
}, | |
sections: [ | |
{ | |
widgets: [ | |
{ | |
image: { | |
imageUrl: image | |
} | |
}, | |
{ | |
decoratedText: { | |
text: `<i>${imageTitle}</i>`, | |
} | |
}, | |
{ | |
decoratedText: { | |
text: `<font color=\"#0870a1\"><b>Definition of ${word.toUpperCase()}</b>:</font>` | |
} | |
}, | |
{ | |
textParagraph: { | |
text: res | |
} | |
} | |
] | |
} | |
] | |
} | |
} | |
] | |
} | |
UrlFetchApp.fetch(url, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
payload: JSON.stringify(payload), | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment