This commit is contained in:
deathbybandaid 2019-10-04 09:38:42 -04:00
parent f8d0e8ac14
commit f23df8a747

View File

@ -16,6 +16,7 @@ from lxml import etree
from lxml import html from lxml import html
api_url = 'https://httpstatuses.com/' api_url = 'https://httpstatuses.com/'
basic_xpath = "/html/body/article/h1[1]/text()"
@SpiceBot.prerun('module') @SpiceBot.prerun('module')
@ -52,8 +53,11 @@ def fetch_result(query):
tree = html.fromstring(r.content) tree = html.fromstring(r.content)
title = tree.xpath((basic_xpath))
return str(title)
try: try:
title = tree.xpath(('/html/body/article/p[1]')[0]) title = tree.xpath((basic_xpath))
# if isinstance(title, list): # if isinstance(title, list):
# title = title[0] # title = title[0]
# title = str(title) # title = str(title)
@ -63,15 +67,6 @@ def fetch_result(query):
except Exception as e: except Exception as e:
title = None title = None
if title: if title:
return title return str(title)
else: else:
return "An Error Occured" return "An Error Occured"
return
page = etree.HTML(r.content)
title = bleach.clean(etree.tostring(page.xpath('/html/body/article/h1[1]')[0]), tags=[], strip=True)
summary = bleach.clean(
re.sub('<a href="#ref-\d+">.*?<\/a>', '', etree.tostring(page.xpath('/html/body/article/p[1]')[0])), tags=[],
strip=True)
return "{title}: {summary} — {link}".format(title=title, summary=summary, link=url)