Skip to content

Instantly share code, notes, and snippets.

@tonyspiro
Created June 10, 2018 20:11
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 tonyspiro/1b041812c301e1d3c9aef76ecf40ec31 to your computer and use it in GitHub Desktop.
Save tonyspiro/1b041812c301e1d3c9aef76ecf40ec31 to your computer and use it in GitHub Desktop.
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