Skip to content

Instantly share code, notes, and snippets.

@reisdev
Last active August 9, 2020 04:17
Show Gist options
  • Save reisdev/33cf7dce310b25728971933c508184f7 to your computer and use it in GitHub Desktop.
Save reisdev/33cf7dce310b25728971933c508184f7 to your computer and use it in GitHub Desktop.
import os
from telegram.ext import Updater, CommandHandler
from telegram import Bot
import telegram
from scrapy.exceptions import CloseSpider
from dotenv import load_dotenv
load_dotenv(".env")
class ReadsBot():
bot = Bot(os.getenv("BOT_TOKEN"))
updater = Updater(os.getenv("BOT_TOKEN"), use_context=True)
subscribers = set()
def __init__(self,stats={},crawler = 10):
self.stats = stats
self.crawler = crawler
self.updater.dispatcher.add_handler(
CommandHandler("status",self.notifyStatus)
)
self.updater.dispatcher.add_handler(
CommandHandler("get_id",self.getId)
)
self.updater.start_polling()
def getId(self,update,context):
update.message.reply_text("Seu id é *{}*!".format(update.message.chat.id),parse_mode=telegram.ParseMode.MARKDOWN)
def getStats(self):
stats = self.crawler.stats.get_stats()
data = {}
try:
data["start_time"] = stats['start_time'].strftime("%H:%M:%S %d/%m/%Y")
except:
data["start_time"] = ""
pass
try:
data["item_counter"] = stats['item_scraped_count']
except:
data["item_counter"] = 0
pass
try:
data["error"] = stats["log_count/ERROR"]
except:
data["error"] = 0
try:
data["finish_time"] = stats["finish_time"].strftime("%H:%M:%S %d/%m/%Y")
except:
data["finish_time"] = ""
return data
def subscribe(self,update,context):
self.subscribers.add(update.message.chat.id)
update.message.reply_text("Agora você receberá atualizações do R.E.A.D.S!")
def notifyEnd(self,chatId,reason):
data = self.getStats()
text = """
R.E.A.D.S. - Extração Encerrada\n
Hora de início: {start_time}
Hora de término: {finish_time}\n
- Quantidade de imóveis extraída: {item_counter}
- Quantidade de erros: {error}
""".format(**data)
self.bot.sendMessage(int(chatId),text)
self.endNotified = True
def notifyStatus(self, update, context):
data = self.getStats()
update.message.reply_text("""
Status do R.E.A.D.S.\n
Hora de início: {start_time}\n
- Quantidade de imóveis extraída: {item_counter}
- Quantidade de erros: {error}
""".format(**data))
def stopCrawler(self,update,context):
update.message.reply_text("Forçando a parada do crawler...")
raise CloseSpider("forced")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment