rest.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/python3
  2. from flask import Flask
  3. from flask import jsonify
  4. from flask_cors import CORS, cross_origin
  5. from whoosh.qparser import MultifieldParser
  6. from whoosh import index
  7. from . import api
  8. app = Flask(__name__)
  9. cors = CORS(app)
  10. app.config['CORS_HEADERS'] = 'Content-Type'
  11. ix = index.open_dir("data")
  12. @app.route("/search/<query>")
  13. @cross_origin()
  14. def search(query):
  15. with ix.searcher() as searcher:
  16. query = MultifieldParser(["title", "description"], ix.schema).parse("*{}*".format(query))
  17. results = searcher.search(query, limit=10)
  18. return jsonify([dict(r) for r in results])
  19. @app.route("/realms/<region>")
  20. @cross_origin()
  21. def realms(region):
  22. return jsonify([realm['name'] for realm in api.get_json("/wow/realm/status", url="https://{}.api.battle.net".format(region))['realms']])
  23. @app.route("/character/<region>/<realm>/<name>")
  24. @cross_origin()
  25. def character(region, realm, name):
  26. return jsonify(api.get_json("/wow/character/{}/{}".format(realm, name), url="https://{}.api.battle.net".format(region)))
  27. @app.after_request
  28. def apply_json(response):
  29. response.headers["Content-Type"] = "application/json"
  30. return response
  31. if __name__ == '__main__':
  32. app.run()