AosFatosWatch.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const request = require('request');
  2. const cheerio = require('cheerio');
  3. const New = require('../models/New.js')();
  4. class AosFatosWatch {
  5. constructor() {
  6. this.url = 'https://aosfatos.org/';
  7. this.websiteSlug = 'aosfatos';
  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 + '/noticias', function (error, response, body) {
  21. if (!error && response.statusCode == 200) {
  22. const $ = cheerio.load(body);
  23. const currentDate = new Date();
  24. const href = this.url + $('#three-cards').find('.card').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('article:not(.social)').html();
  30. const article = {
  31. link: href,
  32. websiteSlug: this.websiteSlug,
  33. // data da postagem, no site...
  34. date: $2('article').find('.publish_date').first().text(),
  35. // imagem da postagem
  36. thumb: this.url + $('#three-cards').find('.card-image').first().css("background-image").replace("url('", '').replace("')", '').replace(/\"/gi, ""),
  37. // titulo da postagem
  38. title: $('#three-cards').find('.card-title > h2').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 = AosFatosWatch;