Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require('dotenv').config()
const async = require('async')
const axios = require('axios')
const algoliasearch = require('algoliasearch')
const client = algoliasearch(process.env.ALGOLIA_ACCOUNT, process.env.ALGOLIA_INDEX)
const index = client.initIndex('Stories')
const getClaps = () => {
let hit_count = 0
index.browse('', {}, function browseDone(err, content) {
if (err) {
throw err
}
const hits = content.hits
async.eachSeries(hits, (hit, callback) => {
const medium_url = hit.metadata.medium_link
if (!medium_url)
return callback()
axios.get(medium_url).then(response => {
const str1 = '"totalClapCount":'
const str2 = ',"sectionCount'
const claps = Number(response.data.split(str1).pop().split(str2).shift())
index.partialUpdateObject({
claps: claps,
objectID: hit.objectID
}, function(err, content) {
if (err) throw err;
// console.log(medium_url, claps, hit.objectID)
callback()
});
}).catch(err => {
console.log(err)
callback()
})
}, () => {
hit_count = hit_count + content.hits.length
if (content.cursor) {
index.browseFrom(content.cursor, browseDone)
} else {
getClaps()
console.log('DONE!', hit_count)
}
})
})
}
getClaps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.