Created
June 10, 2018 20:11
-
-
Save tonyspiro/1b041812c301e1d3c9aef76ecf40ec31 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
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