From 7e21baf82aa9dc32d56a23852505640700122a24 Mon Sep 17 00:00:00 2001 From: lulzette Date: Wed, 14 Dec 2022 23:12:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D0=B0=D0=BC=D0=BE=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D1=8E=D1=89=D1=83=D1=8E=D1=81=D1=8F=20=D1=84=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D1=83=20=D1=81=D1=82=D1=80=D0=B8=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- main.py | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) 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