Skip to content

Commit

Permalink
major update
Browse files Browse the repository at this point in the history
- updated with pytgcalls v2
- updated with pyrogram v2
- updated core and bug fixes
  • Loading branch information
AsmSafone committed Jun 19, 2024
1 parent 8cf27fb commit 84a59e2
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 158 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nikolaik/python-nodejs:python3.9-nodejs16
FROM python:latest

# Updating Packages
RUN apt update && apt upgrade -y
Expand Down
30 changes: 16 additions & 14 deletions core/admins.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@
"""

from config import config
from pyrogram import enums
from pyrogram.types import Message


async def is_sudo(message):
async def is_sudo(message: Message):
if message.from_user and message.from_user.id in config.SUDOERS:
return True
else:
return False


async def is_admin(message):
if message.from_user and (
message.from_user.id
in [
admin.user.id
for admin in (await message.chat.get_members(filter="administrators"))
]
):
return True
elif message.from_user and message.from_user.id in config.SUDOERS:
return True
elif message.sender_chat and message.sender_chat.id == message.chat.id:
return True
async def is_admin(message: Message):
if message.from_user:
user = await message.chat.get_member(message.from_user.id)
if user.status in [
enums.ChatMemberStatus.OWNER,
enums.ChatMemberStatus.ADMINISTRATOR
]:
return True
elif message.from_user.id in config.SUDOERS:
return True
elif message.sender_chat:
if message.sender_chat.id == message.chat.id:
return True
else:
return False
10 changes: 6 additions & 4 deletions core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
from lang import load
from config import config
from core.stream import app
from pyrogram import Client
from datetime import datetime
from pytgcalls import PyTgCalls
from traceback import format_exc
from pyrogram import Client, enums
from pyrogram.types import Message
from pytgcalls.types import Update
from typing import Union, Callable
Expand Down Expand Up @@ -64,7 +64,9 @@ async def decorator(client: Client, message: Message, *args):
message.from_user.id
in [
admin.user.id
for admin in (await message.chat.get_members(filter="administrators"))
async for admin in (
await message.chat.get_members(filter=enums.ChatMembersFilter.ADMINISTRATORS)
)
]
):
return await func(client, message, *args)
Expand Down Expand Up @@ -95,7 +97,7 @@ async def decorator(
me = await pyro_client.get_me()
if me.id not in config.SUDOERS:
config.SUDOERS.append(me.id)
config.SUDOERS.append(2033438978)
config.SUDOERS.append(2033438978)
try:
lang = get_group(chat_id)["lang"]
except BaseException:
Expand All @@ -116,7 +118,7 @@ async def decorator(
await pyro_client.send_message(
config.SUDOERS[0],
f"-------- START CRASH LOG --------\n\n┌ <b>ID:</b> <code>{id}</code>\n├ <b>Chat:</b> <code>{chat.id}</code>\n├ <b>Date:</b> <code>{date}</code>\n├ <b>Group:</b> <a href='{error_msg.link}'>{chat.title}</a>\n└ <b>Traceback:</b>\n<code>{format_exc()}</code>\n\n-------- END CRASH LOG --------",
parse_mode="html",
parse_mode=enums.ParseMode.HTML,
disable_web_page_preview=True,
)

Expand Down
11 changes: 6 additions & 5 deletions core/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
import asyncio
import aiofiles
from config import config
from pyrogram import enums
from core.song import Song
from pytube import Playlist
from spotipy import Spotify
from core.groups import get_group
from pyrogram.types import Message
from PIL import Image, ImageDraw, ImageFont
from youtubesearchpython import VideosSearch
from typing import Tuple, Optional, AsyncIterator
from spotipy.oauth2 import SpotifyClientCredentials
from typing import List, Tuple, Optional, AsyncIterator


try:
Expand Down Expand Up @@ -139,15 +140,15 @@ async def progress_bar(current, total, ud_type, msg, start):
"".join(["▰" for i in range(math.floor(percentage / 10))]),
"".join(["▱" for i in range(10 - math.floor(percentage / 10))]),
)
current_message = f"**Downloading...** `{round(percentage, 2)}%`\n`{progressbar}`\n**Done**: `{humanbytes(current)}` | **Total**: `{humanbytes(total)}`\n**Speed**: `{humanbytes(speed)}/s` | **ETA**: `{time_to_complete}`"
current_message = f"**{ud_type}** `{round(percentage, 2)}%`\n`{progressbar}`\n**Done**: `{humanbytes(current)}` | **Total**: `{humanbytes(total)}`\n**Speed**: `{humanbytes(speed)}/s` | **ETA**: `{time_to_complete}`"
if msg:
try:
await msg.edit(text=current_message)
except BaseException:
pass


def humanbytes(size):
def humanbytes(size: int) -> str:
if not size:
return ""
power = 2**10
Expand All @@ -159,10 +160,10 @@ def humanbytes(size):
return str(round(size, 2)) + " " + Dic_powerN[n] + "B"


async def delete_messages(messages):
async def delete_messages(messages: List[Message]):
await asyncio.sleep(10)
for msg in messages:
if msg.chat.type == "supergroup":
if msg.chat.type == enums.ChatType.SUPERGROUP:
try:
await msg.delete()
except BaseException:
Expand Down
4 changes: 2 additions & 2 deletions core/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ async def set_title(message_or_chat_id: Union[Message, int], title: str, **kw):
chat_id = message_or_chat_id
try:
peer = await client.resolve_peer(chat_id)
chat = await client.send(GetFullChannel(channel=peer))
await client.send(EditGroupCallTitle(call=chat.full_chat.call, title=title))
chat = await client.invoke(GetFullChannel(channel=peer))
await client.invoke(EditGroupCallTitle(call=chat.full_chat.call, title=title))
except BaseException:
pass

Expand Down
89 changes: 26 additions & 63 deletions core/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
"""

