Advertisement
orksnork

Untitled

Jun 12th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // LT Scrape File
  2. // This is to scrape from LT and dump it into the Meteor DB only.
  3.  
  4. //Requjred to Connect to mysql server
  5. var mysql = require('mysql');
  6.  
  7. //Underscore for Parsing
  8. var _ = require('underscore');
  9.  
  10. //Access local mongodb
  11. var MeteorDB = require('meteordb');
  12.  
  13.  
  14. //Main Obj
  15. var LTScrape = {};
  16.  
  17. //Create mysql connection
  18. LTScrape.Connect = mysql.createConnection({
  19.     host: '172.16.5.6',
  20.     user: 'mkozak',
  21.     password: 'Mk1290!@',
  22.     database: 'labtech'
  23. });
  24.  
  25. //This, when initialized, will run a single scrape and store each item in the DB.  
  26. LTScrape.Initialize = function(callback) {
  27.     //Reach out to the MeteorDB and determine what tickets we should scrape from LT based off of last highest ticket stored.
  28.     MeteorDB.getHighestTicket(function(data) {
  29.         //Create our SQL query to scrape data from LT
  30.         var sql = 'SELECT * FROM tickets WHERE TicketID > ' + data.TicketID + ' AND Status < 4 LIMIT 100';
  31.         LTScrape.Connect.query(sql, function(error, results, fields) {
  32.             //If there's no new tickets, return.
  33.             if (results.length === 0) {
  34.                 callback("there is no new tickets");
  35.                 return;
  36.             }
  37.             //Process Tickets
  38.             var highestTicket = "";
  39.             _.each(results, function(i) {
  40.                
  41.                 //Update Highest Ticket Result
  42.                 if (i.TicketID > highestTicket) {
  43.                     highestTicket = i.TicketID;
  44.                     MeteorDB.newHighestTicket(highestTicket);
  45.                 }
  46.                
  47.                 //Create Ticket Data for Storage
  48.                 var link = '<a href=\"labtech:open?computerid=' + i.ComputerID + '\">Link to Computer in Labtech<\/a>';
  49.                 var postData = {
  50.                     "helpdesk_ticket": {
  51.                         "description_html": link,
  52.                         "subject": "LT#" + i.TicketID + ": " + i.Subject,
  53.                         "email": "blackhole@meteormanaged.com",
  54.                         "priority": 1,
  55.                         "status": 2,
  56.                         "source": 2,
  57.                         "ticket_type": "Incident",
  58.                         "group_id": "1000190464"
  59.                     }
  60.                 };
  61.                 //Store the Ticket
  62.                 MeteorDB.storeTicket(postData.helpdesk_ticket, function(response) {
  63.                     callback(response);
  64.                 });
  65.             });
  66.         });
  67.     });
  68. };
  69. module.exports = LTScrape;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement