Skip to content

Instantly share code, notes, and snippets.

@khanhkhuu
Created August 7, 2023 05:48
Show Gist options
  • Save khanhkhuu/19b112683cff8e5a304c1b54e7cedb67 to your computer and use it in GitHub Desktop.
Save khanhkhuu/19b112683cff8e5a304c1b54e7cedb67 to your computer and use it in GitHub Desktop.
wordOfTheDay.js
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