Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- var zlib = require('zlib');
- var Db = require('mongodb').Db,
- Connection = require('mongodb').Connection,
- Server = require('mongodb').Server,
- BSON = require('mongodb').BSONNative;
- var k = 1;
- // Put a friendly message on the terminal
- //console.log("cronJob started");
- var cronJob = require('cron').CronJob;
- var job = new cronJob('*/5 * * * * *', function(){
- // Runs every weekday (Monday through Friday)
- // at 11:30:00 AM. It does not run on Saturday
- // or Sunday.
- var db = new Db('xForum', new Server('localhost', Connection.DEFAULT_PORT, {forceServerObjectId: true}));
- var request = http.get({
- host: 'www.earthempires.com',
- path: '/news_feed?apicode=edited_out_for_privacy',
- port: 80,
- headers: { 'accept-encoding': 'gzip' } }).on('error', function(){});
- request.on('response', function(response) {
- if (response.statusCode == 200){
- switch (response.headers['content-encoding']) {
- case 'gzip':
- var gunzip = zlib.createGunzip();
- var data = "";
- response.pipe(gunzip);
- gunzip.on('data', function(chunk) {
- data += chunk;
- });
- gunzip.on('end', function() {
- db.open(function(err, db) {
- if (err){
- console.log("error:" + err);
- process.exit(1);
- }
- db.collection('news', function(err, collection) {
- console.log("collection: news" + k);
- k++;
- data = data.split('\n');
- data.forEach(function(line) {
- line = line.split(',');
- if (line.length != 15) {
- return;
- };
- var new_rank = {
- serverid: line[0],
- resetid: line[1],
- newsid: line[2],
- timestamp: line[3]*1000,
- type: line[4],
- win: line[5],
- attacker_num: line[6],
- attacker_name: line[7],
- defender_num: line[8],
- defender_name: line[9],
- result1: line[10],
- result2: line[11],
- a_tag: line[12],
- d_tag: line[13],
- killhit: line[14]
- };
- collection.insert(new_rank);
- });
- });
- });
- });
- }
- }
- });
- request.on('error', function(error){
- console.log(error);
- console.log("hmph");
- });
- }, null, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement