Skip to content

Commit

Permalink
Implement a more efficient way to fetch the feeds. Fix sources that d…
Browse files Browse the repository at this point in the history
…o not provide a title
  • Loading branch information
dethos committed Feb 9, 2024
1 parent 06c66ce commit b6cb960
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
40 changes: 25 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ addEventListener('fetch', event => {
/**
* Deliver aggregated content according to the formats requested
* @param {Request} request
* @returns Response
*/
async function handleRequest(request) {
const cacheUrl = new URL(request.url)
Expand Down Expand Up @@ -69,23 +70,30 @@ async function handleRequest(request) {

/**
* Fetch all source feeds and generate the aggregated content
*
* TODO implement a faster way to fetch several sources
*/
async function handleScheduled() {
let feeds = FEEDS.split(',')
let content = []
let sources = []
let items

let promises = []
for (let url of feeds) {
try {
items = await fetchAndHydrate(url)
sources.push({ name: items[0].source_title, link: items[0].source_link })
} catch (error) {
console.log(`Failed to fetch ${url}`)
console.log(error)
promises.push(fetchAndHydrate(url))
}
const results = await Promise.allSettled(promises)

for (let [index, result] of results.entries()) {
if (result.status == 'fulfilled') {
let posts = result.value
let title = posts[0].source_title
let link = posts[0].source_link
let name = title != '' ? title : new URL(link).host
sources.push({ name, link })
content.push(...posts)
} else {
console.log(`Failed to fetch ${feeds[index]}`)
console.log(result.reason)
}
content.push(...items)
}

//sort all the elements chronologically (recent first)
Expand Down Expand Up @@ -116,7 +124,8 @@ async function handleScheduled() {

/**
* Take a feed URL, fetch all items and attach source information
* @param {Array} feeds
* @param {String} feed The URL of the feed to be fetched and parsed
* @returns Array containing all the feed items parsed by rss-parser
*/
async function fetchAndHydrate(feed) {
console.log(`[fetchAndHydrate] start to fetch feed: ${feed}`)
Expand All @@ -141,7 +150,8 @@ async function fetchAndHydrate(feed) {

/**
* Builds a feed object from the provided items
* @param {Array} items
* @param {Array} items parsed by rss-parser
* @return Feed object created by feed
*/
function createFeed(items) {
console.log(`[createFeed] start building the aggregated feed`)
Expand Down Expand Up @@ -175,8 +185,8 @@ function createFeed(items) {
}
/**
* Generate the HTML page with the aggregated contents
* @param {*} items
* @returns
* @param {Array} items parsed by rss-parser
* @returns String with HTML page containing the parsed contents
*/
function createHTML(items, sources) {
console.log(`[createHTML] building the HTML document`)
Expand All @@ -185,7 +195,7 @@ function createHTML(items, sources) {

for (let item of items) {
let shortdescription = striptags(item.content).substring(0, 250)
item.description = shortdescription ? shortdescription + ' [...]' : ""
item.description = shortdescription ? shortdescription + ' [...]' : ''
item.formattedDate = item.pubDate
? dateFormatter.format(new Date(item.pubDate))
: ''
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"format": "prettier --write '**/*.{js,css,json,md}' '!**/worker/*' '!**/templates/*'",
"template": "handlebars -c handlebars/runtime",
"build": "webpack",
"dev": "wrangler dev",
"dev": "webpack && wrangler dev",
"deploy": "wrangler deploy"
},
"author": "Gonçalo Valério <gon@ovalerio.net>",
Expand Down

0 comments on commit b6cb960

Please sign in to comment.