Last active
April 28, 2019 09:01
-
-
Save ritiek/34c8684faaef624ed998308ecf1abf8a to your computer and use it in GitHub Desktop.
Fetch information from wolfram and wikipedia using Flask API
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 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