I'm very new to coding in general, so I apologize ahead of time if this question should be rather obvious. Here's what I'm looking to do, and following that I'll post the code I've used so far.
I'm trying to get gzip'd csv rank data from a website and store it into a database, for a clan website that I'm working on developing. Once I get this figured out, I'll need to grab the data once every 5 minutes. The grabbing the csv data I've been able to accomplish, although it stores it into a text file and I need to store it into mongodb.
Here's my code:
var DB = require('../modules/db-settings.js');
var http = require('http');
var zlib = require('zlib');
var fs = require('fs');
var mongoose = require('mongoose');
var db = mongoose.createConnection(DB.host, DB.database, DB.port, {user: DB.user, pass: DB.password});
var request = http.get({ host: 'www.earthempires.com',
path: '/ranks_feed?apicode=myapicode',
port: 80,
headers: { 'accept-encoding': 'gzip' } });
request.on('response', function(response) {
var output = fs.createWriteStream('./output');
switch (response.headers['content-encoding']) {
// or, just use zlib.createUnzip() to handle both cases
case 'gzip':
response.pipe(zlib.createGunzip()).pipe(output);
break;
default:
response.pipe(output);
break;
}
});
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
var rankSchema = new mongoose.Schema({
serverid: Number,
resetid: Number,
rank: Number,
countryNumber: Number,
name: String,
land: Number,
networth: Number,
tag: String,
gov: String,
gdi: Boolean,
protection: Boolean,
vacation: Boolean,
alive: Boolean,
deleted: Boolean
})
});
Here's an example of what the csv will look like(first 5 lines of file):
9,386,1,451,Super Kancheong Style,22586,318793803,LaF,D,1,0,0,1,0
9,386,2,119,Storm of Swords,25365,293053897,LaF,D,1,0,0,1,0
9,386,3,33,eug gave it to mak gangnam style,43501,212637806,LaF,H,1,0,0,1,0
9,386,4,128,Justpickupgirlsdotcom,22628,201606479,LaF,H,1,0,0,1,0
9,386,5,300,One and Done,22100,196130870,LaF,H,1,0,0,1,0
rankSchemaand then insert them into your collection using the Mongoose APIs. You're going to have to take the time to learn about how to do those steps as there aren't canned solutions.