#!/usr/bin/env python3 from flask import Flask,abort,jsonify,request from db import db import json from linklist import LinkList app = Flask(__name__) d = db() BASEPATH="/pymnts" @app.route(BASEPATH+'/') def index(): return "Hello, World!" @app.route(BASEPATH+'/source/add/', methods=['POST']) def add_source(): selector=None rss=False json=request.get_json() if "selector" in json: selector=json["selector"] if "rss" in json: rss=json["rss"] if "category" not in json: json["category"]=1 json["category"]=int(json["category"]) d.add_source(json["source"],rss,selector,json["name"],json["category"]) return jsonify({'ok': "ok"}) @app.route(BASEPATH+'/source/test/', methods=['POST']) def test_source(): selector=None rss=False json=request.get_json() if "selector" in json and len(json["selector"]) >0: selector=json["selector"] if "rss" in json: rss=json["rss"] l=LinkList(json["source"],selector,rss=rss) return jsonify({'links': l.links}) @app.route(BASEPATH+'/articles/', methods=['POST']) @app.route(BASEPATH+'/articles', methods=['POST']) def filter_articles(): return jsonify(d.filter_articles(request.get_json(),limit=15)) @app.route(BASEPATH+'/keywords/', methods=['GET']) @app.route(BASEPATH+'/keywords', methods=['GET']) def get_keywords(): return jsonify({'keywords': d.get_keywords_list()}) #limit @app.route(BASEPATH+'/articles/', methods=['GET']) @app.route(BASEPATH+'/articles', methods=['GET']) def get_articles(): return jsonify(d.filter_articles({},limit=15)) @app.route(BASEPATH+'/article/delete/', methods=['GET']) def delete_article(art_id): if not d.delete_article(art_id): abort(401) return jsonify({'ok':"ok"}) @app.route(BASEPATH+'/article/', methods=['GET']) def get_article(art_id): art=d.article(art_id) if art is None: abort(401) return jsonify({'articles': art}) @app.route(BASEPATH+'/sources/', methods=['GET']) @app.route(BASEPATH+'/sources', methods=['GET']) def get_sources(): return jsonify({'sources': sorted(d.sources(), key=lambda k: k['name'].lower())}) @app.route(BASEPATH+'/sources/', methods=['GET']) def get_source(source_id): source=d.sources(source_id) if source is None: abort(401) return jsonify({'source': source}) @app.route(BASEPATH+'/add_source', methods=['POST']) def create_task(): if not request.json or not 'title' in request.json: abort(400) task = { 'id': tasks[-1]['id'] + 1, 'title': request.json['title'], 'description': request.json.get('description', ""), 'done': False } tasks.append(task) return jsonify({'task': task}), 201 if __name__ == '__main__': app.run(host="0.0.0.0",debug=True)