Advertisement
Guest User

Untitled

a guest
Mar 7th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. const cheerio = require('cheerio')
  2. const colors = require('colors')
  3. var api = {};
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. api.seekForItem = function(configuration, link, productKeywords, callback) {
  12. if (configuration.proxy.active) {
  13. if (configuration.proxy.username == null || configuration.proxy.username == "" && configuration.proxy.password == null || configuration.proxy.password == "") {
  14. // no auth
  15. var proxyUrl = configuration.proxy.host
  16. } else {
  17. // auth
  18. var proxyUrl = "http://" + user + ":" + password + "@" + host + ":" + port;
  19. }
  20.  
  21. var request = require('request').defaults({
  22. timeout: 30000,
  23. proxy: proxyUrl
  24. });
  25. } else {
  26. var request = require('request').defaults({
  27. timeout: 30000
  28. });
  29. }
  30.  
  31. request(link, function(err, resp, html, rrr, body) {
  32.  
  33. if (err) {
  34. return callback(null, 'No response from website, failed to load data.');
  35. } else {
  36. var $ = cheerio.load(html);
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. var response = {
  50. productCount: $('.product').length,
  51. productDetails: []
  52. }
  53.  
  54.  
  55. var matches = [];
  56.  
  57. $('.grid-view-item').each(function(i, element) {
  58. // var nextElement = $(this).next();
  59. // var prevElement = $(this).prev();
  60. // var details = $(this).has('.details a');
  61.  
  62. // if ($('.sold').eq(i).text() == '') {
  63. // var status = 'Available';
  64. // } else {
  65. // var status = 'Sold Out';
  66. // }
  67.  
  68. var product = {
  69. name: $('.grid-view-item__title').eq(i).text(),
  70. // status: status,
  71. link: 'http://gallery707.myshopify.com' + $('.grid-view-item__title').eq(i).parent().attr('href')
  72. }
  73. response.productDetails.push(product);
  74. console.log('.grid-view-item__title');
  75. });
  76.  
  77. for (i = 0; i < response.productDetails.length; i++) {
  78. var title = response.productDetails[i].name;
  79. if (title.indexOf(productKeywords) > -1) {
  80. matches.push(response.productDetails[i]);
  81. return callback(response.productDetails[i], null);
  82.  
  83. break;
  84.  
  85. } else {
  86. continue;
  87. }
  88. }
  89.  
  90. if (matches[0] === undefined) {
  91. return callback(null, "Could not find any results matching your keywords.");
  92. }
  93.  
  94. });
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. // warning, error, info, success
  120. api.log = function(task, type, text) {
  121.  
  122. var date = new Date()
  123. var formatted = date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1")
  124.  
  125.  
  126. switch (type) {
  127. case "warning":
  128. console.log(`[Task #${task}][${formatted}] ${text}`.yellow)
  129. break;
  130. case "error":
  131. console.log(`[Task #${task}][${formatted}] ${text}`.red)
  132. break;
  133. case "info":
  134. console.log(`[Task #${task}][${formatted}] ${text}`.cyan)
  135. break;
  136. case "success":
  137. console.log(`[Task #${task}][${formatted}] ${text}`.green)
  138. break;
  139.  
  140. default:
  141. console.log(`[Task #${task}][${formatted}] ${text}`.white)
  142. }
  143. }
  144.  
  145. module.exports = api;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement