Основной код:
bot.py
from telegram.ext import Updater, MessageHandler, Filters from pytube import YouTube import logging logging.basicConfig(level=logging.DEBUG, filename='logs.txt') from yandex import sendToScreen import config last_url = "" authorised_users = [] def getVideoUrl(url): global last_url if url == last_url: # Second attempt - trying another player yt = YouTube(url).streams.first() last_url = url return yt.url last_url = url if "https://www.youtube" in url: url = url.split("&")[0] # Removing arguments if "https://youtu.be" in url: url = "https://www.youtube.com/watch?v=" + url.split("/")[-1] # Page parsing and getting video_url here return url def extractUrl(message, context): return message.text # TODO: getting url by entities info def message_recieved(bot, update, context): user_id = update.message.chat_id print(update.message) if update.message.text == config.bot_password: authorised_users.append(user_id) bot.send_message(chat_id=update.message.chat_id, text="Успешная авторизация!") print(f"Authorised: {user_id}") return if not user_id in authorised_users: bot.send_message(chat_id=update.message.chat_id, text="Доступ закрыт!") print("Unauthorised request blocked!") return url = extractUrl(update.message) video_url = getVideoUrl(url) result = sendToScreen(video_url) print(result) bot.send_message(chat_id=update.message.chat_id, text=result + video_url) updater = Updater(token=config.telegram_bot_token, request_kwargs=config.proxy) print("Начинаю сбор Видосиков =)") updater.start_polling()
Config.py
login = "123@yandex.ru" password = "123" telegram_bot_token = "123" proxy = None
yandex.py
import requests import json import config def sendToScreen(video_url): # Auth and getting Session_id auth_data = { 'login': config.login, 'passwd': config.password } s = requests.Session() s.get("https://passport.yandex.ru/") s.post("https://passport.yandex.ru/passport?mode=auth&retpath=https://yandex.ru", data=auth_data) Session_id = s.cookies["Session_id"] # Getting x-csrf-token token = s.get('https://frontend.vh.yandex.ru/csrf_token').text # Detting devices info TODO: device selection here devices_online_stats = s.get("https://quasar.yandex.ru/devices_online_stats").text devices = json.loads(devices_online_stats)["items"] # Preparing request headers = { "x-csrf-token": token, } data = { "msg": { "provider_item_id": video_url }, "device": devices[0]["id"] } if "https://www.youtube" in video_url: data["msg"]["player_id"] = "youtube" # Sending command with video to device res = s.post("https://yandex.ru/video/station", data=json.dumps(data), headers=headers) return res.text