Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Taylor on 3/17/2018.
- */
- var request = require('request'),
- cheerio = require('cheerio');
- var fs = require('fs');
- var nodemailer = require('nodemailer');
- var showDebug = process.argv[2]
- var sendEmail = process.argv[3]
- var dictionary = [
- {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'},
- {store: 'Apmex',jQuery:'.gpvi',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\APMEXstore',url:'http://stores.ebay.com/APMEX?submit=SEARCH&_nkw=special+price'},
- {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'},
- {store: 'Bullion Exchanges',jQuery:'.gpvi',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\BEstore',url:'http://stores.ebay.com/Bullion-Exchanges_W0QQ?_nkw=Sale+Price'}]
- function start(data,done){
- fs.readFile(data.file, 'utf8', function(err, result) {
- if (err) throw err;
- var lines = result.split(/\r?\n/);
- //overwrite file
- fs.writeFile(data.file, '', function(err){
- if(err) debug(err,"writeFile");
- });
- debug(result.split(/\r?\n/),"readFile");
- scrape(data,function(obj){
- var messageBody=[]
- obj.forEach(function(href){
- fs.appendFile(data.file,href +"\n",function(err){
- if (err) debug(err);
- });
- doIexist(result,href,function(bool){
- if(!bool){
- debug(result.split(/\r?\n/),"doIexist")
- //new coin. check for substrings ('Gold')
- if(linkCheck(href)){
- debug("APPENDING MESSAGE BODY","linkCheck")
- messageBody.push(href);
- }
- }
- })
- });
- done(messageBody)
- });
- });
- }
- //console.log(dictionary[0])
- function linkCheck(href){
- return href.indexOf('Gold') !== -1
- }
- function doIexist(file,href,done){
- var found = file.indexOf(href) !=-1
- done(found)
- }
- function scrape(data,callback) {
- request(data.url, function (err, resp, body) {
- var hrefs =[]
- if (!err && resp.statusCode == 200) {
- var $ = cheerio.load(body);
- $(data.jQuery).each(function (index, i) {
- var href = $(this).attr('href');
- hrefs.push(href)
- });
- callback(hrefs)
- }
- });
- }
- var smtpTransport = nodemailer.createTransport({
- service: "hotmail", // sets automatically host, port and connection security settings
- auth: {
- user: "beeslikehoneygold@hotmail.com",
- pass: "HoneyGold123"
- }
- });
- function mail(messageBody,sub,callback) {
- var messageBodyJson = messageBody
- smtpTransport.sendMail({ //email options
- from: "beeslikehoneygold@hotmail.com", // sender address. Must be the same as authenticated user if using Gmail.
- to: "beeslikehoneygold@hotmail.com", // receiver
- subject: sub, // subject
- text: messageBodyJson // body
- }, function(error, response){ //callback
- if(error){
- console.log("error",error);
- }else{
- console.log(response);
- callback("message sent")
- }
- // smtpTransport.close(); // shut down the connection pool, no more messages. Comment this line out to continue sending emails.
- });
- }
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
- function getgoing(){
- var data = dictionary.shift();
- if (!data){
- console.log("Finished list, resetting array")
- dictionary= [
- {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'},
- {store: 'Apmex',jQuery:'.gpvi',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\APMEXstore',url:'http://stores.ebay.com/APMEX?submit=SEARCH&_nkw=special+price'},
- {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'},
- {store: 'Bullion Exchanges',jQuery:'.gpvi',file: 'C:\\Users\\Taylor\\IdeaProjects\\untitled1\\src\\BEstore',url:'http://stores.ebay.com/Bullion-Exchanges_W0QQ?_nkw=Sale+Price'}];
- return;
- }
- console.log("Starting : "+data.store)
- start(data,function(message){
- if(message.length > 0 && sendEmail.toLowerCase()=="true"){
- console.log("Found new coin(s) for "+data.store +'\n Sending email')
- mail(message.join('\r\n'),data.store,function(){
- console.log("EMAIL OUT")
- getgoing();
- })
- }
- else {
- getgoing();
- }
- })
- }
- function debug(message,func){
- if(showDebug.toLowerCase()=="true"){
- console.log("------------------------"+func+'---------------------')
- console.log(message)
- console.log("------------------------"+func+'---------------------')
- }
- }
- setInterval(getgoing,10000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement