David 10 лет назад
Родитель
Сommit
0c167829a5
2 измененных файлов с 23 добавлено и 3 удалено
  1. 11 3
      db.py
  2. 12 0
      web.py

+ 11 - 3
db.py

@@ -5,7 +5,7 @@ class db():
     def __init__(self):
     def __init__(self):
         self.client = MongoClient('localhost', 27017)
         self.client = MongoClient('localhost', 27017)
         self.c_sources=self.client.alexis["sources"]
         self.c_sources=self.client.alexis["sources"]
-        self.articles=self.client.alexis["articles"]
+        self.c_articles=self.client.alexis["articles"]
 
 
     def jsonable(self, el):
     def jsonable(self, el):
         if el is None or "_id" not in el:
         if el is None or "_id" not in el:
@@ -14,6 +14,14 @@ class db():
         el["_id"]=str(el["_id"])
         el["_id"]=str(el["_id"])
         return el
         return el
 
 
+
+    def articles(self, id=None):
+        if id is None:
+            return [ self.jsonable(p) for p in self.c_articles.find() ]
+
+        ret=self.c_articles.find_one({"_id":ObjectId(id)})
+        return self.jsonable(ret)
+
     def sources(self,id=None):
     def sources(self,id=None):
         if id is None:
         if id is None:
             return [ self.jsonable(p) for p in self.c_sources.find() ]
             return [ self.jsonable(p) for p in self.c_sources.find() ]
@@ -27,14 +35,14 @@ class db():
 
 
     def insert_article(self,o):
     def insert_article(self,o):
         try:
         try:
-            self.articles.insert_one(o)
+            self.c_articles.insert_one(o)
             return True
             return True
         except errors.DuplicateKeyError:
         except errors.DuplicateKeyError:
             print("Dup")
             print("Dup")
             return False
             return False
 
 
     def article_exists(self,url):
     def article_exists(self,url):
-        ret=self.articles.find_one({'url':url})
+        ret=self.c_articles.find_one({'url':url})
         if ret is None:
         if ret is None:
             return False
             return False
         return True 
         return True 

+ 12 - 0
web.py

@@ -11,6 +11,18 @@ d = db()
 def index():
 def index():
     return "Hello, World!"
     return "Hello, World!"
 
 
+@app.route('/articles/', methods=['GET'])
+@app.route('/articles', methods=['GET'])
+def get_articles():
+    return jsonify({'articles': d.articles()})
+
+@app.route('/articles/<string:art_id>', methods=['GET'])
+def get_article(art_id):
+    art=d.articles(art_id)
+    if art is None:
+        abort(401)
+    return jsonify({'articles': art})
+
 @app.route('/sources/', methods=['GET'])
 @app.route('/sources/', methods=['GET'])
 @app.route('/sources', methods=['GET'])
 @app.route('/sources', methods=['GET'])
 def get_sources():
 def get_sources():