Skip to content

Instantly share code, notes, and snippets.

@alecslupu
Last active August 29, 2015 14:23
Show Gist options
  • Save alecslupu/61917db63a313b3aefb2 to your computer and use it in GitHub Desktop.
Save alecslupu/61917db63a313b3aefb2 to your computer and use it in GitHub Desktop.
var http = require("http"),
zlib = require("zlib"),
parse = require('csv-parse');
module.exports = {
getGzipped: function(url, callback) {
// buffer to store the streamed decompression
var buffer = [];
http.get(url, function(res) {
// pipe the response into the gunzip to decompress
var gunzip = zlib.createGunzip();
res.pipe(gunzip);
gunzip.on('data', function(data) {
// decompression chunk ready, add it to the buffer
buffer.push(data.toString())
}).on("end", function() {
// response and decompression complete, join the buffer and return
callback(null, buffer.join(""));
}).on("error", function(e) {
callback(e);
})
}).on('error', function(e) {
callback(e);
});
},
fetch: function(url, row_callback) {
this.getGzipped(url, function(err, csv) {
if (typeof(csv) == 'undefined')
return;
parse( csv, { 'columns':true },
function(err, data){
if(err) throw err;
data.forEach(function(row){
row_callback(row);
});
}
);
});
}
};
var MongoClient = require('mongodb').MongoClient,
parser = require('./parser/http-gzip-parser.js'),
async = require('async');
var connectionUrl = 'mongodb://localhost:27017/shop',
productsCollection = 'products';
feeds = [
// some http resuts
]
var urlList = []
feeds.forEach(function(num){
//some id concat
});
MongoClient.connect(connectionUrl, function(err, db) {
if (err) throw err;
var collection = db.collection(productsCollection);
async.waterfall([
function downloadFeeds(callback){
for (var i = 0, len = urlList.length; i < len; i++) {
parser.fetch(urlList[i], function(row){
console.log("row");
collection.findAndModify({ aw_product_id: row.aw_product_id }, {}, { $set: row }, { upsert: true }, function(error,result){});
});
}
}
], function (error) {
if (error) {
console.log("err");
console.error(error);
}
console.log("close");
db.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment