Skip to content

Instantly share code, notes, and snippets.

@faustoct1
Last active May 3, 2024 00:52
Show Gist options
  • Save faustoct1/4dd995e16b5530661f5e980a14740e2f to your computer and use it in GitHub Desktop.
Save faustoct1/4dd995e16b5530661f5e980a14740e2f to your computer and use it in GitHub Desktop.
tweetTabNewsRanking.js
const fetch = require('node-fetch')
const { TwitterApi } = require( 'twitter-api-v2')
const getTabNews = async () => {
const response = await fetch(`https://www.tabnews.com.br/api/v1/contents?page=1&per_page=50&strategy=relevant`)
return await response.json()
}
const tweet = async (text,links) => {
console.log('tweet tab news ranking started')
if(!text) return null
const TWITTER_CREDENTIALS = JSON.parse('{"appKey":"","appSecret":"","accessToken":"","accessSecret":""}')
const client = new TwitterApi(TWITTER_CREDENTIALS)
const tweet = await client.v2.tweet(text)
await client.v2.reply(links,tweet.data.id)
console.log('tweet tab news ranking finished')
}
exports.tweetTabNewsRanking = async () => {
try{
console.log('runTabNews started')
const results = await getTabNews()
try{
if(results.length){
const posts = results.filter(result=>{
//isYesterday?
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
const date = new Date(result.created_at);
return (
date.getFullYear() === yesterday.getFullYear() &&
date.getMonth() === yesterday.getMonth() &&
date.getDate() === yesterday.getDate()
)
}).sort((a,b) => b.tabcoins - a.tabcoins)
if(posts && posts.length) {
const ranking = posts.slice(0,3).map((item,index)=>{
if(index===0){
return `🥇 ${item.owner_username} • ${item.tabcoins} tabcoins\nhttps://www.tabnews.com.br/${item.owner_username}/${item.slug}`
} else if(index===1){
return `🥈 ${item.owner_username} • ${item.tabcoins} tabcoins`
} else if(index===2){
return `🥉 ${item.owner_username} • ${item.tabcoins} tabcoins`
}
})
const links = posts.reverse().slice(0,2).reverse().map((item,index)=>{
if(index===0){
return `🥉 ${item.owner_username} • ${item.title}\nhttps://www.tabnews.com.br/${item.owner_username}/${item.slug}`
} else if(index===1){
return `🥈 ${item.owner_username} https://www.tabnews.com.br/${item.owner_username}/${item.slug}`
}
})
tweet(`${ranking.join('\n\n')}`,links.join('\n\n'))
}
}
}catch(e){
console.log(e)
}
console.log('runTabNews finished')
}catch(e){
console.log(e)
}
}
//https://www.tabnews.com.br/api/v1/analytics/root-content-published
//https://www.tabnews.com.br/api/v1/contents?page=1&per_page=50&strategy=new
(async ()=>{
const { exit } = require("process")
await this.tweetTabNewsRanking()
exit(0)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment