web.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. from flask import Flask,abort,jsonify,request
  3. from db import db
  4. import json
  5. app = Flask(__name__)
  6. d = db()
  7. BASEPATH="/pymnts"
  8. @app.route(BASEPATH+'/')
  9. def index():
  10. return "Hello, World!"
  11. @app.route(BASEPATH+'/articles/', methods=['POST'])
  12. @app.route(BASEPATH+'/articles', methods=['POST'])
  13. def filter_articles():
  14. return jsonify({'articles': d.filter_articles(request.get_json(),limit=15)})
  15. @app.route(BASEPATH+'/keywords/', methods=['GET'])
  16. @app.route(BASEPATH+'/keywords', methods=['GET'])
  17. def get_keywords():
  18. return jsonify({'keywords': d.get_keywords_list()}) #limit
  19. @app.route(BASEPATH+'/articles/', methods=['GET'])
  20. @app.route(BASEPATH+'/articles', methods=['GET'])
  21. def get_articles():
  22. return jsonify({'articles': d.articles(limit=80)}) #limit
  23. @app.route(BASEPATH+'/article/<string:art_id>', methods=['GET'])
  24. def get_article(art_id):
  25. art=d.articles(id=art_id)
  26. if art is None:
  27. abort(401)
  28. return jsonify({'articles': art})
  29. @app.route(BASEPATH+'/sources/', methods=['GET'])
  30. @app.route(BASEPATH+'/sources', methods=['GET'])
  31. def get_sources():
  32. return jsonify({'sources': d.sources()})
  33. @app.route(BASEPATH+'/sources/<string:source_id>', methods=['GET'])
  34. def get_source(source_id):
  35. source=d.sources(source_id)
  36. if source is None:
  37. abort(401)
  38. return jsonify({'source': source})
  39. @app.route(BASEPATH+'/add_source', methods=['POST'])
  40. def create_task():
  41. if not request.json or not 'title' in request.json:
  42. abort(400)
  43. task = {
  44. 'id': tasks[-1]['id'] + 1,
  45. 'title': request.json['title'],
  46. 'description': request.json.get('description', ""),
  47. 'done': False
  48. }
  49. tasks.append(task)
  50. return jsonify({'task': task}), 201
  51. if __name__ == '__main__':
  52. app.run(debug=True)