-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
61 lines (49 loc) · 2.38 KB
/
bot.py
File metadata and controls
61 lines (49 loc) · 2.38 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
import json
import os
import telebot
from playlist_creator import PlaylistMaker
from config import Config
TG_TOKEN = Config.TG_TOKEN
VK_TOKEN = Config.VK_TOKEN
bot = telebot.TeleBot(TG_TOKEN)
HELP_TEXT = "Чтобы загрузить весь свой плейлист, напиши мне '/download VK_ID', где VK_ID - id твоей страницы " \
"ВКонтакте. Аудиозаписи должны быть открыты."
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, HELP_TEXT)
print(message, end='\n\n')
@bot.message_handler(commands=['help'])
def show_help(message):
bot.send_message(message.chat.id, HELP_TEXT)
print(message, end='\n\n')
@bot.message_handler(commands=['download'])
def load_music(message):
print(message, end='\n\n')
if message.chat.type != 'private':
bot.send_message(message.chat.id, 'Доступно только в приватном чате')
return
if len(message.text.split(' ')) != 2:
bot.send_message(message.chat.id, 'Использование: /download VK_ID')
else:
try:
id_ = int(message.text.split(' ')[1])
except ValueError:
bot.send_message(message.chat.id, 'id может быть только числом')
return
bot.send_message(message.chat.id, 'Загрузка плейлиста начата')
pl_maker = PlaylistMaker()
plist = pl_maker.create_playlist_file(id_)
with open(plist) as json_file_playlist:
playlist = json.loads(json_file_playlist.read())
for song in playlist:
if song['src'] and not song['is_blocked']:
try:
# Sync loading here to save order of music in bot dialog
bot.send_audio(message.chat.id, song['src'], caption=f"{song['author']} - {song['title']}",
disable_notification=True)
except telebot.apihelper.ApiException:
bot.send_message(message.chat.id, f"Не удалось загрузить {song['author']} - {song['title']}")
continue
bot.send_message(message.chat.id, 'Загрузка плейлиста окончена')
if __name__ == '__main__':
bot.polling(none_stop=True, interval=5)