Skip to content

Commit

Permalink
[updater, minor fixes]
Browse files Browse the repository at this point in the history
  • Loading branch information
subinps committed Aug 3, 2021
1 parent e528f5d commit efc08fe
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 53 deletions.
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ FROM debian:latest
RUN apt update && apt upgrade -y
RUN apt install git curl python3-pip ffmpeg -y
RUN pip3 install -U pip
COPY requirements.txt /requirements.txt
RUN cd /
RUN git clone https://github.com/subinps/MusicPlayer.git
RUN cd MusicPlayer
WORKDIR /MusicPlayer
RUN pip3 install -U -r requirements.txt
CMD python3 main.py
RUN mkdir /MusicPlayer
WORKDIR /MusicPlayer
COPY start.sh /start.sh
CMD ["/bin/bash", "/start.sh"]
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
worker: python3 main.py
worker: bash start.sh
1 change: 1 addition & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "Telegram Voice Chat Music Player Bot ",
"description": "Telegram Bot to Play Audio in Telegram Voice Chats",
"repository": "https://github.com/subinps/MusicPlayer",
"stack": "container",
"keywords": [
"telegram",
"bot",
Expand Down
3 changes: 3 additions & 0 deletions heroku.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build:
docker:
worker: Dockerfile
42 changes: 38 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
from pyrogram import Client, idle
import asyncio
from pyrogram import Client, idle, filters
import os
from config import Config
from utils import mp
from utils import mp, USERNAME, FFMPEG_PROCESSES
from pyrogram.raw import functions, types

import os
import sys
from threading import Thread
from signal import SIGINT
import subprocess
CHAT=Config.CHAT
bot = Client(
"Musicplayer",
Expand All @@ -38,9 +43,38 @@
async def main():
async with bot:
await mp.start_radio()
def stop_and_restart():
bot.stop()
os.system("git pull")
os.execl(sys.executable, sys.executable, *sys.argv)


bot.run(main())
bot.start()

@bot.on_message(filters.command(["restart", f"restart@{USERNAME}"]) & filters.user(Config.ADMINS) & (filters.chat(CHAT) | filters.private))
async def restart(client, message):
await message.reply_text("🔄 Updating and Restarting...")
await asyncio.sleep(3)
try:
await message.delete()
except:
pass
process = FFMPEG_PROCESSES.get(CHAT)
if process:
try:
process.send_signal(SIGINT)
except subprocess.TimeoutExpired:
process.kill()
except Exception as e:
print(e)
pass
FFMPEG_PROCESSES[CHAT] = ""
Thread(
target=stop_and_restart
).start()


bot.send(
functions.bots.SetBotCommands(
commands=[
Expand Down Expand Up @@ -126,7 +160,7 @@ async def main():
),
types.BotCommand(
command="restart",
description="Restart the bot"
description="Update and restart the bot"
)
]
)
Expand Down
8 changes: 5 additions & 3 deletions plugins/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@
**/volume** Change volume(0-200).
**/mute** Mute in VC.
**/unmute** Unmute in VC.
**/restart** Restarts the Bot.
**/restart**Update restarts the Bot.
"""



@Client.on_callback_query()
async def cb_handler(client: Client, query: CallbackQuery):
if query.from_user.id not in Config.ADMINS and query.data != "help":
admins = await mp.get_admins(Config.CHAT)
if query.from_user.id not in admins and query.data != "help":
await query.answer(
"Who the hell you are",
"😒 Played Joji.mp3",
show_alert=True
)
return
Expand Down
26 changes: 2 additions & 24 deletions plugins/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@
#SOFTWARE.
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from pyrogram import Client, filters
import signal
from utils import USERNAME, FFMPEG_PROCESSES, mp
from utils import USERNAME, mp
from config import Config
import os
import sys
import subprocess
import asyncio
from signal import SIGINT
U=USERNAME
CHAT=Config.CHAT
msg=Config.msg
Expand Down Expand Up @@ -66,7 +60,7 @@
**/volume** Change volume(0-200).
**/mute** Mute in VC.
**/unmute** Unmute in VC.
**/restart** Restarts the Bot.
**/restart** Update and restarts the Bot.
"""


Expand Down Expand Up @@ -114,19 +108,3 @@ async def show_help(client, message):
reply_markup=reply_markup
)
await mp.delete(message)
@Client.on_message(filters.command(["restart", f"restart@{U}"]) & filters.user(Config.ADMINS) & (filters.chat(CHAT) | filters.private))
async def restart(client, message):
await message.reply_text("🔄 Restarting...")
await mp.delete(message)
process = FFMPEG_PROCESSES.get(CHAT)
if process:
try:
process.send_signal(SIGINT)
except subprocess.TimeoutExpired:
process.kill()
except Exception as e:
print(e)
pass
FFMPEG_PROCESSES[CHAT] = ""
os.execl(sys.executable, sys.executable, *sys.argv)

26 changes: 19 additions & 7 deletions plugins/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ async def yplay(_, message: Message):
])
if EDIT_TITLE:
await mp.edit_title()
for track in playlist[:2]:
await mp.download_audio(track)
if message.chat.type == "private":
await message.reply_text(pl)
elif LOG_GROUP:
await mp.send_playlist()
elif not LOG_GROUP and message.chat.type == "supergroup":
k=await message.reply_text(pl)
await mp.delete(k)
for track in playlist[:2]:
await mp.download_audio(track)


