node.js检索http csv文件并加载到mongoose中

我对编码一般都很陌生,所以如果这个问题应该比较明显,我会提前道歉。 以下是我期待做的事情,接下来我将发布迄今为止使用的代码。

我试图从一个网站获取gzip'd csv排名数据,并将其存储到一个数据库中,我正在开发一个家族网站。 一旦我弄明白了,我需要每5分钟抓一次数据。 抓住我已经能够完成的CSV数据,虽然它存储到一个文本文件,我需要将其存储到mongodb。

这是我的代码:

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 }) }); 

下面是csv的样子(文件的前5行)的例子:

 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 

希望现在帮助还不算太迟,但是这是我要做的:

  1. 请求CSV格式的数据并将其存储在内存或文件中。
  2. parsingCSV数据以将每行转换为对象。
  3. 对于每个对象,使用Model.create()来创build你的新条目。

首先,您需要从Schema中创build一个模型:

 var Rank = db.model('Rank', rankSchema); 

然后,你可以parsing你的CSV文本块(无论是从文件中读取,还是在你的请求完成后直接执行)。我创build了自己的伪造datavariables,因为我没有访问api,但作为只要你的数据是CSV文本的换行符分隔的块,这应该工作:

 /* Data is just a block of CSV formatted text. This can be read from a file or retrieved right in the response. */ var data = '' + '9,386,1,451,Super Kancheong Style,22586,318793803,LaF,D,1,0,0,1,0\n' + '9,386,2,119,Storm of Swords,25365,293053897,LaF,D,1,0,0,1,0\n' + '9,386,3,33,eug gave it to mak gangnam style,43501,212637806,LaF,H,1,0,0,1,0\n' + '9,386,4,128,Justpickupgirlsdotcom,22628,201606479,LaF,H,1,0,0,1,0\n' + '9,386,5,300,One and Done,22100,196130870,LaF,H,1,0,0,1,0\n'; data = data.split('\n'); data.forEach(function(line) { line = line.split(','); if (line.length != 14) return; /* Create an object representation of our CSV data. */ var new_rank = { serverid: line[0], resetid: line[1], rank: line[2], countryNumber: line[3], name: line[4], land: line[5], networth: line[6], tag: line[7], gov: line[8], gdi: line[9], protection: line[10], vacation: line[11], alive: line[12], deleted: line[13] }; /* Store the new entry in MongoDB. */ Rank.create(new_rank, function(err, rank) { console.log('Created new rank!', rank); }); }); 

你可以把它放在一个脚本中,并使用cron作业每5分钟运行一次。 在我的Mac上,我会用crontab -e编辑我的cron文件,然后用这样一行代码设置一个作业:

 */5 * * * * /path/to/node /path/to/script.js > /dev/null