From f8c63e8a70af97bafc14d1dba3349358e44ecb3e Mon Sep 17 00:00:00 2001 From: Eudald Dachs Gonzalez Date: Tue, 2 Feb 2021 20:12:00 +0100 Subject: [PATCH] add crons --- bot.js | 57 +++++++++++++++++++++++++++---------------------- config/index.js | 8 +++++++ package.json | 1 + 3 files changed, 41 insertions(+), 25 deletions(-) create mode 100644 config/index.js diff --git a/bot.js b/bot.js index 1186edf..2d9a890 100644 --- a/bot.js +++ b/bot.js @@ -1,6 +1,6 @@ require("dotenv").config() const { Telegraf } = require("telegraf") -const { Topics, TopicsList } = require("./topics") +const { Topics } = require("./topics") const wait = hores => new Promise(resolve => setTimeout(resolve, hores * 60 * 60 * 1000)) const News = require("./news") const mongoose = require("mongoose") @@ -8,6 +8,8 @@ const bot = new Telegraf(process.env.BOT_TOKEN) const chatId = process.env.CHAT_ID const dayjs = require("dayjs") const Article = require("./models/Article") +const conf = require("./config") +const cron = require("node-cron") bot.start(ctx => ctx.reply("Bot iniciat!")) @@ -42,43 +44,42 @@ bot.hears("saluda_picateclas", ctx => { Topics(bot) -const main = async () => { +const initDB = async () => { try { await mongoose.connect(process.env.MONGODB, { useNewUrlParser: true, useUnifiedTopology: true }) - + console.log("Mongo connected...") } catch (error) { console.error("PROBLEMES DE CONEXIO") console.error(error) process.exit(0) } +} - for (;;) { - for (let news_id of Object.keys(News)) { - let news_items = [] - try { - console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} -> SCRAPPING: ${news_id}`) - news_items = await News[news_id]() - for (let item of news_items) { - try { - let url = item.link.trim().toLowerCase() - console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} -> ${url}`) - await Article.create({ url }) - await bot.telegram.sendMessage(chatId, url) - console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} : Enviat: ${item.link}`) - } catch (error) { - console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} : Exists: ${item.link}`) - console.log(error) - } - await wait(1) +const notificationNews = async () => { + for (let news_id of Object.keys(News)) { + let news_items = [] + try { + console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} -> SCRAPPING: ${news_id}`) + news_items = await News[news_id]() + for (let item of news_items) { + try { + let url = item.link.trim().toLowerCase() + console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} -> ${url}`) + await Article.create({ url }) + await bot.telegram.sendMessage(chatId, url) + console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} : Enviat: ${item.link}`) + } catch (error) { + console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} : Exists: ${item.link}`) + console.log(error) } - } catch (error) { - console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} : URL: ${news_id} PROBLEM`) + await wait(0.1) } - await wait(2) + } catch (error) { + console.log(`${dayjs().format("YYYY-MM-DD HH:mm:ss")} : URL: ${news_id} PROBLEM`) } } } @@ -91,4 +92,10 @@ bot process.once("SIGINT", () => bot.stop("SIGINT")) process.once("SIGTERM", () => bot.stop("SIGTERM")) -main() +initDB() + +for(let cronConf of conf.crons) { + cron.schedule(cronConf, function() { + notificationNews() + }) +} diff --git a/config/index.js b/config/index.js new file mode 100644 index 0000000..aea9339 --- /dev/null +++ b/config/index.js @@ -0,0 +1,8 @@ +const conf = { + crons: [ + '0 12 * * *', + '0 17 * * *' + ] +} + +module.exports = conf \ No newline at end of file diff --git a/package.json b/package.json index 54528b1..6fda272 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "dayjs": "^1.10.4", "dotenv": "^8.2.0", "mongoose": "^5.10.7", + "node-cron": "^2.0.3", "rss-parser": "^3.10.0", "telegraf": "^4.0.1", "telegraf-inline-menu": "^5.3.0",