Skip to content

Instantly share code, notes, and snippets.

@ritiek

ritiek/wolfpedia.py

Last active Apr 28, 2019
Embed
What would you like to do?
Fetch information from wolfram and wikipedia using Flask API
import wolframalpha
import wikipedia
from wikipedia.exceptions import DisambiguationError
from flask import Flask
app = Flask(__name__)
client = wolframalpha.Client('T27H46-H3TH6GL545')
def wolframit(query):
response = client.query(query)
try:
pods = response.pods
except AttributeError:
return 'Nothing matching found'
result = ''
for pod in pods:
try:
result += '\n' + pod['@title'] + '\n'
except TypeError:
pass
try:
result += pod.text + '\n'
except TypeError:
pass
return result
@app.route('/wolfram/<query>')
def wolfram_respond(query):
return wolframit(query)
@app.route('/wikipedia/<query>')
def wiki_respond(query):
try:
return wikipedia.page(query).content
except DisambiguationError as e:
return 'Ambiguous query:\n\n' + '\n'.join(e.args[1])
if __name__ == '__main__':
app.run(port=8001, threaded=True, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.