forked from Decentrala/chatbot
Better wiki redirect
This commit is contained in:
parent
a256bc277d
commit
161abdf32e
@ -6,7 +6,7 @@ def processmsg(msg, rcpt):
|
||||
return msg.replace("youtube.com", "iv.datura.network")
|
||||
elif msg.startswith("!wiki"):
|
||||
cmd, query = msg.split(" ", 1)
|
||||
return sf.query_external_website("https://en.wikipedia.org/wiki/", query)
|
||||
return sf.query_external_website("https://en.wikipedia.org", "/wiki/" + query)
|
||||
elif "good bot" in msg:
|
||||
return "^_^"
|
||||
|
||||
|
@ -7,9 +7,13 @@ def query_external_website(base_url, query):
|
||||
page = requests.get(base_url + quote(query))
|
||||
soup = BeautifulSoup(page.content, "html.parser")
|
||||
title = soup.find(id="firstHeading").text
|
||||
content = next((paragraph for paragraph in soup.find(id="mw-content-text").select("p") if not paragraph.has_attr("class")), None)
|
||||
mainContentElement = soup.find(id="mw-content-text")
|
||||
if "This page is a redirect" in mainContentElement.text:
|
||||
redirectLink = mainContentElement.find(class_="redirectMsg").find_all("a")[0]["href"]
|
||||
return query_external_website(base_url, redirectLink)
|
||||
content = next((paragraph for paragraph in mainContentElement.select("p") if not paragraph.has_attr("class")), None)
|
||||
if content == None:
|
||||
raise Exception("Can't parse")
|
||||
raise Exception("Can't parse search result :(")
|
||||
return "\nTITLE:\n" + title + "\n\nCONTENT:\n" + content.text + "\n\nFULL LINK:\n" + base_url + quote(query)
|
||||
except:
|
||||
return "Can't parse search result :("
|
||||
except Exception as e:
|
||||
return e
|
||||
|
Loading…
Reference in New Issue
Block a user