Skip to content

Commit

Permalink
feat: Finance
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasm committed Sep 2, 2024
1 parent d1f1f6a commit b60d654
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 17 deletions.
6 changes: 5 additions & 1 deletion components/WidgetTrends/WidgetTrends.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
.trendsImage ul li button,
.trendsMusic ul li button,
.trendsNews ul li button,
.trendsFinance ul li button,
.trendsShopping ul li button {
gap: 0;
padding: 0;
Expand All @@ -66,21 +67,24 @@
.trendsImage ul li button span,
.trendsMusic ul li button span,
.trendsNews ul li button span,
.trendsFinance ul li button span,
.trendsShopping ul li button span {
text-align: center;
padding: 2rem;
}
.trendsCode ul li button span {
line-break: anywhere;
}
.trendsNews ul li button span {
.trendsNews ul li button span,
.trendsFinance ul li button span {
text-align: left;
}

.trendsVideos ul li button img,
.trendsMusic ul li button img,
.trendsImage ul li button img,
.trendsNews ul li button img,
.trendsFinance ul li button img,
.trendsShopping ul li button img {
width: 100%;
}
Expand Down
26 changes: 25 additions & 1 deletion components/WidgetTrends/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ export default function SearchTrends() {
}
const { dataNews, errorNews } = useApiNews()

// Finance
function useApiFinance() {
const { data, error } = useSWR(
country && category === 'Finance'
? `/api/trends/finance?country=${country}`
: null,
fetcher,
)

return {
dataFinance: data,
errorFinance: error,
}
}
const { dataFinance, errorFinance } = useApiFinance()

// Shopping
function useApiShopping() {
const { data, error } = useSWR(
Expand Down Expand Up @@ -304,6 +320,12 @@ export default function SearchTrends() {
errorNews && setErrorTrends(errorNews)
}
break
case 'Finance':
{
dataFinance && setDataTrends(dataFinance)
errorFinance && setErrorTrends(errorFinance)
}
break
case 'Legal':
{
dataLegal && setDataTrends(dataLegal)
Expand Down Expand Up @@ -346,6 +368,8 @@ export default function SearchTrends() {
errorCode,
dataNews,
errorNews,
dataFinance,
errorFinance,
dataApps,
errorApps,
dataLegal,
Expand All @@ -356,7 +380,7 @@ export default function SearchTrends() {
return <Alert>{t('warnings.torrent')}</Alert>
}

if (category === 'Games' || category === 'Finance') {
if (category === 'Games') {
return null
}

Expand Down
4 changes: 4 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,10 @@
"name": "Finance",
"active": false,
"data": [
{
"name": "Google Finance",
"action": "https://google.com/finance/quote/"
},
{
"name": "finviz",
"action": "https://finviz.com/search.ashx?p="
Expand Down
38 changes: 23 additions & 15 deletions locales/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -1347,38 +1347,46 @@
"data": [
{
"name": "Google Finance",
"action": "https://finance.google.com/search?q="
"action": "https://google.com/finance/quote/"
},
{
"name": "Caixa",
"action": "https://www.caixa.gov.br/site/Paginas/Pesquisa.aspx?k="
"name": "Mobills",
"action": "https://www.mobills.com.br/busca/"
},
{
"name": "Itaú",
"action": "https://www.itau.com.br/resultados-de-busca?q="
"name": "Exame",
"action": "https://exame.com/busca/"
},
{
"name": "Nubank",
"action": "https://blog.nubank.com.br/busca/?search="
"name": "Valor",
"action": "https://valor.globo.com/busca/?q="
},
{
"name": "Bradesco",
"action": "https://blog.nubank.com.br/busca/?search="
"name": "B3",
"action": "https://www.b3.com.br/pt_br/busca/?query="
},
{
"name": "BB",
"name": "Itaú",
"action": "https://www.itau.com.br/resultados-de-busca?q="
},
{
"name": "Caixa",
"action": "https://www.caixa.gov.br/site/Paginas/Pesquisa.aspx?k="
},
{
"name": "Santander",
"action": "https://www.itau.com.br/resultados-de-busca?q="
"name": "BB",
"action": "https://www.bb.com.br/site/busca/?q="
},
{
"name": "Inter",
"action": "https://www.caixa.gov.br/site/Paginas/Pesquisa.aspx?k="
"name": "Nubank",
"action": "https://blog.nubank.com.br/busca/?search="
},
{
"name": "Bradesco",
"action": "https://banco.bradesco/html/classic/resultado-busca/index-nova-busca.shtm?termsearched="
},
{
"name": "BTG Pactual",
"name": "Santander",
"action": "https://www.itau.com.br/resultados-de-busca?q="
}
],
Expand Down
88 changes: 88 additions & 0 deletions pages/api/trends/finance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { NextApiRequest, NextApiResponse } from 'next/types'
import Parser from 'rss-parser'

const parser = new Parser({
customFields: {
item: [
['media:content', 'thumbnail'],
['media:content', 'mediaContent'],
],
},
})

export default async function endpoint(
request: NextApiRequest,
response: NextApiResponse,
) {
const {
query: { country },
} = request

if (!country) {
return response
.status(400)
.json({ message: 'Erro: Parâmetro COUNTRY ausente.' })
}

const getData = async (): Promise<{
credits_title: string
credits_url: string
data: { title: string; image?: string; url: string }[]
}> => {
let url: string
let credits: string[]

switch (country) {
case 'BR':
url = 'https://valor.globo.com/rss/valor'
credits = ['Exame', 'https://exame.com/']
break
default:
url = 'https://techcrunch.com/feed/'
credits = ['TechCrunch', 'https://techcrunch.com/']
break
}

try {
const data = await parser.parseURL(url)
const items = data.items.slice(0, 16).map((item) => ({
title: item?.title || 'No title',
image:
country === 'BR'
? item?.thumbnail?.$?.url
: item?.mediaContent?.$?.url,
url: item?.link || '#',
}))

return {
credits_title: credits[0],
credits_url: credits[1],
data: items,
}
} catch (error) {
console.error('Erro ao buscar dados remotos:', error)
return {
credits_title: 'Erro',
credits_url: '',
data: [
{
title: 'Ops! Click to report error',
image: undefined,
url: 'mailto:feedback@findto.app?subject=Findto%20Feedback&body=Error%20in%20News%20API',
},
],
}
}
}

try {
const data = await getData()
response.status(200).json(data)
} catch (error) {
console.error('Error fetching data:', error)
response.status(500).json({
message: 'Error fetching data',
error: error,
})
}
}

0 comments on commit b60d654

Please sign in to comment.