Добавил самообновляющуюся фотку стрима

This commit is contained in:
lulzette 2022-12-14 23:12:26 +03:00
parent 21ca98a4b6
commit 7e21baf82a
2 changed files with 21 additions and 8 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@ config_python.py
venv-pg venv-pg
.idea/ .idea/
output.log output.log
msgid msgid
tmppic.jpg

26
main.py
View File

@ -8,8 +8,10 @@ from termcolor import colored
import os import os
import sys import sys
import telebot import telebot
from telebot.types import InputFile
from datetime import datetime from datetime import datetime
import requests
import shutil
streamer = config_python.streamer streamer = config_python.streamer
app_id = config_python.appid app_id = config_python.appid
@ -27,6 +29,15 @@ def stream_message_worker(data: dict):
stream_title = data['title'] stream_title = data['title']
stream_viewers = str(data['viewer_count']) 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' # '2022-12-14T17:26:47Z'
stream_started_at = str(data['started_at']) stream_started_at = str(data['started_at'])
@ -35,9 +46,9 @@ def stream_message_worker(data: dict):
stream_duration = time_now - time_started stream_duration = time_now - time_started
# Готовим сообщение # Готовим сообщение
message = stream_title + '\n' + \ message = stream_title + '\n' + \
"Игра: " + game_name + '\n' + \ "Игра: " + game_name + '\n' + \
"Зрителей: " + stream_viewers + '\n' + \ "Зрителей: " + stream_viewers + '\n' + \
"Длительность: " + str(stream_duration) "Длительность: " + str(stream_duration)
log.info(message) log.info(message)
# Если файл с айди существует, читаем его и редактируем сообщение из него # Если файл с айди существует, читаем его и редактируем сообщение из него
if os.path.exists('msgid'): if os.path.exists('msgid'):
@ -49,14 +60,15 @@ def stream_message_worker(data: dict):
msgid = 0 msgid = 0
if 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) print(send_message)
msgid = send_message.message_id msgid = send_message.message_id
with open('msgid', 'w') as f: with open('msgid', 'w') as f:
f.write(str(msgid)) f.write(str(msgid))
else: else:
try: 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: except telebot.apihelper.ApiTelegramException as e:
log.error(e) log.error(e)
@ -140,4 +152,4 @@ if __name__ == '__main__':
while True: while True:
check_alive() check_alive()
time.sleep(300) time.sleep(config_python.period)