crawler.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. let link = "https://www.skroutz.gr/c/";
  2. const https = require('https');
  3. const cheerio = require('cheerio')
  4. const db = require('../db');
  5. class Crawler {
  6. constructor(){
  7. }
  8. getPage(code, category) {
  9. return new Promise((res, rej) => {
  10. https.get(link + category + ".html?from=catspan&keyphrase=" + code, (resp) => {
  11. let data = '';
  12. // A chunk of data has been recieved.
  13. resp.on('data', (chunk) => {
  14. data += chunk;
  15. });
  16. // The whole response has been received. Print out the result.
  17. resp.on('end', () => {
  18. res(data);
  19. });
  20. }).on("error", (err) => {
  21. console.log("Error: " + err.message);
  22. rej(err);
  23. });
  24. })
  25. }
  26. findPosition(data, player = "Toner Sales") {
  27. return new Promise((res,rej) => {
  28. const $ = cheerio.load(data);
  29. let items = $('li.cf.card.click-to-shop.js-product-card');
  30. let len = items.length;
  31. console.log(data);
  32. for (var i=0;i < len; i++) {
  33. let txt = $(items.get(i)).find('a.shop.icon.js-shop-info-link').text();
  34. let price = $(items.get(i)).find('a.sku-link.js-sku-link').text();
  35. console.log(txt + '-' + player);
  36. if(txt == player) {
  37. res({
  38. position: i+1,
  39. price: price
  40. });
  41. return;
  42. }
  43. }
  44. rej("[!]Not found");
  45. })
  46. }
  47. newWithData(name, enemy, data) {
  48. return {
  49. data: {
  50. [enemy]: data
  51. },
  52. price: 0.0,
  53. position: 0,
  54. enemies: [enemy]
  55. }
  56. }
  57. update(name, enemy, data) {
  58. return db.get(name).then((item) => {
  59. item.data[enemy] = data;
  60. if(data.position < item.position) {
  61. item.alert = 1;
  62. }
  63. item.lastDate = new Date().toISOString();
  64. return db.put(name, item);
  65. })
  66. .catch((e) => {
  67. console.log("Error");
  68. console.log(e);
  69. db.put(name, this.newWithData(name, enemy, data));
  70. });
  71. }
  72. updateItem(name, data) {
  73. return db.get(name).then((item) => {
  74. item.position = data.position;
  75. item.price = data.price;
  76. item.alert = 0;
  77. item.lastDate = (new Date().toISOString());
  78. return db.put(name, item).then(() => console.log(item));
  79. });
  80. }
  81. collect(name, value) {
  82. console.log("Item: " + name + " - " + value.category)
  83. return this.getPage(name, value.category)
  84. .then((page) => {
  85. let parr = [];
  86. let start = Promise.resolve([]);
  87. start = start.then(() => {
  88. return this.findPosition(page)
  89. })
  90. .then((data) => {
  91. return this.updateItem(name, data)
  92. });
  93. for(var i in value.enemies){
  94. let temp = value.enemies[i];
  95. start = start
  96. .then(() => this.findPosition(page, temp))
  97. .then((newdata) => this.update(name, temp, newdata));
  98. }
  99. return start;
  100. }).catch((e) => console.log("Error" + e));
  101. }
  102. }
  103. module.exports = new Crawler();