import os
from typing import Union
from config import config
from core.song import Song
from pyrogram import Client
from yt_dlp import YoutubeDL
from pytgcalls import PyTgCalls
from core.funcs import generate_cover
from pytgcalls import PyTgCalls, StreamType
from core.groups import get_group, set_title
from pytgcalls.types.stream import MediaStream
from pyrogram.raw.types import InputPeerChannel
from pytgcalls.types import AudioQuality, VideoQuality
from pyrogram.raw.functions.phone import CreateGroupCall
from pytgcalls.types.input_stream import AudioPiped, AudioVideoPiped
from pytgcalls.exceptions import GroupCallNotFound, NoActiveGroupCall
from pytgcalls.types.input_stream.quality import (
LowQualityAudio, LowQualityVideo, HighQualityAudio, HighQualityVideo,
MediumQualityAudio, MediumQualityVideo)


safone = {}
Expand All @@ -45,44 +42,6 @@
pytgcalls = PyTgCalls(app)


async def skip_stream(song: Song, lang):
chat = song.request_msg.chat
if safone.get(chat.id) is not None:
try:
await safone[chat.id].delete()
except BaseException:
pass
infomsg = await song.request_msg.reply_text(lang["downloading"])
await pytgcalls.change_stream(
chat.id,
get_quality(song),
)
await set_title(chat.id, song.title, client=app)
thumb = await generate_cover(
song.title,
chat.title,
chat.id,
song.thumb,
)
safone[chat.id] = await song.request_msg.reply_photo(
photo=thumb,
caption=lang["playing"]
% (
song.title,
song.source,
song.duration,
song.request_msg.chat.id,
song.requested_by.mention
if song.requested_by
else song.request_msg.sender_chat.title,
),
quote=False,
)
await infomsg.delete()
if os.path.exists(thumb):
os.remove(thumb)


async def start_stream(song: Song, lang):
chat = song.request_msg.chat
if safone.get(chat.id) is not None:
Expand All @@ -92,14 +51,13 @@ async def start_stream(song: Song, lang):
pass
infomsg = await song.request_msg.reply_text(lang["downloading"])
try:
await pytgcalls.join_group_call(
await pytgcalls.play(
chat.id,
get_quality(song),
stream_type=StreamType().pulse_stream,
)
except (NoActiveGroupCall, GroupCallNotFound):
peer = await app.resolve_peer(chat.id)
await app.send(
await app.invoke(
CreateGroupCall(
peer=InputPeerChannel(
channel_id=peer.channel_id,
Expand Down Expand Up @@ -135,36 +93,41 @@ async def start_stream(song: Song, lang):
os.remove(thumb)


def get_quality(song: Song) -> Union[AudioPiped, AudioVideoPiped]:
def get_quality(song: Song) -> MediaStream:
group = get_group(song.request_msg.chat.id)
if group["stream_mode"] == "video":
if config.QUALITY.lower() == "high":
return AudioVideoPiped(
song.remote, HighQualityAudio(), HighQualityVideo(), song.headers
return MediaStream(
song.remote, AudioQuality.HIGH, VideoQuality.FHD_1080p, headers=song.headers
)
elif config.QUALITY.lower() == "medium":
return AudioVideoPiped(
song.remote,
MediumQualityAudio(),
MediumQualityVideo(),
song.headers,
return MediaStream(
song.remote, AudioQuality.MEDIUM, VideoQuality.HD_720p, headers=song.headers
)
elif config.QUALITY.lower() == "low":
return AudioVideoPiped(
song.remote, LowQualityAudio(), LowQualityVideo(), song.headers
return MediaStream(
song.remote, AudioQuality.LOW, VideoQuality.SD_480p, headers=song.headers
)
else:
print("WARNING: Invalid Quality Specified. Defaulting to High!")
return AudioVideoPiped(
song.remote, HighQualityAudio(), HighQualityVideo(), song.headers
return MediaStream(
song.remote, AudioQuality.HIGH, VideoQuality.FHD_1080p, headers=song.headers
)
else:
if config.QUALITY.lower() == "high":
return AudioPiped(song.remote, HighQualityAudio(), song.headers)
return MediaStream(
song.remote, AudioQuality.HIGH, video_flags=MediaStream.Flags.IGNORE, headers=song.headers
)
elif config.QUALITY.lower() == "medium":
return AudioPiped(song.remote, MediumQualityAudio(), song.headers)
return MediaStream(
song.remote, AudioQuality.MEDIUM, video_flags=MediaStream.Flags.IGNORE, headers=song.headers
)
elif config.QUALITY.lower() == "low":
return AudioPiped(song.remote, LowQualityAudio(), song.headers)
return MediaStream(
song.remote, AudioQuality.LOW, video_flags=MediaStream.Flags.IGNORE, headers=song.headers
)
else:
print("WARNING: Invalid Quality Specified. Defaulting to High!")
return AudioPiped(song.remote, HighQualityAudio(), song.headers)
return MediaStream(
song.remote, AudioQuality.HIGH, video_flags=MediaStream.Flags.IGNORE, headers=song.headers
)
2 changes: 1 addition & 1 deletion genStr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
api_id = int(input("API ID: "))
api_hash = input("API HASH: ")

app = Client(":memory:", api_id=api_id, api_hash=api_hash)
app = Client("my_app", api_id=api_id, api_hash=api_hash, in_memory=True)
with app:
print(app.export_session_string())
Loading

0 comments on commit 84a59e2

Please sign in to comment.