Created
March 23, 2024 22:52
-
-
Save itxtoledo/161e8352991d0aef65dde772533b0c27 to your computer and use it in GitHub Desktop.
Script para gerar resumo das melhores noticias do site The Defiant e obter um resumo do mercado pela api do Coinsamba
This file contains hidden or 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
import Parser from "rss-parser"; | |
import OpenAI from "openai"; | |
import axios from "axios"; | |
import clipboardy from "clipboardy"; | |
let parser = new Parser(); | |
const api = new OpenAI({ | |
apiKey: "paste_here", | |
}); | |
function obterDataFormatada() { | |
const hoje = new Date(); | |
const dia = String(hoje.getDate()).padStart(2, "0"); | |
const mes = String(hoje.getMonth() + 1).padStart(2, "0"); | |
return `${dia} - ${mes}`; | |
} | |
(async () => { | |
let feed = await parser.parseURL("https://thedefiant.io/api/feed"); | |
console.log(feed.title); | |
const news = []; | |
feed.items.slice(0, 15).forEach((item) => { | |
if (item.content.length < 200 && item.content.length > 50) { | |
news.push({ | |
title: item.title, | |
content: item.content, | |
}); | |
} | |
}); | |
const response = await api.chat.completions.create({ | |
model: "gpt-3.5-turbo", | |
messages: [ | |
{ | |
role: "system", | |
content: "You are a news specialist.", | |
}, | |
{ | |
role: "user", | |
content: `Filter the top 5 news articles from the given array and return only a JSON RFC 8259 format without any aditional text: ${JSON.stringify( | |
news | |
)}`, | |
}, | |
], | |
max_tokens: 1024, | |
}); | |
// console.log(response); | |
const hottestNews = JSON.parse(response.choices[0].message.content); | |
console.log(hottestNews); | |
console.log("==========="); | |
const response2 = await api.chat.completions.create({ | |
model: "gpt-3.5-turbo", | |
messages: [ | |
{ | |
role: "system", | |
content: | |
"Você é um especialista em traduzir e melhorar textos em JSON.", | |
}, | |
{ | |
role: "user", | |
content: `Traduza para o português brasileiro, melhore o "title" e o "content" das notícias abaixo, estendendo o texto do "content" para 470 chars, retorne apenas um array no formato JSON RFC 8259 sem nenhum texto ou parametro adicional: ${JSON.stringify( | |
hottestNews | |
)}`, | |
}, | |
], | |
max_tokens: 1024, | |
}); | |
const hottestNewsImproved = JSON.parse(response2.choices[0].message.content); | |
console.log(hottestNewsImproved); | |
const { data: indexes } = await axios.get( | |
"https://api.coinsamba.com/v0/indexes?quote=BRL" | |
); | |
const btc = indexes.find((i) => i.base === "BTC"); | |
const eth = indexes.find((i) => i.base === "ETH"); | |
let message = `${obterDataFormatada()} Coinsamba News\n\n`; | |
message += "Bom dia,\n\n"; | |
message += `Bitcoin com variação de ${btc.change.toFixed( | |
2 | |
)}%, enquanto o Ethereum varia ${eth.change.toFixed(2)}%.\n\n`; | |
hottestNewsImproved.forEach((h) => { | |
message += `*The Defiant - ${h.title}*\n${h.content}\n\n`; | |
}); | |
console.log(message); | |
clipboardy.writeSync(message); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment