willyb321

santu eddn

Jan 19th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const zlib = require("zlib");
  2. const zmq = require("zeromq");
  3. const sock = zmq.socket("sub");
  4. const MongoClient = require('mongodb').MongoClient;
  5.  
  6. // Connection URL
  7. const url = 'mongodb://localhost:27017';
  8.  
  9. // Database Name
  10. const dbName = 'santu';
  11.  
  12. sock.connect("tcp://eddn.edcd.io:9500");
  13. console.log("Worker connected to port 9500");
  14.  
  15. sock.subscribe("");
  16.  
  17. sock.on("message", topic => {
  18.         const parsed = JSON.parse(zlib.inflateSync(topic));
  19.         if (parsed.message.StarSystem === 'San Tu' || parsed.message.systemName === 'San Tu') {
  20.                 console.log(parsed.message.StarSystem || parsed.message.systemName);
  21.                 for (const i in parsed) {
  22.                         for (const x in parsed[i]) {
  23.                         if (i.startsWith('$')) {
  24.                                 parsed[i.replace('$', '')] === parsed[i];
  25.                                 delete parsed[i];
  26.  
  27.                         }
  28.                         if (x.startsWith('$')) {
  29. parsed[x.replace('$', '')] === parsed[i][x];
  30.                                 delete parsed[i][x];
  31.  
  32.                         }
  33.         }
  34.                 }
  35.                 MongoClient.connect(url, function(err, client) {
  36.                         console.log("Connected successfully to server");
  37.  
  38.                         const db = client.db(dbName);
  39.                         db.collection('eddn')
  40.                         .insertOne(parsed, (err, res) => {
  41.                                 if (err) {
  42.                                         console.error(err);
  43.                                         process.exit(1);
  44.                                 } else {
  45.                                         console.log('new san tu entry');
  46.                                 }
  47.                         })
  48.                         client.close();
  49.                   });
  50.         }
  51. });
Advertisement
Add Comment
Please, Sign In to add comment