if type=="youtube" or type=="query":
Expand All @@ -187,6 +187,8 @@ async def yplay(_, message: Message):
"Song not found.\nTry inline mode.."
)
print(str(e))
await mp.delete(message)
await mp.delete(msg)
return
else:
return
Expand All @@ -195,7 +197,17 @@ async def yplay(_, message: Message):
"nocheckcertificate": True
}
ydl = YoutubeDL(ydl_opts)
info = ydl.extract_info(url, False)
try:
info = ydl.extract_info(url, False)
except Exception as e:
print(e)
k=await msg.edit(
f"YouTube Download Error ❌\nError:- {e}"
)
print(str(e))
await mp.delete(message)
await mp.delete(k)
return
duration = round(info["duration"] / 60)
title= info["title"]
if int(duration) > DURATION_LIMIT:
Expand Down Expand Up @@ -250,15 +262,15 @@ async def yplay(_, message: Message):
])
if EDIT_TITLE:
await mp.edit_title()
for track in playlist[:2]:
await mp.download_audio(track)
if message.chat.type == "private":
await message.reply_text(pl)
if LOG_GROUP:
await mp.send_playlist()
elif not LOG_GROUP and message.chat.type == "supergroup":
k=await message.reply_text(pl)
await mp.delete(k)
for track in playlist[:2]:
await mp.download_audio(track)
await mp.delete(message)


Expand Down Expand Up @@ -346,13 +358,13 @@ async def deezer(_, message):
await message.reply_text(pl)
if EDIT_TITLE:
await mp.edit_title()
for track in playlist[:2]:
await mp.download_audio(track)
if LOG_GROUP:
await mp.send_playlist()
elif not LOG_GROUP and message.chat.type == "supergroup":
k=await message.reply_text(pl)
await mp.delete(k)
for track in playlist[:2]:
await mp.download_audio(track)
await mp.delete(message)


Expand Down
5 changes: 5 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
echo "Cloning Repo...."
git clone https://github.com/subinps/MusicPlayer.git /MusicPlayer
cd /MusicPlayer
echo "Starting Bot...."
python3 main.py
22 changes: 12 additions & 10 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@
"outtmpl": "downloads/%(id)s.%(ext)s",
}
ydl = YoutubeDL(ydl_opts)
def youtube(url: str) -> str:
info = ydl.extract_info(url, False)
duration = round(info["duration"] / 60)
try:
ydl.download([url])
except Exception as e:
print(e)
pass
return path.join("downloads", f"{info['id']}.{info['ext']}")

RADIO_TITLE=os.environ.get("RADIO_TITLE", " 🎸 Music 24/7 | Radio Mode")
if RADIO_TITLE=="NO":
Expand Down Expand Up @@ -150,7 +141,18 @@ async def download_audio(self, song):
if song[3] == "telegram":
original_file = await bot.download_media(f"{song[2]}")
elif song[3] == "youtube":
original_file = youtube(song[2])
url=song[2]
try:
info = ydl.extract_info(url, False)
ydl.download([url])
original_file=path.join("downloads", f"{info['id']}.{info['ext']}")
except Exception as e:
playlist.pop(1)
print(f"Unable to download due to {e} and skipped.")
if len(playlist) == 1:
return
await self.download_audio(playlist[1])
return
else:
original_file=wget.download(song[2])
ffmpeg.input(original_file).output(
Expand Down

0 comments on commit efc08fe

Please sign in to comment.