Skip to content

Instantly share code, notes, and snippets.

@jkcgs
Created December 2, 2018 04:53
Show Gist options
  • Save jkcgs/6d9d8260cc4de45a158dedb6b1359a0a to your computer and use it in GitHub Desktop.
Save jkcgs/6d9d8260cc4de45a158dedb6b1359a0a to your computer and use it in GitHub Desktop.
Módulo de bot que hice a la rápida para trackear la meta de la Teletón 2018
import discord
import locale
from bot import Command
from bs4 import BeautifulSoup
locale.setlocale(locale.LC_MONETARY, '')
class MetaT(Command):
def __init__(self, bot):
super().__init__(bot)
self.name = 'teleton'
self.actual = -1
self.meta = -1
async def on_ready(self):
st = self.bot.sv_config.get('all', 'mt_done', '0')
if st == '1':
return
self.bot.schedule(self.task, 15)
async def task(self):
st = self.bot.sv_config.get('all', 'mt_done', '0')
if st == '1':
return
self.log.debug('Loading teleton.cl')
async with self.http.get('https://teleton.cl/') as r:
text = await r.read()
dom = BeautifulSoup(text.decode('utf-8'), 'html.parser')
self.actual = int(dom.find(id="computoValue2").get_text())
self.meta = int(dom.find(class_="meta__value--small").find_all(name='span')[1].get_text().replace('.', ''))
if self.actual >= self.meta:
self.bot.sv_config.set('all', 'mt_done', '1')
await self.bot.send_message(discord.Object(id='319199984580952072'),
'Meta superada! {}'.format(self.texto()))
async def handle(self, cmd):
await cmd.answer(self.texto())
def texto(self):
t = 'Actual: {} - Meta: {}'.format(
locale.currency(self.actual, grouping=True)[:-1],
locale.currency(self.meta, grouping=True)[:-1])
if self.actual < self.meta:
t += ' (falta: {})'.format(locale.currency(self.meta - self.actual, grouping=True)[:-1])
return t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment