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