Created
August 5, 2017 15:21
-
-
Save alfredfrancis/57b7e99cad1b48f6fcf0975dd947dde9 to your computer and use it in GitHub Desktop.
Python class for Wikipeida seaching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup as bs | |
class Wikipedia(): | |
def getTitles(self,query,num=4): | |
url="https://en.wikipedia.org/w/api.php?action=query&list=search&srnamespace=0&format=json&srlimit={}&srsearch={}&srprop=".format(num,query) | |
data = requests.get(url) | |
if data.json().get("query").get("searchinfo").get("totalhits") > 0: | |
titles = [ title.get("title") for title in data.json().get("query").get("search")] | |
else: | |
titles = False | |
return titles | |
def getSummary(self,title): | |
try: | |
data = requests.get("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&titles={}&exintro=1".format(title)) | |
raw_extract = data.json()["query"]["pages"].itervalues().next()["extract"] | |
soup = bs(raw_extract) | |
return soup.find('p').text | |
except: | |
return False | |
def process(self,message): | |
titles = self.getTitles(message) | |
if titles: | |
response = self.getSummary(titles[0]) | |
return {"id": titles[0], | |
"text_response": response, | |
"recommendations": titles | |
} | |
else: | |
return False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment