Created
June 12, 2020 20:30
-
-
Save reichert621/8ad2cbaf843cdd629a6e26b01a3e1d49 to your computer and use it in GitHub Desktop.
Send top Hacker News posts to Slack and Google Sheets
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
const Taro = require('taro-client')( | |
'__YOUR_API_KEY__' | |
); | |
const main = async () => { | |
const posts = await Taro.scrape.hn({threshold: 400}); | |
if (posts.length === 0) { | |
return; | |
} | |
// Finds or creates a Google Sheet called "HN Posts" | |
const token = await Taro.sheets.load('HN Posts'); | |
// Retrieves the data from the spreadsheet as JSON | |
const {data: existing} = await Taro.sheets.retrieve(token); | |
const existingLinks = existing.map(e => e.link); | |
// Filter out links that were logged already | |
const filtered = posts.filter(post => { | |
return existingLinks.indexOf(post.link) === -1; | |
}); | |
if (filtered.length === 0) { | |
console.log('No new posts!'); | |
return; | |
} | |
const items = filtered.map(({text, link, score}) => { | |
return `> <${link}|[${score} points] ${text}>`; | |
}); | |
const message = ['Top posts on HN:', ...items].join('\n'); | |
// Send new posts to Slack | |
await Taro.notify.slack({message: message}); | |
// Add new posts to the spreadsheet | |
await Taro.sheets.append(token, filtered); | |
console.log('Message successfully sent!', message); | |
}; | |
module.exports = main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment