Skip to content

Instantly share code, notes, and snippets.

@otarza
Created December 22, 2016 18:03
Show Gist options
  • Save otarza/8de9603231514f943f8455ec1e911de0 to your computer and use it in GitHub Desktop.
Save otarza/8de9603231514f943f8455ec1e911de0 to your computer and use it in GitHub Desktop.
FetchByUrls
const fetchByURLs_$ = (urls, parserTemplate) => {
const url = 'mongodb://database:27017/veshapi';
const mongo = require('mongoskin');
const db = mongo.db(url, {native_parser: true});
db.bind('uris');
let promises = [];
let index = 1;
urls.map(url => {
let promise = new Promise(function (resolve, reject) {
index++;
setTimeout(function () {
scrapeIt(url, parserTemplate, (err, result) => {
if (err) {
reject(Error(err));
} else {
resolve(result);
}
});
}, 10000 * index);
});
promises.push(promise);
});
promises.map(promise => {
promise.then(result => {
let uris = result.urls.map(uri => {
return {uri: uri.url}
});
db.uris.insertMany(
uris
);
}).catch(e => {
console.log(Error(e));
});
});
Promise.all(promises).then(values => {
console.log("Finished!");
db.close();
}).catch(reason => {
console.log(reason);
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment