Created
December 22, 2016 18:03
-
-
Save otarza/8de9603231514f943f8455ec1e911de0 to your computer and use it in GitHub Desktop.
FetchByUrls
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
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