-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (65 loc) · 2.35 KB
/
main.py
File metadata and controls
86 lines (65 loc) · 2.35 KB
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
86
from telegram.ext import Updater, MessageHandler, Filters
import json
import logging
import mutagen
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
with open("config.json", "r") as read_file:
config = json.load(read_file)
updater = Updater(config["TOKEN"])
dispatcher = updater.dispatcher
class Audio:
def __init__(self, bot, update):
self.bot = bot
self.update = update
self.chat_id = update.effective_message.chat_id
self.message_id = update.effective_message.message_id
self.audio = self.download_audio()
def download_audio(self):
audio = self.update.effective_message.effective_attachment
file_id = audio.file_id
new_file = self.bot.get_file(file_id)
logging.log(logging.INFO, "Downloading file")
self.bot.edit_message_caption(chat_id=self.chat_id, message_id=self.message_id, caption="downloading...")
new_file.download('file.mp3')
logging.log(logging.INFO, "File downloaded")
return mutagen.File('file.mp3')
def set_new_caption(self):
title = ""
artist = ""
album = ""
genre = ""
try:
title = self.audio.tags["TIT2"]
except:
pass
try:
artist = self.audio.tags["TPE1"]
except:
pass
try:
album = self.audio.tags["TALB"]
except:
pass
try:
genre = self.audio.tags["TCON"]
except:
pass
new_caption = '''✏️ Title: {0}
👤 Artist: {1}
💽 Album: {2}
🎼 Genre: {3}'''.format(title, artist, album, genre)
self.bot.edit_message_caption(chat_id=self.chat_id, message_id=self.message_id, caption=new_caption)
logging.log(logging.INFO, "Caption changed")
def change_caption(bot, update):
logging.log(logging.INFO, "Changing caption")
chat_id = update.effective_message.chat_id
message_id = update.effective_message.message_id
audio = Audio(bot, update)
audio.set_new_caption()
handler = MessageHandler(Filters.audio, change_caption, channel_post_updates=True, message_updates=False)
# handler = CommandHandler("ok", echo)
dispatcher.add_handler(handler=handler)
POLLING_INTERVAL = 0.2
updater.start_polling(poll_interval=POLLING_INTERVAL)
updater.idle()