Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by Taylor on 3/17/2018.
  3.  */
  4.  
  5. var request = require('request'),
  6.     cheerio = require('cheerio');
  7. var fs = require('fs');
  8. var nodemailer = require('nodemailer');
  9.  
  10. var showDebug = process.argv[2]
  11. var sendEmail = process.argv[3]
  12.  
  13. var dictionary = [
  14.     {store: 'JM',jQuery:'.tpitem_g .title a',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\JMstore',url:'http://stores.ebay.com/JM-Bullion/_i.html?_nkw=ON+SALE'},
  15.     {store: 'Apmex',jQuery:'.gpvi',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\APMEXstore',url:'http://stores.ebay.com/APMEX?submit=SEARCH&_nkw=special+price'},
  16.     {store: 'MCM',jQuery:'.tpitem_g .title a',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\MCMstore',url:'http://stores.ebay.com/ModernCoinMart/Daily-Deals-/_i.html?_fsub=24095177011&_sid=160224921&_trksid=p4634.m322'},
  17.     {store: 'Bullion Exchanges',jQuery:'.gpvi',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\BEstore',url:'http://stores.ebay.com/Bullion-Exchanges_W0QQ?_nkw=Sale+Price'}]
  18.  
  19. function start(data,done){
  20.  
  21.     fs.readFile(data.file, 'utf8', function(err, result) {
  22.         if (err) throw err;
  23.  
  24.         var lines = result.split(/\r?\n/);
  25.  
  26.         //overwrite file
  27.         fs.writeFile(data.file, '', function(err){
  28.             if(err) debug(err,"writeFile");
  29.         });
  30.  
  31.         debug(result.split(/\r?\n/),"readFile");
  32.  
  33.         scrape(data,function(obj){
  34.             var messageBody=[]
  35.             obj.forEach(function(href){
  36.  
  37.                 fs.appendFile(data.file,href +"\n",function(err){
  38.                     if (err) debug(err);
  39.  
  40.  
  41.                 });
  42.                 doIexist(result,href,function(bool){
  43.                     if(!bool){
  44.  
  45.                         debug(result.split(/\r?\n/),"doIexist")
  46.                         //new coin. check for substrings ('Gold')
  47.                         if(linkCheck(href)){
  48.                             debug("APPENDING MESSAGE BODY","linkCheck")
  49.                             messageBody.push(href);
  50.                         }
  51.  
  52.                     }
  53.                 })
  54.  
  55.  
  56.             });
  57.             done(messageBody)
  58.  
  59.         });
  60.  
  61.     });
  62.  
  63. }
  64. //console.log(dictionary[0])
  65. function linkCheck(href){
  66.     return href.indexOf('Gold') !== -1
  67. }
  68. function doIexist(file,href,done){
  69.  
  70.     var found = file.indexOf(href) !=-1
  71.     done(found)
  72. }
  73.  
  74. function scrape(data,callback) {
  75.  
  76.     request(data.url, function (err, resp, body) {
  77.         var hrefs =[]
  78.         if (!err && resp.statusCode == 200) {
  79.             var $ = cheerio.load(body);
  80.         $(data.jQuery).each(function (index, i) {
  81.  
  82.                 var href = $(this).attr('href');
  83.                 hrefs.push(href)
  84.             });
  85.             callback(hrefs)
  86.         }
  87.     });
  88. }
  89.  
  90. var smtpTransport = nodemailer.createTransport({
  91.     service: "hotmail",  // sets automatically host, port and connection security settings
  92.     auth: {
  93.         user: "beeslikehoneygold@hotmail.com",
  94.         pass: "HoneyGold123"
  95.     }
  96. });
  97.  
  98. function mail(messageBody,sub,callback) {
  99.     var messageBodyJson = messageBody
  100.     smtpTransport.sendMail({  //email options
  101.         from: "beeslikehoneygold@hotmail.com", // sender address.  Must be the same as authenticated user if using Gmail.
  102.         to: "beeslikehoneygold@hotmail.com", // receiver
  103.         subject: sub, // subject
  104.         text: messageBodyJson // body
  105.     }, function(error, response){  //callback
  106.         if(error){
  107.  
  108.             console.log("error",error);
  109.         }else{
  110.             console.log(response);
  111.             callback("message sent")
  112.         }
  113.  
  114.         //    smtpTransport.close(); // shut down the connection pool, no more messages.  Comment this line out to continue sending emails.
  115.     });
  116. }
  117. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  118.  
  119. function getgoing(){
  120.     var data = dictionary.shift();
  121.  
  122.     if (!data){
  123.         console.log("Finished list, resetting array")
  124.         dictionary= [
  125.             {store: 'JM',jQuery:'.tpitem_g .title a',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\JMstore',url:'http://stores.ebay.com/JM-Bullion/_i.html?_nkw=ON+SALE'},
  126.             {store: 'Apmex',jQuery:'.gpvi',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\APMEXstore',url:'http://stores.ebay.com/APMEX?submit=SEARCH&_nkw=special+price'},
  127.             {store: 'MCM',jQuery:'.tpitem_g .title a',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\MCMstore',url:'http://stores.ebay.com/ModernCoinMart/Daily-Deals-/_i.html?_fsub=24095177011&_sid=160224921&_trksid=p4634.m322'},
  128.             {store: 'Bullion Exchanges',jQuery:'.gpvi',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\BEstore',url:'http://stores.ebay.com/Bullion-Exchanges_W0QQ?_nkw=Sale+Price'}];
  129.         return;
  130.     }
  131.     console.log("Starting : "+data.store)
  132.  
  133.     start(data,function(message){
  134.  
  135.         if(message.length > 0 && sendEmail.toLowerCase()=="true"){
  136.             console.log("Found new coin(s) for "+data.store +'\n Sending email')
  137.  
  138.             mail(message.join('\r\n'),data.store,function(){
  139.                 console.log("EMAIL OUT")
  140.                 getgoing();
  141.             })
  142.  
  143.         }
  144.         else {
  145.                 getgoing();
  146.         }
  147.  
  148.  
  149.     })
  150. }
  151. function debug(message,func){
  152.     if(showDebug.toLowerCase()=="true"){
  153.         console.log("------------------------"+func+'---------------------')
  154.         console.log(message)
  155.         console.log("------------------------"+func+'---------------------')
  156.     }
  157.  
  158. }
  159. setInterval(getgoing,10000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement