Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Oct 9, 2023
1 parent 0463ba1 commit 4a191b0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 53 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ unknown_errors.txt
.env
.codesandbox

*.lock
*.lock

tg-codes
2 changes: 1 addition & 1 deletion bot/plugins/commands/join.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pyrogram.types import Message
from pyrogram import filters, client
from pyrogram import filters, Client
from bot import on_call, kreacher, VOICE_CHATS
from bot.decorators.only_admins import only_admins
from bot.decorators.only_grps_chnns import only_grps_chnns
Expand Down
2 changes: 1 addition & 1 deletion bot/plugins/commands/leave.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyrogram.types import message
from pyrogram.types import Message
from pyrogram import filters, Client
from bot import kreacher, VOICE_CHATS
from bot.helpers.queues import clear_queue
Expand Down
31 changes: 18 additions & 13 deletions bot/plugins/commands/play_book.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import io
import os
import uuid
import shutup
import PyPDF2
import shutup
import logging
from asyncio import sleep
from bot.helpers.tts import tts
from bot import kreacher, on_call, VOICE_CHATS
from pyrogram.types import Message
from html.parser import HTMLParser
from pyrogram.types import Message
from pyrogram import filters, Client
from ebooklib import epub as epublib, ITEM_IMAGE, ITEM_DOCUMENT

from bot.helpers.tts import tts
from bot.helpers.progress import progress
from bot import kreacher, on_call, VOICE_CHATS
from bot.decorators.only_grps_chnns import only_grps_chnns
from ebooklib import epub as epublib, ITEM_IMAGE, ITEM_DOCUMENT
from bot.helpers.queues import (
clear_queue,
add_or_create_queue,
get_queues,
get_last_position_in_queue,
remove_queue,
)

# used to hide ebooklib annoying warnings
Expand Down Expand Up @@ -55,11 +59,12 @@ async def _(client: Client, message: Message):
elif " " not in message.text and "epub" in file_type:
epub = epublib.read_epub(f)
await msg.edit("**__Grouping pages...__**")
for index, item in enumerate(epub.get_items(), start=1):
for i, item in enumerate(epub.get_items(), start=1):
if item.get_type() == ITEM_DOCUMENT:
h.feed(item.get_body_content().decode())
text += h.text
await msg.edit(f"**__{index} pages were grouped__**")
# pylint: disable=undefined-loop-variable
await msg.edit(f"**__{i} pages were grouped__**")
elif " " in message.text and file_type == "pdf":
pdf = PyPDF2.PdfReader(open(f, "rb"))
page_number = message.text.split(maxsplit=1)[1]
Expand Down Expand Up @@ -99,19 +104,19 @@ async def _(client: Client, message: Message):
caption="**__Started audiobook__**",
)
await msg.edit("**__Started audiobook__**")
if os.path.exists(book):
os.remove(book)
if os.path.exists(file_name):
os.remove(file_name)
except Exception as e:
logging.error(e)
await msg.edit(
f"**__Oops master, something wrong has happened.__** \n\n`Error: {e}`",
)
if message.chat.id in VOICE_CHATS:
await VOICE_CHATS[message.chat.id].stop()
await clear_queue(message.chat)
remove_queue(str(message.chat.id))
VOICE_CHATS.pop(message.chat.id)
if os.path.exists(book):
os.remove(book)
if os.path.exists(file_name):
os.remove(file_name)
elif os.path.exists(audiobook):
os.remove(audiobook)

Expand Down
84 changes: 49 additions & 35 deletions bot/plugins/commands/play_song.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
import uuid
import logging
from asyncio import sleep
from datetime import datetime
from pyrogram.types import Message
from pyrogram import filters, Client
from pyrogram.enums.chat_type import ChatType
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup

from bot.helpers.pkl import load_pkl
from bot.helpers.user_info import user_info
from bot import kreacher, on_call, VOICE_CHATS
from bot.helpers.progress import progress
from bot.helpers.yt import ytsearch, ytdl
from bot.helpers.queues import (
clear_queue,
add_or_create_queue,
get_queues,
get_last_position_in_queue,
remove_queue,
)

fotoplay = "https://telegra.ph/file/b6402152be44d90836339.jpg"
Expand All @@ -27,7 +30,6 @@

@kreacher.on_message(filters.regex(pattern="^[!?/]play_song"))
async def _(client: Client, message: Message):
QUEUE = await load_pkl(queues, "rb", "dict")
data = await user_info(message.from_user)
file_name = os.path.join(_cwd, f"../../downloads/songs/{str(uuid.uuid4())}.mp3")
if message.chat.type == ChatType.PRIVATE:
Expand Down Expand Up @@ -76,31 +78,43 @@ async def _(client: Client, message: Message):
name = search[0]
ref = search[1]
duration = search[2]
# thumb = await gen_thumb(videoid)
fmt = "best[height<=?720][width<=?1280]"
hm, url = await ytdl(fmt, ref)
if hm == 0:
await msg.edit(f"`{url}`")
_, url = await ytdl(fmt, ref)
if search == 0:
return await msg.edit(
"__Can't find song.\n\nTry searching with more specific title.__",
)
if message.chat.id in QUEUE:
# pos = await add_to_queue(message.chat, name, url, ref, "audio")
if message.chat.id in get_queues():
position = get_last_position_in_queue(str(message.chat.id)) + 1
add_or_create_queue(
message.chat.id,
from_user=str(message.from_user.id),
date=str(datetime.now()),
file=url,
type_of="song_yt",
position=position,
)
return await msg.edit(
f"__Added to queue at \n\n Title: [{name}]({url})\nDuration: {duration} Minutes\n Requested by:__ [{data['first_name']}]({data['mention']})",
# file=thumb,
f"__Added to queue at {position} \n\n Title: [{name}]({url})\nDuration: {duration} Minutes\n Requested by:__ [{data['first_name']}]({data['mention']})",
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("cʟᴏꜱᴇ", callback_data="cls")]]
),
)
if message.chat.id not in get_queues():
add_or_create_queue(
message.chat.id,
from_user=str(message.from_user.id),
date=str(datetime.now()),
is_playing=True,
file=url,
type_of="song_yt",
)
if VOICE_CHATS.get(message.chat.id) is None:
await msg.edit("🪄 **__Joining the voice chat...__**")
await on_call.start(message.chat.id)
VOICE_CHATS[message.chat.id] = on_call
await sleep(2)
await on_call.start_audio(url, repeat=False)
# await add_to_queue(message.chat, name, url, ref, "audio")
await msg.edit(
f"**__Started Streaming__**\n\n **Title**: [{name}]({url})\n **Duration:** {duration} **Minutes\n Requested by:** [{data['first_name']}]({data['mention']})",
# file=thumb,
Expand Down Expand Up @@ -141,31 +155,31 @@ async def _(client: Client, message: Message):
)
url_mention = f"https://t.me/c/{message.chat.id}/{message.reply_to_message.id}"
msg_mention = url_mention.replace("/c/-100", "/c/")
if message.chat.id in QUEUE:
# pos = add_to_queue(message.chat, name, url, ref, "audio")
await msg.delete()
return await kreacher.send_photo(
if message.chat.id in get_queues():
position = get_last_position_in_queue(str(message.chat.id)) + 1
add_or_create_queue(
message.chat.id,
caption=f"**__Added to queue at__** \n\n **Title:** [{name}]({msg_mention})\n **Requested by:** [{data['first_name']}]({data['mention']})",
photo=ngantri,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("\u23EA", callback_data="back"),
InlineKeyboardButton(
"\u23F8\uFE0F",
callback_data="pause_or_resume",
),
InlineKeyboardButton("\u23ED\uFE0F", callback_data="next"),
],
[
InlineKeyboardButton(
"\U0001f52e ᴄᴏɴᴛʀᴏʟs", callback_data="controls"
)
],
]
from_user=str(message.from_user.id),
date=str(datetime.now()),
file=media,
type_of="song_file",
position=position,
)
return await msg.edit(
f"__Added to queue at {position} \n\n Title: [{name}]({url})\nDuration: {duration} Minutes\n Requested by:__ [{data['first_name']}]({data['mention']})",
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("cʟᴏꜱᴇ", callback_data="cls")]]
),
)
if message.chat.id not in get_queues():
add_or_create_queue(
message.chat.id,
from_user=str(message.from_user.id),
date=str(datetime.now()),
is_playing=True,
file=media,
type_of="song_file",
)
if VOICE_CHATS.get(message.chat.id) is None:
await msg.edit("🪄 **__Joining the voice chat...__**")
await on_call.start(message.chat.id)
Expand Down Expand Up @@ -201,5 +215,5 @@ async def _(client: Client, message: Message):
)
if message.chat.id in VOICE_CHATS:
await VOICE_CHATS[message.chat.id].stop()
await clear_queue(message.chat)
remove_queue(str(message.chat.id))
VOICE_CHATS.pop(message.chat.id)
2 changes: 1 addition & 1 deletion bot/plugins/commands/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def _(client: Client, message: Message):
serie_name = os.path.join(
_cwd, f"../../downloads/series/{str(uuid.uuid4())}.mp4"
)
tmp = os.path.join(_cwd, f"../../tmp")
tmp = os.path.join(_cwd, "../../tmp")
series_channel = await assistant.get_chat(config.ES_SERIES_CHANNEL)
movies_channel = await assistant.get_chat(config.ES_MOVIES_CHANNEL)
async for serie in assistant.search_messages(
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ aiofiles==23.1.0
aiohttp==3.8.5
av==10.0.0
EbookLib==0.18
google-images-search==1.4.6
numpy==1.24.4
opencv-python-headless==4.8.0.74
pillow==10.0.0
Expand Down

0 comments on commit 4a191b0

Please sign in to comment.