Skip to content

Commit

Permalink
Merge pull request #16 from girona/feature/crons
Browse files Browse the repository at this point in the history
  • Loading branch information
warlock authored Feb 4, 2021
2 parents f37daf6 + f8c63e8 commit 5a78827
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
57 changes: 32 additions & 25 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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")
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!"))

Expand Down Expand Up @@ -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`)
}
}
}
Expand All @@ -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()
})
}
8 changes: 8 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const conf = {
crons: [
'0 12 * * *',
'0 17 * * *'
]
}

module.exports = conf
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5a78827

Please sign in to comment.