PensarContemporaneoWatch.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const request = require('request');
  2. const cheerio = require('cheerio');
  3. const New = require('../models/New.js')();
  4. class PensarContemporaneoWatch {
  5. constructor ( ) {
  6. this.url = 'https://www.pensarcontemporaneo.com/';
  7. this.websiteSlug = 'pensarcontemporaneo';
  8. this.tag = 'diversos';
  9. }
  10. get Url () {
  11. return this.url;
  12. }
  13. get Tag () {
  14. return this.tag;
  15. }
  16. get WebsiteSlug () {
  17. return this.websiteSlug;
  18. }
  19. registerLatestNovelty () {
  20. request(this.url, function (error, response, body) {
  21. if (!error && response.statusCode == 200) {
  22. const $ = cheerio.load(body);
  23. const currentDate = new Date ();
  24. const href = $('.td_block_inner').find('.td-module-thumb > a').first().attr('href');
  25. request(href, function (error2, response2, body2) {
  26. if (!error2 && response2.statusCode == 200) {
  27. const $2 = cheerio.load(body2);
  28. // conteúdo da postagem
  29. var content = $2('.td-post-content').html();
  30. const article = {
  31. link: href,
  32. websiteSlug: this.websiteSlug,
  33. // data da postagem, no site...
  34. date: $('.td_block_inner').find('.entry-date').first().text(),
  35. // imagem da postagem
  36. thumb: $('.td_block_inner').find('img').first().attr('src'),
  37. // titulo da postagem
  38. title: $('.td_block_inner').find('.entry-title > a').first().text(),
  39. content: content,
  40. // tag da categoria
  41. tag: this.tag,
  42. }
  43. New.query
  44. .findAndCountAll({ where: { link: article.link } })
  45. .then(result => {
  46. if (result.count < 1) {
  47. New.query.create(article);
  48. console.log('[ADDED] ' + article.title + ' +')
  49. } else {
  50. console.log('[CHECKED] ' + article.websiteSlug + ' ✓')
  51. }
  52. });
  53. } else {
  54. console.log('[ERROR] HOUVE UM ERRO NO LINK DA POSTAGEM x ')
  55. }
  56. }.bind(this));
  57. } else {
  58. console.log('[ERROR] HOUVE ALGUM ERRO x ')
  59. }
  60. }.bind(this));
  61. }
  62. }
  63. module.exports = PensarContemporaneoWatch;