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

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

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ venv-pg
.idea/
output.log
msgid
tmppic.jpg

20
main.py
View File

@ -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'])
@ -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)