Skip to content

Commit

Permalink
Added a CANCEL SEARCH button
Browse files Browse the repository at this point in the history
  • Loading branch information
francocruces committed Oct 28, 2017
1 parent da5c6b3 commit fdfc37d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
19 changes: 12 additions & 7 deletions AZScrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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,
Expand All @@ -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")
)]
Expand Down Expand Up @@ -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/', ''),
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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")
))
Expand Down
8 changes: 7 additions & 1 deletion Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
20 changes: 12 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fdfc37d

Please sign in to comment.