-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.py
285 lines (202 loc) · 7.64 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python
import json
import logging
import requests
import socket
import traceback
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, InputMediaAudio, InputFile, File
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext, MessageHandler, Filters
from telegram.ext.filters import Filters
from vk_funcs import ep_vk_search, ep_vk_audio_by_ids, ep_vk_finish, download_audio, download_cover, renew_connection
from shazam_funcs import shazam_recognize
from auths import AUTHS
socket._GLOBAL_DEFAULT_TIMEOUT = 100
n_results_per_page = 10
n_results_per_page = 6
updater = None
SAVED = {}
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
global DEBUG
DEBUG = {'msgs':[]}
def log(*args):
logger.info(*args)
def info(*args):
logger.info(*args)
def debug(*args):
logger.debug(*args)
def warning(*args):
logger.warning(*args)
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Здравствуй! Ищу и качаю музыку с вконтакте, пиши что надо найти')
def renew(update: Update, context: CallbackContext) -> None:
try:
renew_connection()
update.message.reply_text('VK connection renewed')
except:
traceback.print_exc()
try:
update.message.reply_text('Error..')
except:
pass
MAX_MESSAGE_LEN = 500
def send_exc(message, s, print_also=True):
if print_also:
print(s)
try:
message.reply_text(s[:MAX_MESSAGE_LEN])
if len(s)>MAX_MESSAGE_LEN:
send_exc(message,s[MAX_MESSAGE_LEN:], print_also=False)
except:
traceback.print_exc()
def msg_add_text(msg, s):
log(s)
msg.text = msg.text + s
msg.edit_text(msg.text)
def button(update: Update, context: CallbackContext) -> None:
global DEBUG
DEBUG['context'] = context
DEBUG['update'] = update
query = update.callback_query
# CallbackQueries need to be answered, even if no notification to the user is needed
# Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
query.answer()
msg = query.message.reply_text("думаю..")
log_fun = lambda s: msg_add_text(msg,s)
ids = query.data
if(ids[0]=='{'):
# page stuff!
ts = ids[1:].split('|')
r = {'q':ts[0], 'page':int(ts[1])}
info('looking at page %i for %s'
% (r['page'],r['q']))
log_fun('ищу..')
page_search(r['q'], query.message, page=int(r['page']), edit=True)
msg.delete()
else:
global SAVED
if(ids not in SAVED):
info('%s not in SAVED! getting from vk..' % ids)
msg_add_text(msg, 'смотрю данные..')
r = ep_vk_audio_by_ids(ids)
SAVED[ids] = r
r = SAVED[ids]
DEBUG['msg'] = msg
info('Getting %s : %s' % (ids, r['title_str']))
print(r)
try:
content = download_audio(r['url'], log_fun)
thumb = None
if r['track_covers']:
thumb = download_cover(r['track_covers'], log_fun)
else:
thumb = None
log_fun('отправляю..')
msg2 = query.message.reply_audio(
content,
duration=r['duration'],
title=r['title'],
performer=r['artist'],
thumb=thumb,
)
DEBUG['reply_audio'] = msg2
info('Got %s : %s' % (ids, r['title_str']))
msg.delete()
except Exception as e:
log_fun('ошибка')
warning(str(r))
warning(traceback.format_exc())
# send_exc(query.message, traceback.format_exc())
try:
query.message.reply_audio(
r['url'],
title=r['title'],
performer=r['artist'],
)
except:
traceback.print_exc()
def help_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text("Use /start to test this bot.")
def page_button(q, page, delta_n):
return InlineKeyboardButton(
"<" if delta_n < 0 else ">",
callback_data='{'+q+'|'+str(page + delta_n) # json.dumps({'q':q,'page':page+1})
)
def prepare_keyboard(q, R, page):
global SAVED
keyboard = [
[InlineKeyboardButton(
r['title_str'],
callback_data=r['ids_string']
)]
for j,r in enumerate(R)
]
prev_page_btn = page_button(q, page, -1)
next_page_btn = page_button(q, page, 1)
page_btns = []
if page > 0:
page_btns.append(prev_page_btn)
if len(R) >= n_results_per_page:
page_btns.append(next_page_btn)
if len(page_btns)>0:
keyboard.append(page_btns)
return keyboard
def audio_or_voice(update: Update, context: CallbackContext) -> None:
print(update.message)
DEBUG['msgs'].append(update.message)
file_data = update.message.audio or update.message.voice
file = update.message.bot.get_file(file_data.file_id)
content = file.download_as_bytearray()
print('recognizing file..')
rec = shazam_recognize(content)
print(rec)
DEBUG['rec'] = rec
if len(rec['matches'])==0:
update.message.reply_text('Nothing found, sorry..')
else:
page_search(
rec['track']['title']+' - '+rec['track']['subtitle'],
update.message
)
def message(update: Update, context: CallbackContext) -> None:
page_search(update.message.text, update.message)
def page_search(s, message, page=0, edit=False):
info('Looking for ' + s)
R = ep_vk_search(s, n_results_per_page=n_results_per_page, page=page)
info('Found : %i results' % len(R))
if len(R)==0:
info('nothing..')
message.reply_text('Ничего не нашлось..')
else:
global SAVED
for r in R:
SAVED[r['callback_data']] = {k:r[k] for k in r}
keyboard = prepare_keyboard(s, R, page)
reply_markup = InlineKeyboardMarkup(keyboard)
if edit:
message.edit_reply_markup(reply_markup)
else:
message.reply_text('Вот что нашёл:', reply_markup=reply_markup)
def main():
global updater
# Create the Updater and pass it your bot's token.
updater = Updater(AUTHS[2])
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('renew', renew))
updater.dispatcher.add_handler(MessageHandler(Filters.text, message))
updater.dispatcher.add_handler(MessageHandler(Filters.audio | Filters.voice, audio_or_voice))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(CommandHandler('help', help_command))
me = updater.bot.getMe()
print(f'I am:\nname:{me.first_name} username:{me.username} id:{me.id}\nSecurity token:\n{AUTHS[2]}')
# Start the Bot
updater.start_polling()
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()
ep_vk_finish()
if __name__ == '__main__':
print('This is ep_vk_music__bot')
main()