-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbot.py
85 lines (69 loc) · 2.53 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import io
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from pyromod import listen
from PIL import Image
from music_tag import load_file
BOT_TOKEN = os.environ.get("BOT_TOKEN")
API_ID = os.environ.get("API_ID")
API_HASH = os.environ.get("API_HASH")
Bot = Client(
"MusicEditorBot",
bot_token = BOT_TOKEN,
api_id = API_ID,
api_hash = API_HASH
)
START_TXT = """
Hi {}, I am Music Editor Bot.
I can change the music tags and artwork.
Send a music to get started.
"""
START_BTN = InlineKeyboardMarkup(
[[
InlineKeyboardButton('Source Code', url='https://github.com/samadii/MusicEditorBot'),
]]
)
@Bot.on_message(filters.command(["start"]))
async def start(bot, update):
text = START_TXT.format(update.from_user.mention)
reply_markup = START_BTN
await update.reply_text(
text=text,
disable_web_page_preview=True,
reply_markup=reply_markup
)
@Bot.on_message(filters.private & filters.audio)
async def tag(bot, m):
mes = await m.reply("`Downloading...`")
await m.download(f"temp/{m.audio.file_name}.mp3")
music = load_file(f"temp/{m.audio.file_name}.mp3")
try:
artwork = music['artwork']
image_data = artwork.value.data
img = Image.open(io.BytesIO(image_data))
img.save("temp/artwork.jpg")
except ValueError:
image_data = None
await mes.delete()
fname = await bot.ask(m.chat.id,'`Send the Filename`', filters=filters.text)
title = await bot.ask(m.chat.id,'`Send the Title name`', filters=filters.text)
artist = await bot.ask(m.chat.id,'`Send the Artist(s) name`', filters=filters.text)
answer = await bot.ask(m.chat.id,'`Send the Artwork or` /skip', filters=filters.photo | filters.text)
music.remove_tag('artist')
music.remove_tag('title')
music['artist'] = artist.text
music['title'] = title.text
if answer.photo:
await bot.download_media(message=answer.photo, file_name="temp/artwork.jpg")
music.remove_tag('artwork')
with open('temp/artwork.jpg', 'rb') as img_in:
music['artwork'] = img_in.read()
music.save()
try:
await bot.send_audio(chat_id=m.chat.id, file_name=fname.text, performer=artist.text, title=title.text, duration=m.audio.duration, audio=f"temp/{m.audio.file_name}.mp3", thumb='temp/artwork.jpg' if answer.photo or image_data else None)
except Exception as e:
print(e)
return
os.remove(f"temp/{m.audio.file_name}.mp3")
Bot.run()