video-stream/driver/utils.py

145 lines
4.1 KiB
Python
Raw Normal View History

2021-12-11 08:53:52 +00:00
import os
import asyncio
2021-11-26 07:11:09 +00:00
from driver.veez import bot, call_py
2021-10-28 15:16:30 +00:00
from pytgcalls.types import Update
2021-10-26 08:59:28 +00:00
from pytgcalls.types.input_stream import AudioPiped, AudioVideoPiped
2022-01-18 07:01:51 +00:00
from driver.queues import (
QUEUE,
clear_queue,
get_queue,
pop_an_item,
)
2021-10-30 23:06:11 +00:00
from pytgcalls.types.input_stream.quality import (
HighQualityAudio,
HighQualityVideo,
LowQualityVideo,
MediumQualityVideo,
)
from pyrogram.types import (
CallbackQuery,
InlineKeyboardButton,
InlineKeyboardMarkup,
Message,
)
from pyrogram import Client, filters
2021-10-28 15:16:30 +00:00
from pytgcalls.types.stream import StreamAudioEnded, StreamVideoEnded
2021-10-26 08:59:28 +00:00
2021-10-28 15:16:30 +00:00
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(text="• Mᴇɴ", callback_data="cbmenu"),
InlineKeyboardButton(text="• Cʟsᴇ", callback_data="cls"),
]
]
)
2021-10-26 08:59:28 +00:00
async def skip_current_song(chat_id):
2021-10-30 23:06:11 +00:00
if chat_id in QUEUE:
chat_queue = get_queue(chat_id)
if len(chat_queue) == 1:
await call_py.leave_group_call(chat_id)
clear_queue(chat_id)
return 1
else:
2021-11-21 05:32:21 +00:00
try:
songname = chat_queue[1][0]
url = chat_queue[1][1]
link = chat_queue[1][2]
type = chat_queue[1][3]
2022-01-18 05:52:25 +00:00
resol = chat_queue[1][4]
image = chat_queue[1][5]
2022-01-18 07:01:51 +00:00
duration = chat_queue[1][6]
2021-11-21 05:32:21 +00:00
if type == "Audio":
await call_py.change_stream(
chat_id,
AudioPiped(
url,
),
)
elif type == "Video":
2022-01-18 05:52:25 +00:00
if resol == 720:
2021-11-21 05:32:21 +00:00
hm = HighQualityVideo()
2022-01-18 05:52:25 +00:00
elif resol == 480:
2021-11-21 05:32:21 +00:00
hm = MediumQualityVideo()
2022-01-18 05:52:25 +00:00
elif resol == 360:
2021-11-21 05:32:21 +00:00
hm = LowQualityVideo()
await call_py.change_stream(
2022-01-18 05:52:25 +00:00
chat_id,
AudioVideoPiped(
url,
HighQualityAudio(),
hm
)
2021-11-21 05:32:21 +00:00
)
pop_an_item(chat_id)
2022-01-18 07:01:51 +00:00
return [songname, link, type, image, duration]
2021-11-21 05:32:21 +00:00
except:
await call_py.leave_group_call(chat_id)
clear_queue(chat_id)
return 2
2021-10-30 23:06:11 +00:00
else:
return 0
2021-10-26 08:59:28 +00:00
async def skip_item(chat_id, h):
2021-10-30 23:06:11 +00:00
if chat_id in QUEUE:
chat_queue = get_queue(chat_id)
try:
x = int(h)
songname = chat_queue[x][0]
chat_queue.pop(x)
return songname
except Exception as e:
print(e)
return 0
else:
return 0
2021-10-26 08:59:28 +00:00
2021-11-21 05:32:21 +00:00
@call_py.on_kicked()
async def kicked_handler(_, chat_id: int):
if chat_id in QUEUE:
clear_queue(chat_id)
@call_py.on_closed_voice_chat()
async def closed_voice_chat_handler(_, chat_id: int):
if chat_id in QUEUE:
clear_queue(chat_id)
@call_py.on_left()
async def left_handler(_, chat_id: int):
if chat_id in QUEUE:
clear_queue(chat_id)
2021-10-26 08:59:28 +00:00
@call_py.on_stream_end()
2021-11-26 07:09:11 +00:00
async def stream_end_handler(_, u: Update):
2021-11-27 04:08:55 +00:00
if isinstance(u, StreamAudioEnded):
2021-11-01 04:17:54 +00:00
chat_id = u.chat_id
print(chat_id)
op = await skip_current_song(chat_id)
if op==1:
2022-01-08 15:36:21 +00:00
await bot.send_message(chat_id, "✅ streaming end")
elif op==2:
2022-01-08 15:36:21 +00:00
await bot.send_message(chat_id, "❌ an error occurred\n\n» **Clearing** __Queues__ and leaving video chat.")
else:
2022-01-18 07:01:51 +00:00
await bot.send_message(chat_id, f"💡 **Streaming next track**\n\n🏷 **Name:** [{op[0]}]({op[1]}) | `{op[2]}`\n⏱ **Duration:** `{op[3]}`", disable_web_page_preview=True, reply_markup=keyboard)
else:
pass
2021-12-11 08:42:51 +00:00
async def bash(cmd):
process = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await process.communicate()
err = stderr.decode().strip()
out = stdout.decode().strip()
return out, err