web.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. from flask import Flask,abort,jsonify,request
  3. from db import db
  4. import json
  5. from linklist import LinkList
  6. app = Flask(__name__)
  7. d = db()
  8. BASEPATH="/pymnts"
  9. @app.route(BASEPATH+'/')
  10. def index():
  11. return "Hello, World!"
  12. @app.route(BASEPATH+'/source/add/', methods=['POST'])
  13. def add_source():
  14. selector=None
  15. rss=False
  16. json=request.get_json()
  17. if "selector" in json:
  18. selector=json["selector"]
  19. if "rss" in json:
  20. rss=json["rss"]
  21. d.add_source(json["source"],rss,selector,json["name"])
  22. return jsonify({'ok': "ok"})
  23. @app.route(BASEPATH+'/source/test/', methods=['POST'])
  24. def test_source():
  25. selector=None
  26. rss=False
  27. json=request.get_json()
  28. if "selector" in json and len(json["selector"]) >0:
  29. selector=json["selector"]
  30. if "rss" in json:
  31. rss=json["rss"]
  32. l=LinkList(json["source"],selector,rss=rss)
  33. return jsonify({'links': l.links})
  34. @app.route(BASEPATH+'/articles/', methods=['POST'])
  35. @app.route(BASEPATH+'/articles', methods=['POST'])
  36. def filter_articles():
  37. return jsonify(d.filter_articles(request.get_json(),limit=15))
  38. @app.route(BASEPATH+'/keywords/', methods=['GET'])
  39. @app.route(BASEPATH+'/keywords', methods=['GET'])
  40. def get_keywords():
  41. return jsonify({'keywords': d.get_keywords_list()}) #limit
  42. @app.route(BASEPATH+'/articles/', methods=['GET'])
  43. @app.route(BASEPATH+'/articles', methods=['GET'])
  44. def get_articles():
  45. return jsonify(d.filter_articles({},limit=15))
  46. @app.route(BASEPATH+'/article/<string:art_id>', methods=['GET'])
  47. def get_article(art_id):
  48. art=d.article(art_id)
  49. if art is None:
  50. abort(401)
  51. return jsonify({'articles': art})
  52. @app.route(BASEPATH+'/sources/', methods=['GET'])
  53. @app.route(BASEPATH+'/sources', methods=['GET'])
  54. def get_sources():
  55. return jsonify({'sources': d.sources()})
  56. @app.route(BASEPATH+'/sources/<string:source_id>', methods=['GET'])
  57. def get_source(source_id):
  58. source=d.sources(source_id)
  59. if source is None:
  60. abort(401)
  61. return jsonify({'source': source})
  62. @app.route(BASEPATH+'/add_source', methods=['POST'])
  63. def create_task():
  64. if not request.json or not 'title' in request.json:
  65. abort(400)
  66. task = {
  67. 'id': tasks[-1]['id'] + 1,
  68. 'title': request.json['title'],
  69. 'description': request.json.get('description', ""),
  70. 'done': False
  71. }
  72. tasks.append(task)
  73. return jsonify({'task': task}), 201
  74. if __name__ == '__main__':
  75. app.run(debug=True)