From b4eca5092078c0fe029761cf1de3b34e767471cb Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Thu, 1 Oct 2015 22:57:54 +0700 Subject: [PATCH] Fix /leli on wiki articles that have no paragraphs --- tululbot.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tululbot.py b/tululbot.py index 0252065..d55a90f 100644 --- a/tululbot.py +++ b/tululbot.py @@ -30,14 +30,15 @@ def leli(term): """ def search_on_wikipedia(): - def has_result(page): - return 'Search results' not in page - def parse_content_text(page): return BeautifulSoup(page).find('div', id='mw-content-text') def parse_first_paragraph(page): - return parse_content_text(page).find('p').get_text() + return parse_content_text(page).find('p') + + def has_result(page): + return ('Search results' not in page and + parse_first_paragraph(page) is not None) def has_disambiguations(paragraph): return 'may refer to:' in paragraph @@ -62,8 +63,8 @@ def valid_link(tag): return None first_paragraph = parse_first_paragraph(page) - if not has_disambiguations(first_paragraph): - return first_paragraph + if not has_disambiguations(first_paragraph.get_text()): + return first_paragraph.get_text() disambiguation_url = parse_first_disambiguation_link(page) @@ -72,7 +73,7 @@ def valid_link(tag): return None disambiguated_page = response.text - return parse_first_paragraph(disambiguated_page) + return parse_first_paragraph(disambiguated_page).get_text() def search_on_google(): query_string = urlencode(dict(q=term))