diff --git a/.gitignore b/.gitignore index 59be168..0d8dfdb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ config_python.py venv-pg .idea/ -output.log \ No newline at end of file +output.log +msgid \ No newline at end of file diff --git a/main.py b/main.py index 1700646..2fd6000 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,10 @@ from logging.handlers import TimedRotatingFileHandler from termcolor import colored import os import sys +import telebot + +import main + streamer = config_python.streamer app_id = config_python.appid app_secret = config_python.appsecret @@ -15,19 +19,58 @@ log_format = logging.Formatter('%(asctime)s %(levelname)s:%(message)s') log_file = 'output.log' -# Если стрим идет def stream_message_worker(data: dict): # Если это стрим, а не повтор, значит все в порядке if data['type'] == 'live': + # Для удобства выносим в переменные game_name = data['game_name'] stream_title = data['title'] stream_viewers = str(data['viewer_count']) - log.info(streamer + " стримит " + game_name + " Title: " + stream_title + " Зрителей: " + stream_viewers) + # Готовим сообщение + message = streamer + " стримит " + game_name + " Title: " + stream_title + " Зрителей: " + stream_viewers + log.info(message) + # Если файл с айди существует, читаем его и редактируем сообщение из него + if os.path.exists('msgid'): + with open('msgid', 'r') as f: + msgid = f.read() + if msgid == '': + msgid = 0 + else: + msgid = 0 + + if msgid == 0: + send_message = bot.send_message(config_python.tgchat, message) + print(send_message) + msgid = send_message.message_id + with open('msgid', 'w') as f: + f.write(str(msgid)) + else: + try: + edit_msg = bot.edit_message_text(message, config_python.tgchat, int(msgid)) + except telebot.apihelper.ApiTelegramException as e: + log.error(e) # Если стрим не идет def no_stream_msg_worker(): log.info(streamer + " Не стримит") + # Проверяем наличие файла + if os.path.exists('msgid'): + # Если файл существует - читаем + with open('msgid', 'r') as f: + msgid = f.read() + if msgid == '': + # Если пустой - удаляем + os.remove('msgid') + else: + # Если не пустой, то удаляем сообщение по айдишнику + try: + # Удаляем сообщение + edit_msg = bot.delete_message(config_python.tgchat, int(msgid)) + except telebot.apihelper.ApiTelegramException as e: + log.error(e) + # В конце удаляем айди сообщения + os.remove('msgid') def check_alive(): @@ -83,6 +126,8 @@ if __name__ == '__main__': log = get_logger("main") log.info("Запущен") twitch_client = Twitch(app_id, app_secret) + bot = telebot.TeleBot(config_python.tgtoken) + while True: check_alive() - time.sleep(3) + time.sleep(180) diff --git a/requirements.txt b/requirements.txt index cd84a48..bd7eaf1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ twitchAPI==2.5.7.1 -termcolor \ No newline at end of file +termcolor +pyTelegramBotAPI=4.8.0 \ No newline at end of file