Skip to content

Commit e212613

Browse files
committed
Fix bot bugs
1 parent 2ee221b commit e212613

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

libgen_bot/bot.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from telegram import MessageEntity
44
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler, RegexHandler
55

6-
from handlers import start,search, fetch, search_with_out_command
6+
from handlers import start,search, fetch, search_or_fetch
77
from config import API_KEY
88

99

@@ -17,19 +17,19 @@
1717

1818
updater = Updater(API_KEY)
1919
dispatcher = updater.dispatcher
20-
message_handler = MessageHandler(Filters.text, search_with_out_command)
20+
message_handler = MessageHandler(Filters.text, search_or_fetch)
2121
start_handler = CommandHandler('start', start)
2222
search_handler = CommandHandler('search', search, pass_args=True)
2323
test_handler = CommandHandler('test', search, pass_args=True)
24-
fetch_handler = RegexHandler(re.compile('/get[0-9A-F]{32}'), fetch, pass_chat_data=True)
24+
#fetch_handler = RegexHandler(re.compile('/get[0-9A-F]{32}'), fetch, pass_chat_data=True)
2525

2626

2727
## Add handlers to dispatcher
2828
dispatcher.add_handler(message_handler)
2929
dispatcher.add_handler(start_handler)
3030
dispatcher.add_handler(search_handler)
3131
dispatcher.add_handler(test_handler)
32-
dispatcher.add_handler(fetch_handler)
32+
#dispatcher.add_handler(fetch_handler)
3333

3434
print('INFO: Starting bot... ')
3535
updater.start_polling()

libgen_bot/handlers.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ def start(bot, update):
1515
<b>Commands</b> \
1616
\n/search -keyword- -- Search books by title', parse_mode=ParseMode.HTML)
1717

18-
def fetch(bot, update, **kwargs):
18+
19+
def search_or_fetch(bot, update, **args):
20+
if update.effective_message.text.startswith('/get'): return fetch(bot, update, args)
21+
22+
search(bot, update, args)
23+
24+
def fetch(bot, update, args):
1925
md5 = update.effective_message.text[4:]
2026
url = api.fetch(md5)
2127
bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING)
@@ -26,11 +32,10 @@ def emojize_book_list(books):
2632

2733
def search(bot, update, args):
2834
bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING)
29-
results = api.search(' '.join(args))
35+
results = api.search(update.message.text)
3036
bot.send_message(chat_id=update.message.chat_id, text=(emojize_book_list(results) if results else 'Oops, no results found!!'), parse_mode=ParseMode.HTML)
3137

3238
def search_with_out_command(bot, update):
3339
bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING)
3440
results = api.search(update.message.text)
3541
bot.send_message(chat_id=update.message.chat_id, text=(emojize_book_list(results) if results else 'Oops, no results found!!'), parse_mode=ParseMode.HTML)
36-

libgen_bot/libgen/api.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33

44

55
BASE_URL = 'http://gen.lib.rus.ec/'
6-
BASE_DOWNLAOD_LINK = 'http://libgen.io/ads.php'
6+
BASE_DOWNLAOD_LINK = 'http://libgen.lc/ads.php'
77

88
def browse(term):
9-
url = BASE_URL + 'search.php/?req=' + (term.replace(' ', '+'))
9+
url = BASE_URL + 'search.php?req=' + (term.replace(' ', '+'))
1010
result = urllib.request.urlopen(url)
11-
assert result
12-
1311
return result
1412

1513
def get_dict_of_books(result):
1614
results = []
17-
1815
soup = BeautifulSoup(result, 'html.parser')
1916
soup = soup.find_all('table', attrs={'align': 'center'})[0]
2017
search_results = soup.find_all('tr') [1:]
@@ -46,7 +43,7 @@ def fetch(md5):
4643
result = urllib.request.urlopen(url)
4744
soup = BeautifulSoup(result, 'html.parser')
4845

49-
return soup.table.find_all('td')[2].a['href']
46+
return soup.table.find_all('a')[0]['href']
5047

5148
def search(term):
5249
return get_dict_of_books(browse(term))

0 commit comments

Comments
 (0)