Last active
March 16, 2020 04:10
-
-
Save emersonbroga/147a0f9cddb4c1a417199cd282cbbd7d to your computer and use it in GitHub Desktop.
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 Parser = require('rss-parser'); | |
const parseVolumeToInt = volume => { | |
const vol = volume.replace('+', '').replace(',', ''); | |
return Number.parseInt(vol, 10); | |
}; | |
async function getGoogleTrends(url) { | |
const parser = new Parser({ | |
defaultRSS: 2.0, | |
customFields: { | |
item: [['ht:approx_traffic', 'volume', { keepArray: false }]], | |
}, | |
}); | |
const trends = await parser.parseURL(url); | |
const items = trends.items.map(item => { | |
return { name: item.title, volume: parseVolumeToInt(item.volume) }; | |
}); | |
const sorted = items.sort((a, b) => b.volume - a.volume); | |
return sorted; | |
} | |
const fetchData = async (limit = 10) => { | |
const [us, br] = await Promise.all([ | |
getGoogleTrends('https://trends.google.com/trends/trendingsearches/daily/rss?geo=US'), | |
getGoogleTrends('https://trends.google.com/trends/trendingsearches/daily/rss?geo=BR'), | |
]); | |
const items = [...us, ...br]; | |
const sorted = items.sort((a, b) => b.volume - a.volume); | |
const limited = sorted.slice(0, limit); | |
return limited; | |
}; | |
export default { fetchData }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment