web.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3
  2. from flask import Flask, abort, jsonify, request
  3. from db import db
  4. from linklist import LinkList
  5. app = Flask(__name__)
  6. d = db()
  7. BASEPATH = "/pymnts"
  8. @app.route(BASEPATH + '/source/add/', methods=['POST'])
  9. def add_source():
  10. selector = None
  11. rss = False
  12. json = request.get_json()
  13. if "selector" in json:
  14. selector = json["selector"]
  15. if "rss" in json:
  16. rss = json["rss"]
  17. if "category" not in json:
  18. json["category"] = 1
  19. json["category"] = int(json["category"])
  20. d.add_source(json["source"], rss, selector, json["name"], json["category"])
  21. return jsonify({'ok': "ok"})
  22. @app.route(BASEPATH + '/source/test/', methods=['POST'])
  23. def test_source():
  24. selector = None
  25. rss = False
  26. json = request.get_json()
  27. if "selector" in json and len(json["selector"]) > 0:
  28. selector = json["selector"]
  29. if "rss" in json:
  30. rss = json["rss"]
  31. l = LinkList(json["source"], selector, rss=rss)
  32. return jsonify({'links': l.links})
  33. @app.route(BASEPATH + '/articles/', methods=['POST'])
  34. @app.route(BASEPATH + '/articles', methods=['POST'])
  35. def filter_articles():
  36. return jsonify(d.filter_articles(request.get_json(), limit=15))
  37. @app.route(BASEPATH + '/keywords/', methods=['GET'])
  38. @app.route(BASEPATH + '/keywords', methods=['GET'])
  39. def get_keywords():
  40. return jsonify({'keywords': d.get_keywords_list()}) # limit
  41. @app.route(BASEPATH + '/keywords/', methods=['POST'])
  42. @app.route(BASEPATH + '/keywords', methods=['POST'])
  43. def update_keywords(kw, _type):
  44. if _type == 'ADD':
  45. res = db.add_keyword(kw)
  46. return jsonify({})
  47. elif _type == 'DEL':
  48. res = db.delete_keyword(kw)
  49. return jsonify({'keywords': d.get_keywords_list()})
  50. elif _type == 'UPD':
  51. res = db.update_keyword(kw)
  52. return jsonify({'keywords': d.get_keywords_list()})
  53. @app.route(BASEPATH + '/articles/', methods=['GET'])
  54. @app.route(BASEPATH + '/articles', methods=['GET'])
  55. def get_articles():
  56. return jsonify(d.filter_articles({}, limit=15))
  57. @app.route(BASEPATH + '/article/delete/<string:art_id>', methods=['GET'])
  58. def delete_article(art_id):
  59. if not d.delete_article(art_id):
  60. abort(401)
  61. return jsonify({'ok': "ok"})
  62. @app.route(BASEPATH + '/article/<string:art_id>', methods=['GET'])
  63. def get_article(art_id):
  64. art = d.article(art_id)
  65. if art is None:
  66. abort(401)
  67. return jsonify({'articles': art})
  68. @app.route(BASEPATH + '/sources/', methods=['GET'])
  69. @app.route(BASEPATH + '/sources', methods=['GET'])
  70. def get_sources():
  71. s = sorted(d.sources(), key=lambda k: k['name'].lower())
  72. return jsonify({'sources': s})
  73. @app.route(BASEPATH + '/sources/<string:source_id>', methods=['GET'])
  74. def get_source(source_id):
  75. source = d.sources(source_id)
  76. if source is None:
  77. abort(401)
  78. return jsonify({'source': source})
  79. @app.route(BASEPATH + '/add_source', methods=['POST'])
  80. def create_task():
  81. if not request.json or 'title' not in request.json:
  82. abort(400)
  83. task = {
  84. 'id': tasks[-1]['id'] + 1,
  85. 'title': request.json['title'],
  86. 'description': request.json.get('description', ""),
  87. 'done': False
  88. }
  89. tasks.append(task)
  90. return jsonify({'task': task}), 201
  91. if __name__ == '__main__':
  92. app.run(host="0.0.0.0", debug=False)