Advertisement
Guest User

unhandled exception

a guest
Jan 18th, 2013
89
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. // Put a friendly message on the terminal
  10. //console.log("cronJob started");
  11.  
  12. var cronJob = require('cron').CronJob;
  13. var job = new cronJob('*/5 * * * * *', function(){
  14.     // Runs every weekday (Monday through Friday)
  15.     // at 11:30:00 AM. It does not run on Saturday
  16.     // or Sunday.
  17.  
  18.    
  19. var db = new Db('xForum', new Server('localhost', Connection.DEFAULT_PORT, {forceServerObjectId: true}));
  20.  
  21.  
  22.  
  23.  
  24. var request = http.get({
  25.                          host: 'www.earthempires.com',
  26.                          path: '/news_feed?apicode=edited_out_for_privacy',
  27.                          port: 80,
  28.                          headers: { 'accept-encoding': 'gzip' } }).on('error', function(){});
  29.  
  30.  
  31.   request.on('response', function(response) {
  32.     if (response.statusCode == 200){
  33.       switch (response.headers['content-encoding']) {
  34.         case 'gzip':
  35.           var gunzip = zlib.createGunzip();
  36.           var data = "";
  37.           response.pipe(gunzip);
  38.           gunzip.on('data', function(chunk) {
  39.             data += chunk;
  40.           });
  41.           gunzip.on('end', function() {
  42.             db.open(function(err, db) {
  43.               if (err){
  44.                 console.log("error:" + err);
  45.                 process.exit(1);
  46.               }
  47.               db.collection('news', function(err, collection) {
  48.              
  49.                 console.log("collection: news" + k);
  50.                 k++;
  51.  
  52.                 data = data.split('\n');
  53.                 data.forEach(function(line) {
  54.                   line = line.split(',');
  55.                   if (line.length != 15) {
  56.                      return;
  57.                   };
  58.                   var new_rank = {
  59.                       serverid: line[0],
  60.                       resetid: line[1],
  61.                       newsid: line[2],
  62.                       timestamp: line[3]*1000,
  63.                       type: line[4],
  64.                       win: line[5],
  65.                       attacker_num: line[6],
  66.                       attacker_name: line[7],
  67.                       defender_num: line[8],
  68.                       defender_name: line[9],
  69.                       result1: line[10],
  70.                       result2: line[11],
  71.                       a_tag: line[12],
  72.                       d_tag: line[13],
  73.                       killhit: line[14]
  74.                     };
  75.                   collection.insert(new_rank);
  76.                 });
  77.               });
  78.             });
  79.           });
  80.       }
  81.     }      
  82.   });
  83.  
  84.   request.on('error', function(error){
  85.     console.log(error);
  86.     console.log("hmph");
  87.   });
  88. }, null, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement