示例#1
0
def corona_stats(update: Update, context: CallbackContext):
    if update.message is not None and update.message.from_user == update.effective_user:
        CSVStats.date = parseDateFromString(update.effective_message.text)
    csvStat = CSVStats("todaystats.csv")
    if csvStat.status_code != 200:
        keyboard = [[InlineKeyboardButton("Yes, show me data from previous day", callback_data="True"),
                     InlineKeyboardButton("No, thank you", callback_data="False")]]
        reply_markup = InlineKeyboardMarkup(keyboard)
        bot.send_message(chat_id=update.effective_chat['id'],
                         text=f"Something went wrong. Maybe, there is no data yet from {CSVStats.date}."
                              f"Do you want to now data from previous day?", reply_markup=reply_markup)
    else:
        csvStat.changeRequest()
        top_five = csvStat.getTopFiveProvinces()
        # print(top_five)
        text = "TOP-5 infected regions:\n"
        for i in range(len(top_five)):
            text += f'{i + 1}. {top_five[i]["province"]} - {top_five[i]["new infected"]} infected\n'
        if update.message is not None and update.message.from_user == update.effective_user:
            bot.send_message(chat_id=update.effective_message.chat_id,
                             message_id=update.effective_message.message_id,
                             text=f"COVID-19 statistics dynamics from {CSVStats.date}\n{text}")
        else:
            bot.edit_message_text(chat_id=update.effective_message.chat_id,
                                  message_id=update.effective_message.message_id,
                                  text=f"COVID-19 statistics dynamics from {CSVStats.date}\n{text}")
        CSVStats.date = datetime.date.today().strftime("%m-%d-%Y")
示例#2
0
class TestCSVStats(unittest.TestCase):
    def setUp(self):
        self.csvstats = CSVStats("test.csv")

    def tearDown(self):
        f = open("test.csv", "w")
        f.close()

    @mock.patch.object(CSVStats, 'getTopFiveProvinces', return_value=123)
    def test_mock_getTopFiveProvinces(self, mock_getTopFiveProvinces):
        self.assertEqual(self.csvstats.getTopFiveProvinces(), 123)
示例#3
0
def corona_stats(update: Update, context: CallbackContext):
    if update.message is not None and update.message.from_user == update.effective_user:
        CSVStats.date = parseDateFromString(update.effective_message.text)
    csvStat = CSVStats("todaystats.csv")
    if csvStat.status_code != 200:
        keyboard = [[
            InlineKeyboardButton("Да, покажи данные за предыдущий день",
                                 callback_data="True"),
            InlineKeyboardButton("Нет, спасибо", callback_data="False")
        ]]
        reply_markup = InlineKeyboardMarkup(keyboard)
        bot.send_message(
            chat_id=update.effective_chat['id'],
            text=
            f"Что-то пошло не так. Возможно, данные за {CSVStats.date} еще не появились. "
            f"Хотите посмотреть данные за предыдущий день?",
            reply_markup=reply_markup)
    else:
        csvStat.changeRequest()
        top_five = csvStat.getTopFiveProvinces()
        # print(top_five)
        text = "Топ зараженных провинций:\n"
        for i in range(len(top_five)):
            text += f'{i + 1}. {top_five[i]["province"]} - {top_five[i]["new infected"]} заражённых\n'
        if update.message is not None and update.message.from_user == update.effective_user:
            bot.send_message(
                chat_id=update.effective_message.chat_id,
                message_id=update.effective_message.message_id,
                text=
                f"Статистика заражённых COVID-19 за {CSVStats.date}\n{text}")
        else:
            bot.edit_message_text(
                chat_id=update.effective_message.chat_id,
                message_id=update.effective_message.message_id,
                text=
                f"Статистика заражённых COVID-19 за {CSVStats.date}\n{text}")
        CSVStats.date = datetime.date.today().strftime("%m-%d-%Y")
class TestCSVStats(unittest.TestCase):
    def setUp(self):
        self.csvstats = CSVStats("test.csv")

    def tearDown(self):
        f = open("test.csv", "w")
        f.close()

    @mock.patch.object(CSVStats, 'getTopFiveProvinces', return_value=123)
    def test_mock_getTopFiveProvinces(self, mock_getTopFiveProvinces):
        self.assertEqual(self.csvstats.getTopFiveProvinces(), 123)

    def testParseDateFromString1(self):
        string = "11.04.2020"
        res = "04-11-2020"
        self.assertEqual(res, parseDateFromString(string))

    def testParseDateFromString2(self):
        string = "11..04.///2020"
        res = "04-11-2020"
        self.assertEqual(res, parseDateFromString(string))

    def testParseDateFromString3(self):
        string = "11..04"
        res = "04-11-2020"
        self.assertEqual(res, parseDateFromString(string))

    def testParseDateFromString4(self):
        string = "/corona_stats Хочу получить данные за 11..04.///2020"
        res = "04-11-2020"
        self.assertEqual(res, parseDateFromString(string))

    def testParseDateFromString5(self):
        string = "Данные за 41..04"
        res = date.today().strftime("%m-%d-%Y")
        self.assertEqual(res, parseDateFromString(string))
 def setUp(self):
     self.csvstats = CSVStats("test.csv")
示例#6
0
def corona_stats_dynamics(update: Update, context: CallbackContext):
    today_stats = CSVStats("today_stats.csv")
    yesterday_stats = CSVStats("yesterday_stats.csv")
    while today_stats.status_code != 200:
        today_stats.date = (datetime.datetime.strptime(today_stats.date, "%m-%d-%Y") -
                            datetime.timedelta(days=1)).strftime("%m-%d-%Y")
        today_stats.changeRequest()
    yesterday_stats.date = (datetime.datetime.strptime(today_stats.date, "%m-%d-%Y") -
                            datetime.timedelta(days=1)).strftime("%m-%d-%Y")
    yesterday_stats.changeRequest()
    today_top_five = today_stats.getTopFiveProvinces()
    yesterday_top_five = yesterday_stats.getTopFiveProvinces()
    text = f"COVID-19 statistics dynamics from {yesterday_stats.date} to {today_stats.date}: \n"
    for i in range(5):
        old_infected = 0
        for j in range(len(yesterday_top_five)):
            if today_top_five[i]["province"] == yesterday_top_five[j]["province"]:
                old_infected = yesterday_top_five[j]["new infected"]
                break
        text += f'{i + 1}. {today_top_five[i]["province"]} - {today_top_five[i]["new infected"]} ' \
                f'(+{today_top_five[i]["new infected"] - old_infected}) infected\n'
    bot.send_message(chat_id=update.effective_message.chat_id, message_id=update.effective_message.message_id,
                     text=f"{text}")