diff --git a/.gitignore b/.gitignore index 0d8dfdb..e4f93d1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ config_python.py venv-pg .idea/ output.log -msgid \ No newline at end of file +msgid +tmppic.jpg \ No newline at end of file diff --git a/main.py b/main.py index c7d5f84..73fde21 100644 --- a/main.py +++ b/main.py @@ -8,8 +8,10 @@ from termcolor import colored import os import sys import telebot +from telebot.types import InputFile from datetime import datetime - +import requests +import shutil streamer = config_python.streamer app_id = config_python.appid @@ -27,6 +29,15 @@ def stream_message_worker(data: dict): stream_title = data['title'] stream_viewers = str(data['viewer_count']) + # Получаем скрин стрима + stream_pic_url = data['thumbnail_url'].format(width='1920',height='1080') + picture_file = requests.get(stream_pic_url, stream=True) + picture_file.raw.decode_content = True + # Увы, но я не придумал ничего лучше, чем сохранять фотку во временный файл и потом читать его + with open('tmppic.jpg', 'wb') as f: + picture_file.raw.decode_content = True + shutil.copyfileobj(picture_file.raw, f) + # Считаем длительность стрима # '2022-12-14T17:26:47Z' stream_started_at = str(data['started_at']) @@ -35,9 +46,9 @@ def stream_message_worker(data: dict): stream_duration = time_now - time_started # Готовим сообщение message = stream_title + '\n' + \ - "Игра: " + game_name + '\n' + \ - "Зрителей: " + stream_viewers + '\n' + \ - "Длительность: " + str(stream_duration) + "Игра: " + game_name + '\n' + \ + "Зрителей: " + stream_viewers + '\n' + \ + "Длительность: " + str(stream_duration) log.info(message) # Если файл с айди существует, читаем его и редактируем сообщение из него if os.path.exists('msgid'): @@ -49,14 +60,15 @@ def stream_message_worker(data: dict): msgid = 0 if msgid == 0: - send_message = bot.send_message(config_python.tgchat, message) + send_message = bot.send_photo(config_python.tgchat, InputFile('tmppic.jpg'), 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)) + media = telebot.types.InputMediaPhoto(InputFile('tmppic.jpg'), message) + edit_msg_media = bot.edit_message_media(media, config_python.tgchat, msgid) except telebot.apihelper.ApiTelegramException as e: log.error(e) @@ -140,4 +152,4 @@ if __name__ == '__main__': while True: check_alive() - time.sleep(300) + time.sleep(config_python.period) \ No newline at end of file