web.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if "category" not in json:
  22. json["category"]=1
  23. json["category"]=int(json["category"])
  24. d.add_source(json["source"],rss,selector,json["name"],json["category"])
  25. return jsonify({'ok': "ok"})
  26. @app.route(BASEPATH+'/source/test/', methods=['POST'])
  27. def test_source():
  28. selector=None
  29. rss=False
  30. json=request.get_json()
  31. if "selector" in json and len(json["selector"]) >0:
  32. selector=json["selector"]
  33. if "rss" in json:
  34. rss=json["rss"]
  35. l=LinkList(json["source"],selector,rss=rss)
  36. return jsonify({'links': l.links})
  37. @app.route(BASEPATH+'/articles/', methods=['POST'])
  38. @app.route(BASEPATH+'/articles', methods=['POST'])
  39. def filter_articles():
  40. return jsonify(d.filter_articles(request.get_json(),limit=15))
  41. @app.route(BASEPATH+'/keywords/', methods=['GET'])
  42. @app.route(BASEPATH+'/keywords', methods=['GET'])
  43. def get_keywords():
  44. return jsonify({'keywords': d.get_keywords_list()}) #limit
  45. @app.route(BASEPATH+'/articles/', methods=['GET'])
  46. @app.route(BASEPATH+'/articles', methods=['GET'])
  47. def get_articles():
  48. return jsonify(d.filter_articles({},limit=15))
  49. @app.route(BASEPATH+'/article/delete/<string:art_id>', methods=['GET'])
  50. def delete_article(art_id):
  51. if not d.delete_article(art_id):
  52. abort(401)
  53. return jsonify({'ok':"ok"})
  54. @app.route(BASEPATH+'/article/<string:art_id>', methods=['GET'])
  55. def get_article(art_id):
  56. art=d.article(art_id)
  57. if art is None:
  58. abort(401)
  59. return jsonify({'articles': art})
  60. @app.route(BASEPATH+'/sources/', methods=['GET'])
  61. @app.route(BASEPATH+'/sources', methods=['GET'])
  62. def get_sources():
  63. return jsonify({'sources': d.sources()})
  64. @app.route(BASEPATH+'/sources/<string:source_id>', methods=['GET'])
  65. def get_source(source_id):
  66. source=d.sources(source_id)
  67. if source is None:
  68. abort(401)
  69. return jsonify({'source': source})
  70. @app.route(BASEPATH+'/add_source', methods=['POST'])
  71. def create_task():
  72. if not request.json or not 'title' in request.json:
  73. abort(400)
  74. task = {
  75. 'id': tasks[-1]['id'] + 1,
  76. 'title': request.json['title'],
  77. 'description': request.json.get('description', ""),
  78. 'done': False
  79. }
  80. tasks.append(task)
  81. return jsonify({'task': task}), 201
  82. if __name__ == '__main__':
  83. app.run(host="0.0.0.0",debug=True)