web.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/delete/<string:art_id>', methods=['GET'])
  47. def delete_article(art_id):
  48. if not d.delete_article(art_id):
  49. abort(401)
  50. return jsonify({'ok':"ok"})
  51. @app.route(BASEPATH+'/article/<string:art_id>', methods=['GET'])
  52. def get_article(art_id):
  53. art=d.article(art_id)
  54. if art is None:
  55. abort(401)
  56. return jsonify({'articles': art})
  57. @app.route(BASEPATH+'/sources/', methods=['GET'])
  58. @app.route(BASEPATH+'/sources', methods=['GET'])
  59. def get_sources():
  60. return jsonify({'sources': d.sources()})
  61. @app.route(BASEPATH+'/sources/<string:source_id>', methods=['GET'])
  62. def get_source(source_id):
  63. source=d.sources(source_id)
  64. if source is None:
  65. abort(401)
  66. return jsonify({'source': source})
  67. @app.route(BASEPATH+'/add_source', methods=['POST'])
  68. def create_task():
  69. if not request.json or not 'title' in request.json:
  70. abort(400)
  71. task = {
  72. 'id': tasks[-1]['id'] + 1,
  73. 'title': request.json['title'],
  74. 'description': request.json.get('description', ""),
  75. 'done': False
  76. }
  77. tasks.append(task)
  78. return jsonify({'task': task}), 201
  79. if __name__ == '__main__':
  80. app.run(host="0.0.0.0",debug=True)