Skip to content

Instantly share code, notes, and snippets.

@ritiek
Last active April 28, 2019 09:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritiek/34c8684faaef624ed998308ecf1abf8a to your computer and use it in GitHub Desktop.
Save ritiek/34c8684faaef624ed998308ecf1abf8a to your computer and use it in GitHub Desktop.
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