Better wiki redirect

This commit is contained in:
t3xhno 2024-02-06 11:39:10 +01:00
parent a256bc277d
commit 161abdf32e
2 changed files with 9 additions and 5 deletions

View File

@ -6,7 +6,7 @@ def processmsg(msg, rcpt):
return msg.replace("youtube.com", "iv.datura.network") return msg.replace("youtube.com", "iv.datura.network")
elif msg.startswith("!wiki"): elif msg.startswith("!wiki"):
cmd, query = msg.split(" ", 1) 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: elif "good bot" in msg:
return "^_^" return "^_^"

View File

@ -7,9 +7,13 @@ def query_external_website(base_url, query):
page = requests.get(base_url + quote(query)) page = requests.get(base_url + quote(query))
soup = BeautifulSoup(page.content, "html.parser") soup = BeautifulSoup(page.content, "html.parser")
title = soup.find(id="firstHeading").text 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: 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) return "\nTITLE:\n" + title + "\n\nCONTENT:\n" + content.text + "\n\nFULL LINK:\n" + base_url + quote(query)
except: except Exception as e:
return "Can't parse search result :(" return e