Advertisement
Guest User

unhandled exception

a guest
Jan 18th, 2013
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http');
  2. var zlib = require('zlib');
  3. var Db = require('mongodb').Db,
  4.     Connection = require('mongodb').Connection,
  5.     Server = require('mongodb').Server,
  6.     BSON = require('mongodb').BSONNative;
  7.  
  8. var k = 1;
  9. var cronJob = require('cron').CronJob;
  10. var job = new cronJob('*/5 * * * * *', function(){
  11.      
  12.   var db = new Db('xForum', new Server('localhost', Connection.DEFAULT_PORT, {forceServerObjectId: true}));
  13.   var request = http.get({
  14.                          host: 'www.earthempires.com',
  15.                          path: '/news_feed?apicode=edited_out_for_privacy',
  16.                          port: 80,
  17.                          headers: { 'accept-encoding': 'gzip' } });
  18.  
  19.  
  20.   request.on('response', function(response) {
  21.     if (response.statusCode == 200){
  22.       switch (response.headers['content-encoding']) {
  23.         case 'gzip':
  24.           var gunzip = zlib.createGunzip();
  25.           var data = "";
  26.           response.pipe(gunzip);
  27.           gunzip.on('data', function(chunk) {
  28.             data += chunk;
  29.           });
  30.           gunzip.on('end', function() {
  31.             db.open(function(err, db) {
  32.               if (err){
  33.                 console.log("error:" + err);
  34.               }
  35.               db.collection('news', function(err, collection) {
  36.              
  37.                 console.log("collection: news" + k);
  38.                 k++;
  39.  
  40.                 data = data.split('\n');
  41.                 data.forEach(function(line) {                                                                                                                                                
  42.                   line = line.split(',');  
  43.  
  44.                   if (line.length != 15) {
  45.                      return;
  46.                   };
  47.                                                                                                            
  48.                   var new_rank = {                                                                                                                                                        
  49.                       serverid: line[0],                                                                                                                                                  
  50.                       resetid: line[1],                                                                                                                                                    
  51.                       newsid: line[2],                                                                                                                                              
  52.                       timestamp: line[3]*1000,                                                                                                                                                      
  53.                       type: line[4],                                                                                                                                                      
  54.                       win: line[5],                                                                                                                                                  
  55.                       attacker_num: line[6],                                                                                                                                                        
  56.                       attacker_name: line[7],                                                                                                                                                        
  57.                       defender_num: line[8],                                                                                                                                                        
  58.                       defender_name: line[9],                                                                                                                                                
  59.                       result1: line[10],                                                                                                                                                  
  60.                       result2: line[11],                                                                                                                                                    
  61.                       a_tag: line[12],
  62.                       d_tag: line[13],
  63.                       killhit: line[14]                                                                                                                                                  
  64.                   };
  65.                   collection.insert(new_rank);
  66.                 });
  67.               });        
  68.             });
  69.           });
  70.  
  71.       }
  72.     }      
  73.   });
  74.  
  75.   request.on('error', function(error){
  76.     console.log(error);
  77.     console.log("hmph");
  78.   });
  79. }, null, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement