Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const request = require("request-promise");
  4. // const fs = require('fs');
  5.  
  6. const cheerio = require("cheerio");
  7. const randomUA = require('random-ua');
  8. const config = require("./config.json");
  9. const nodemailer = require("nodemailer");
  10. const prompt = require('password-prompt');
  11. const sleep = require('sleep');
  12.  
  13.  
  14. const njuskalo = {
  15. link: config.njuskalo,
  16. uri: "www.njuskalo.hr"
  17. };
  18. const oglasnik = {
  19. link: config.oglasnik,
  20. uri: "www.oglasnik.hr"
  21. };
  22. const index = {
  23. link: config.index,
  24. uri: "www.index.hr"
  25. };
  26.  
  27. const userAgent = randomUA.generate();
  28.  
  29. async function getListings(site) {
  30. let list = [];
  31.  
  32. if(site.link) {
  33. const options = {
  34. url: site.link,
  35. headers: {
  36. "Accept-Encoding" : "identity",
  37. "User-Agent" : userAgent
  38. },
  39. };
  40. try {
  41. await request.get(options, function(error, response, html){
  42. if(!error){
  43. let $ = cheerio.load(html);
  44.  
  45. // fs.writeFile("./response.html", html)
  46.  
  47. if (site.uri == "www.njuskalo.hr")
  48. $('li.EntityList-item--Regular .entity-title a').each(function(){
  49. list.push(this.attribs.href);
  50. });
  51.  
  52. if (site.uri == "www.oglasnik.hr")
  53. $('#ads-list a').each(function(){
  54. list.push(this.attribs.href);
  55. });
  56.  
  57. if (site.uri == "www.index.hr")
  58. $('div.results a.result').each(function(){
  59. list.push(this.attribs.href)
  60. });
  61.  
  62. } else {
  63. console.log(error);
  64. };
  65. });
  66. }
  67. catch(error) {
  68. console.log(error)
  69. }
  70. };
  71.  
  72. if (list.length == 0)
  73. console.log("Error fetching", site.uri);
  74.  
  75. return list;
  76. };
  77.  
  78. function mailNotification(content) {
  79. console.log("Notifying...");
  80. return;
  81. }
  82.  
  83. async function main() {
  84. const password = await prompt('Enter password for ' + config.from + ": ");
  85. const transporter = nodemailer.createTransport({
  86. service: 'gmail',
  87. auth: {
  88. user: config.from,
  89. pass: password
  90. }
  91. });
  92.  
  93. console.log("Initializing...");
  94. let listingsOld = [].concat(await getListings(njuskalo), await getListings(index), await getListings(oglasnik));
  95. console.log("Listings initialized!");
  96.  
  97. let html = '<p>Program successfully started.</p><p>Initialized listings: \n';
  98.  
  99. for( let i = 0; i < listingsOld.length; i++)
  100. html = html + "\n" + listingsOld[i];
  101. html = html + "</p>"
  102.  
  103. console.log(html)
  104.  
  105. let mailOptions = {
  106. from: config.from, // sender address
  107. to: config.to, // list of receivers
  108. subject: config.subject, // Subject line
  109. html: html// plain text body
  110. };
  111.  
  112. transporter.sendMail(mailOptions, function (err, info) {
  113. if(err){
  114. console.log(err);
  115. process.exit();
  116. }
  117. });
  118.  
  119. let counter = 0;
  120. while(true) {
  121. console.log("\nRefreshing @", new Date().toLocaleTimeString());
  122. const listingsNew = [].concat(await getListings(njuskalo), await getListings(index), await getListings(oglasnik));
  123. const diff = listingsNew.filter( x => !listingsOld.includes(x));
  124.  
  125. if(diff.length > 0) {
  126. console.log("New listings: ", diff);
  127.  
  128. html = "";
  129. counter++;
  130. for( let i = 0; i < diff.length; i++)
  131. html = html + "<p>" + diff[i] + "</p>";
  132.  
  133. mail.options.subject = config.subject + " " + counter;
  134. mailOptions.html = html;
  135.  
  136. transporter.sendMail(mailOptions, function (err, info) {
  137. if(err)
  138. console.log(err);
  139. });
  140.  
  141. listingsOld = listingsNew;
  142. }
  143. else {
  144. console.log("No new listings.");
  145. }
  146. sleep.sleep(60*5);
  147. };
  148. };
  149.  
  150. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement