diff --git a/AZScrapper.py b/AZScrapper.py index 81a59f8..99d158f 100644 --- a/AZScrapper.py +++ b/AZScrapper.py @@ -14,7 +14,8 @@ from telepot.namedtuple import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, \ InlineKeyboardMarkup -from Config import MAX_RESULTS, NO_RESULTS_ALERT +from Config import MAX_RESULTS, NO_RESULTS_ALERT, CANCEL_DATA_STRING, NO_RESULTS_TEXT, CANCEL_BUTTON_TEXT, \ + TITLE_ARTIST_SEPARATOR __author__ = "Franco Cruces Ayala" @@ -28,6 +29,10 @@ def get_lyrics_as_inline_keyboard(query): """ buttons = get_inline_keyboard_buttons(query) if len(buttons) > 0: + buttons.append([InlineKeyboardButton( + text=CANCEL_BUTTON_TEXT, + callback_data=CANCEL_DATA_STRING, + )]) return [InlineQueryResultArticle( id=query, title="Search for " + query, @@ -40,7 +45,7 @@ def get_lyrics_as_inline_keyboard(query): # TODO: Add a cancel button. else: return [InlineQueryResultArticle( - id=0, title="NO RESULTS", input_message_content=InputTextMessageContent( + id=0, title=NO_RESULTS_TEXT, input_message_content=InputTextMessageContent( message_text=NO_RESULTS_ALERT, parse_mode="Markdown") )] @@ -88,7 +93,7 @@ def get_inline_keyboard_buttons(query): buttons = [] search_results = get_search_result(query) for song in search_results: - title = song['title'] + " by " + song['artist'] + title = song['title'] + TITLE_ARTIST_SEPARATOR + song['artist'] buttons.append([InlineKeyboardButton( text=title, callback_data=song['url'].replace('https://www.azlyrics.com/lyrics/', ''), @@ -108,10 +113,10 @@ def get_lyric_body(url): body = BeautifulSoup(page.read(), 'lxml').html.body.find_all( "div", class_="col-xs-12 col-lg-8 text-center")[0].find_all("div") lyrics = body[6].get_text() - artist = body[4].text.replace(" Lyrics", "").replace("\n","") + artist = body[4].text.replace(" Lyrics", "").replace("\n", "") title = body[3].text.replace(" lyrics", "").replace('"', '') print("Done: " + url) - return "*" + title + " by " + artist + "*" + lyrics + return "*" + title + TITLE_ARTIST_SEPARATOR + artist + "*" + lyrics def get_lyric_body_from_id(an_id): @@ -147,7 +152,7 @@ def get_lyrics(query): result = [] search_results = get_search_result(query) for song in search_results: - title = song['title'] + " by " + song['artist'] + title = song['title'] + TITLE_ARTIST_SEPARATOR + song['artist'] result.append(InlineQueryResultArticle( id=song['url'].replace('https://www.azlyrics.com/lyrics/', ''), title=title, @@ -158,7 +163,7 @@ def get_lyrics(query): ))) if len(result) == 0: result.append(InlineQueryResultArticle( - id=0, title="NO RESULTS", input_message_content=InputTextMessageContent( + id=0, title=NO_RESULTS_TEXT, input_message_content=InputTextMessageContent( message_text=NO_RESULTS_ALERT, parse_mode="Markdown") )) diff --git a/Config.py b/Config.py index d7be1d2..31f00e8 100644 --- a/Config.py +++ b/Config.py @@ -3,8 +3,14 @@ """ MAX_RETRIES = 20 -MAX_RESULTS = 7 # Max amount of results to show. Set to -1 for no limit. +MAX_RESULTS = 6 # Max amount of results to show. Set to -1 for no limit. MAX_MESSAGE_SIZE = 4096 # A message cannot contain more characters MESSAGE_TOO_LONG_ALERT = "_Message too long. Sending through private message.\n Please make sure you have started a " \ "conversation with this bot first via PM @EasyLyricsBot._" +NO_RESULTS_TEXT = "NO RESULTS" NO_RESULTS_ALERT = "_No results :c_" +CANCEL_BUTTON_TEXT = "CANCEL SEARCH" +CANCEL_DATA_STRING = "~~CANCEL~~" +SEARCH_CANCELLED_ALERT = "_The search was cancelled_" + +TITLE_ARTIST_SEPARATOR = " by " \ No newline at end of file diff --git a/main.py b/main.py index 2d1f726..2370143 100644 --- a/main.py +++ b/main.py @@ -8,7 +8,7 @@ from telepot.aio.delegate import per_inline_from_id, create_open, pave_event_space, intercept_callback_query_origin from telepot.aio.helper import InlineUserHandler, AnswererMixin, InterceptCallbackQueryMixin from telepot.aio.loop import MessageLoop -from Config import MAX_MESSAGE_SIZE, MESSAGE_TOO_LONG_ALERT +from Config import MAX_MESSAGE_SIZE, MESSAGE_TOO_LONG_ALERT, CANCEL_DATA_STRING, SEARCH_CANCELLED_ALERT from AZScrapper import get_lyrics_as_inline_keyboard, get_lyric_body_from_id from __TOKEN__ import TOKEN # Replace with your own token. Provided by BotFather @@ -52,14 +52,18 @@ def on_chosen_inline_result(self, msg): async def on_callback_query(self, msg): print(msg) in_id = msg['inline_message_id'] - lyrics = get_lyric_body_from_id(msg['data']) - if len(lyrics) <= MAX_MESSAGE_SIZE: - await self.bot.editMessageText(str(in_id), lyrics, parse_mode="Markdown") + data = msg['data'] + if data != CANCEL_DATA_STRING: + lyrics = get_lyric_body_from_id(data) + if len(lyrics) <= MAX_MESSAGE_SIZE: + await self.bot.editMessageText(str(in_id), lyrics, parse_mode="Markdown") + else: + await self.bot.editMessageText(str(in_id), MESSAGE_TOO_LONG_ALERT, parse_mode="Markdown") + while lyrics != "": + await self.bot.sendMessage(msg['from']['id'], lyrics[:MAX_MESSAGE_SIZE]) + lyrics = lyrics[MAX_MESSAGE_SIZE:] else: - await self.bot.editMessageText(str(in_id), MESSAGE_TOO_LONG_ALERT, parse_mode="Markdown") - while lyrics != "": - await self.bot.sendMessage(msg['from']['id'], lyrics[:MAX_MESSAGE_SIZE]) - lyrics = lyrics[MAX_MESSAGE_SIZE:] + await self.bot.editMessageText(str(in_id), SEARCH_CANCELLED_ALERT, parse_mode="Markdown") # ASYNC MAIN