-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnisanyanbot.py
98 lines (77 loc) · 3.36 KB
/
nisanyanbot.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from typing import List
from telegram.ext import Updater
from telegram.ext import InlineQueryHandler
from bs4 import BeautifulSoup
import requests
import logging
from telegram import InlineQueryResultArticle, InputTextMessageContent
updater = Updater(token='TOKEN')
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
SEARCH_URL = 'https://www.nisanyansozluk.com/?k='
NOT_FOUND_DESCRIPTION = 'Nişanyan Sözlük - Çağdaş Türkçenin Etimolojisi'
def inline_nisanyan(update, context):
query = update.inline_query.query
results = get_results(word=query)
context.bot.answer_inline_query(update.inline_query.id, results)
inline_nisanyan_handler = InlineQueryHandler(inline_nisanyan)
dispatcher.add_handler(inline_nisanyan_handler)
updater.start_polling()
################################################################
# HELPERS
################################################################
def get_results(word: str) -> List[InlineQueryResultArticle]:
results = list()
first_result = find_description(word)
if first_result == NOT_FOUND_DESCRIPTION:
# Let's try whether the word has homonyms(eşsesli in Turkish)
first_result_word = f"{word}1"
homonymous_result = find_description(first_result_word)
if homonymous_result == NOT_FOUND_DESCRIPTION:
# Word does not exist in Nisanyan
results.append(return_not_found_word(word))
return results
results.append(return_valid_result(first_result_word, homonymous_result))
results = try_result_homonyms(word, results)
else:
results.append(return_valid_result(word, first_result))
return results
def find_description(word: str) -> str:
search_page = soup_search(word)
metas = search_page.find_all('meta')
return [meta.attrs['content'] for meta in metas if 'property' in meta.attrs and
meta.attrs['property'] == 'og:description'][0].encode('latin-1').decode('utf-8')
def soup_search(search_term: str) -> BeautifulSoup:
url = SEARCH_URL + search_term
response = requests.get(url)
return BeautifulSoup(response.text, 'html.parser')
def return_valid_result(word: str, result: str) -> InlineQueryResultArticle:
return InlineQueryResultArticle(
id=word,
title=f"{word} - {result[:100]}",
input_message_content=InputTextMessageContent(word + ': \n' + result)
)
def return_not_found_word(word: str) -> InlineQueryResultArticle:
return InlineQueryResultArticle(
id=word,
title=f"\"{word}\" nisanyan\'da bulunamadi. Baska kelimeler var ama.",
input_message_content=InputTextMessageContent(f"{word}:\nYok kardesim yok.")
)
def try_result_homonyms(word: str, results: List[InlineQueryResultArticle]) -> \
List[InlineQueryResultArticle]:
"""
Try finding all homonyms of the word in Nisanyan
:param word: the word to search
:param results: List of InlineQueryResultArticle
:return: result list of InlineQueryResultArticle after homonyms search
"""
i = 2
while True:
query = f"{word}{i}"
result = find_description(query)
if result == NOT_FOUND_DESCRIPTION:
break
results.append(return_valid_result(query, result))
i += 1
return results