-
Notifications
You must be signed in to change notification settings - Fork 0
/
etymbot.py
55 lines (43 loc) · 1.61 KB
/
etymbot.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
from telegram.ext import Updater
from telegram.ext import InlineQueryHandler
from bs4 import BeautifulSoup
import requests
import logging
from telegram import InlineQueryResultArticle, InputTextMessageContent
import os
logger = logging.getLogger(__name__)
updater = Updater(token=os.environ.get("BOT_TOKEN"))
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
SEARCH_URL = 'https://www.etymonline.com/search?q='
def inline_etym(update, context):
query = update.inline_query.query
try:
logger.info(f"Query: {query}")
results = list()
text = first_result_return(query)
content = f"{query}:\n{text}"
results.append(InlineQueryResultArticle(
id=query,
title=query,
input_message_content=InputTextMessageContent(content))
)
context.bot.answer_inline_query(update.inline_query.id, results)
except AttributeError:
pass
if __name__ == '__main__':
inline_etym_handler = InlineQueryHandler(inline_etym)
dispatcher.add_handler(inline_etym_handler)
updater.start_polling()
'''
functions from https://github.com/tetrismegistus/etym
'''
def soup_search(search_term):
# post request to search URL, return beautiful soup parsed object
url = SEARCH_URL + search_term
response = requests.get(url)
return BeautifulSoup(response.text, 'html.parser')
def first_result_return(word):
search_page = soup_search(word)
return search_page.find("section", class_="word__defination--2q7ZH undefined").get_text()