def test_clima_ordering(self): CommandCall(user='******', _value=CommandCall.CLIMA, _datetime=pendulum.yesterday()).save() c2 = CommandCall(user='******', _value=CommandCall.CLIMA, _datetime=pendulum.now()).save() self.assertAlmostEqual(CommandCall.objects.count(), 2) self.assertEqual(CommandCall.last_clima(), c2)
def test_ordering(self): c1 = CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.yesterday()).save() CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.now()).save() self.assertAlmostEqual(CommandCall.objects.count(), 2) self.assertEqual(CommandCall.objects.first(), c1)
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)
def weather(bot, update, args): """Define weather at certain location""" user = update.message.from_user bot_instance = BotTelegramCore.instance() if bot_instance.is_from_oficial_chat(update): if not CommandCall.allow_call(command=CommandCall.CLIMA): last_call = CommandCall.last_clima() bot.sendMessage(chat_id=user.id, text=COMMAND_THROTTLED.format( segundos=last_call.cooldown_left, comando=last_call.value)) return api_key = config('OPENWEATHERMAP_TOKEN') owm = pyowm.OWM(api_key) text_location = " ".join(args) try: observation = owm.weather_at_place(text_location) _weather = observation.get_weather() humidity = _weather.get_humidity() wind = _weather.get_wind() temp = _weather.get_temperature('celsius') update.message.reply_text(f"🧭 Localização: {text_location}\n" f"🔥️ Temp. Maxima: " f"{temp.get('temp_max')} °C \n" f"❄️ Temp. Minima: " f"{temp.get('temp_min')} °C \n" f"💨 Vel. do Vento: " f"{wind.get('speed')} m/s \n" f"💧 Humidade: " f"{humidity}%") if bot_instance.is_from_oficial_chat(update): CommandCall.clima(user.username) except NotFoundError: update.message.reply_text(f"⚠️ Não consegui localizar a cidade " f"{text_location}!") except APICallError: update.message.reply_text(f"⚠️ Você precisa digitar uma cidade")
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')
def test_clima(self): CommandCall.clima('123') self.assertAlmostEqual(CommandCall.objects.count(), 1) self.assertEqual(CommandCall.objects.first()._value, CommandCall.CLIMA)
def test_allow_call_coach_false(self): CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.now()).save() self.assertFalse(CommandCall.allow_call(command=CommandCall.COACH))
def test_allow_call_coach_true(self): CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.yesterday()).save() self.assertTrue(CommandCall.allow_call(command=CommandCall.COACH))
def test_allow_call_coach_true_empty(self): self.assertTrue(CommandCall.allow_call(command=CommandCall.COACH))
def test_is_cooldown_over_false(self): c = CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.now()).save() self.assertFalse(c.is_cooldown_over())
def test_is_cooldown_over_true_2(self): c = CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.now().subtract( seconds=CommandCall.COOLDOWN)).save() self.assertTrue(c.is_cooldown_over())
def test_is_cooldown_over_true(self): c = CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.yesterday()).save() self.assertTrue(c.is_cooldown_over())
def test_allow_call_clima_true_empty(self): self.assertTrue(CommandCall.allow_call(command=CommandCall.CLIMA))
def test_get_value(self): c = CommandCall.coach('123') self.assertAlmostEqual(CommandCall.objects.count(), 1) self.assertEqual(c.value, CommandCall.CHOICES.get(CommandCall.COACH))
def test_coach(self): CommandCall.coach('123') self.assertAlmostEqual(CommandCall.objects.count(), 1) self.assertEqual(CommandCall.objects.first()._value, CommandCall.COACH)
def test_allow_call_coach_clima_true(self): CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.now()).save() self.assertTrue(CommandCall.allow_call(command=CommandCall.CLIMA))
def test_allow_call_shared_false_2(self): CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.now()).save() self.assertFalse(CommandCall.allow_call(shared=True))
def test_allow_call_shared_true_2(self): CommandCall(user='******', _value=CommandCall.COACH, _datetime=pendulum.yesterday()).save() self.assertTrue(CommandCall.allow_call(shared=True))
def test_allow_call_shared_true(self): self.assertTrue(CommandCall.allow_call(shared=True))