Skip to content

Instantly share code, notes, and snippets.

@spencercarli
Created January 8, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spencercarli/d0990b770e940d2eb46d2a0764728460 to your computer and use it in GitHub Desktop.
Save spencercarli/d0990b770e940d2eb46d2a0764728460 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const fetch = require('node-fetch');
const data = require('./data');
const target = 'name="description" content="';
const target2 = 'rel="canonical" href="';
const fetchData = (post) => {
return fetch(post.url)
.then(res => res.text())
.then(res => ({
indexOfDescription: res.indexOf(target),
indexOfCanonical: res.indexOf(target2),
data: res,
}))
.then(({ data, indexOfDescription, indexOfCanonical }) => {
const str = data.slice(indexOfDescription);
const end = str.indexOf(">");
const str2 = data.slice(indexOfCanonical);
const end2 = str2.indexOf(">")
return {
...post,
description: str.slice(0 + target.length, end -1),
url: str2.slice(0 + target2.length, end2 -1),
};
})
.catch(err => console.log(`ERROR: ${post.url}`, err))
}
// fetchData(data.posts[0]).then(res => console.log(res))
const augmentData = async (data) => {
const promises = data.posts.map(post => fetchData(post));
const results = await Promise.all(promises);
fs.writeFile('augmented-data.json', JSON.stringify(results, null, 2), () => {
console.log('complete!')
})
}
augmentData(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment