from .__init__ import *
import re
PATTERN = r'(.+)?(.+spotify.+)\/(track|album|playlist)\/([A-Z0-9a-z]+)(.*)'
@bot.on_message(filters.command('search') | filters.regex(PATTERN))
async def SearchFilter(client:Client,msg:Message):
url = re.search(PATTERN, msg.text)
if url:
_type, _id = url[3], url[4]
if _type == 'track':
text, image, markup = track(_id)
elif _type == 'album':
text, image, markup = album(_id)
elif _type == 'playlist':
text, image, markup = playlist(_id)
await msg.reply_photo(image, caption=text, reply_markup=markup)
else:
if msg.text.startswith('/search'):
query = ' '.join(msg.text.split()[1:]) if len(msg.text) > 1 else ''
if query: await SearchHandler(client, msg, query)
else: return await msg.reply('ꜰᴏʀᴍᴀᴛ - /search song-name
')
async def SearchHandler(client, msg, query):
search = SpotifySearch(query)
results = await search.search()
if not results:
return await msg.reply(f'`ɴᴏ ᴍᴀᴛᴄʜ ꜰᴏᴜɴᴅ ꜰᴏʀ Qᴜᴇʀʏ - "{query}"`')
await Redis.save(f'search|{msg.from_user.id}', results)
type_ = 'track'
sep = 4; index = 0
items = results[type_]
if len(items) > sep:
ITEMS = [items[i:i+sep] for i in range(0,len(items),sep)]
items = ITEMS[index]
markup = [ [InlineKeyboardButton(f'{item["name"]}', callback_data=f'show|{type_}|{item["_id"]}')] for item in items ]
if len(results[type_]) > 1:
markup += [ [InlineKeyboardButton('🔙 Prev', callback_data=f'prev|{type_}|0'), InlineKeyboardButton('Next 🔜', callback_data=f'next|{type_}|0')] ]
markup += [[
InlineKeyboardButton(('☑️ Tracks' if type_ == 'track' else 'Tracks'), callback_data='track'),
InlineKeyboardButton(('☑️ Albums' if type_ == 'album' else 'Albums'), callback_data='album'),
InlineKeyboardButton(('☑️ Playlists' if type_ == 'playlist' else 'Playlists'), callback_data='playlist')
]]
text = f"Search Results for `\"{query}\"` :-"
await msg.reply_animation('https://cdn.dribbble.com/users/441326/screenshots/3165191/spotify-gif---oliver-keane.gif', caption=text, reply_markup=InlineKeyboardMarkup(markup))
@bot.on_callback_query(filters.create(lambda _,__,query: query.data.startswith(('prev', 'next'))))
async def BackCallback(client:Client, query: CallbackQuery):
results = await Redis.get(f'search|{query.from_user.id}')
index = int(query.data.split('|')[2])
move = query.data.split('|')[0]
type_ = query.data.split('|')[1]
sep = 4
items = results[type_]
if len(items) > sep:
ITEMS = [items[i:i+sep] for i in range(0,len(items),sep)]
if move == 'next':
if index < len(ITEMS)-1: index += 1
else: index = 0
if move == 'prev':
if index-1 != -1: index -= 1
else: index = len(ITEMS) - 1
items = ITEMS[index]
markup = [ [InlineKeyboardButton(f'{item["name"]}', callback_data=f'show|{type_}|{item["_id"]}')] for item in items ]
markup += [ [InlineKeyboardButton('🔙 Prev', callback_data=f'prev|{type_}|{index}'), InlineKeyboardButton('Next 🔜', callback_data=f'next|{type_}|{index}')] ]
markup += [[
InlineKeyboardButton(('☑️ Tracks' if type_ == 'track' else 'Tracks'), callback_data='track'),
InlineKeyboardButton(('☑️ Albums' if type_ == 'album' else 'Albums'), callback_data='album'),
InlineKeyboardButton(('☑️ Playlists' if type_ == 'playlist' else 'Playlists'), callback_data='playlist')
]]
await query.edit_message_reply_markup(InlineKeyboardMarkup(markup))
@bot.on_callback_query(filters.create(lambda _,__,query: query.data.startswith(('track', 'album', 'playlist'))))
async def BackCallback(client:Client, query: CallbackQuery):
results = await Redis.get(f'search|{query.from_user.id}')
type_ = query.data.split('|')[0]
sep = 4; index = 0
items = results[type_]
if len(items) > sep:
ITEMS = [items[i:i+sep] for i in range(0,len(items),sep)]
items = ITEMS[index]
markup = [ [InlineKeyboardButton(f'{item["name"]}', callback_data=f'show|{type_}|{item["_id"]}')] for item in items ]
if len(results[type_]) > 1:
markup += [ [InlineKeyboardButton('🔙 Prev', callback_data=f'prev|{type_}|0'), InlineKeyboardButton('Next 🔜', callback_data=f'next|{type_}|0')] ]
markup += [[
InlineKeyboardButton(('☑️ Tracks' if type_ == 'track' else 'Tracks'), callback_data='track'),
InlineKeyboardButton(('☑️ Albums' if type_ == 'album' else 'Albums'), callback_data='album'),
InlineKeyboardButton(('☑️ Playlists' if type_ == 'playlist' else 'Playlists'), callback_data='playlist')
]]
await query.edit_message_reply_markup(InlineKeyboardMarkup(markup))