コード例 #1
0
def coach(bot, update, args):
    user = update.message.from_user
    bot_instance = BotTelegramCore.instance()

    if bot_instance.is_from_oficial_chat(update):
        if not CommandCall.allow_call(command=CommandCall.COACH):
            last_call = CommandCall.last_coach()
            bot.sendMessage(chat_id=user.id,
                            text=COMMAND_THROTTLED.format(
                                segundos=last_call.cooldown_left,
                                comando=last_call.value))
            return

    url_base = 'https://www.pensador.com'

    if len(args) == 0:
        query = "motivacional/"
        url_search = f'{url_base}/{query}/'
    else:
        pre_query = "+".join(args)
        query = f'busca.php?q={pre_query}/'
        url_search = f'{url_base}/{query}&p='

    response = requests.get(url_search, timeout=1)

    soup = BeautifulSoup(response.content, 'html.parser')
    tag_total = soup.find(class_='total')
    if tag_total is None:
        update.message.reply_text(
            "Desculpe, ainda não conheço nada sobre o assunto.")

    text_total = tag_total.get_text()
    total_match = re.search(r"\d+", text_total)
    string_total = total_match.group(0)

    total_pages = int(string_total) // QTY_POSTS_PER_PAGE or 1

    random_page = random.randint(1, total_pages)
    response_two = requests.get(f'{url_search}{random_page}', timeout=1)

    soup_two = BeautifulSoup(response_two.content, 'html.parser')
    frases = soup_two.find_all(class_='fr')

    update.message.reply_text(frases[random.randint(0,
                                                    len(frases) -
                                                    1)].get_text())

    if bot_instance.is_from_oficial_chat(update):
        CommandCall.coach(user.username)
コード例 #2
0
    def test_set_value(self):
        c = CommandCall.coach('123')
        self.assertAlmostEqual(CommandCall.objects.count(), 1)
        self.assertEqual(c.value, CommandCall.CHOICES.get(CommandCall.COACH))

        c.value = '1'
        self.assertEqual(c._value, '1')

        c.value = '2'
        self.assertEqual(c._value, '2')

        self.assertRaises(ValueError, setattr, c, 'value', 'TESTE')
コード例 #3
0
 def test_get_value(self):
     c = CommandCall.coach('123')
     self.assertAlmostEqual(CommandCall.objects.count(), 1)
     self.assertEqual(c.value, CommandCall.CHOICES.get(CommandCall.COACH))
コード例 #4
0
 def test_coach(self):
     CommandCall.coach('123')
     self.assertAlmostEqual(CommandCall.objects.count(), 1)
     self.assertEqual(CommandCall.objects.first()._value, CommandCall.COACH)