|
|
@@ -1,10 +1,11 @@
|
|
|
-from pymongo import MongoClient
|
|
|
+from pymongo import MongoClient,errors
|
|
|
from bson.objectid import ObjectId
|
|
|
|
|
|
class db():
|
|
|
def __init__(self):
|
|
|
self.client = MongoClient('localhost', 27017)
|
|
|
- self.col=self.client.alexis["sources"]
|
|
|
+ self.c_sources=self.client.alexis["sources"]
|
|
|
+ self.articles=self.client.alexis["articles"]
|
|
|
|
|
|
def jsonable(self, el):
|
|
|
if el is None or "_id" not in el:
|
|
|
@@ -15,11 +16,25 @@ class db():
|
|
|
|
|
|
def sources(self,id=None):
|
|
|
if id is None:
|
|
|
- return [ self.jsonable(p) for p in self.col.find() ]
|
|
|
+ return [ self.jsonable(p) for p in self.c_sources.find() ]
|
|
|
|
|
|
- ret=self.col.find_one({"_id":ObjectId(id)})
|
|
|
+ ret=self.c_sources.find_one({"_id":ObjectId(id)})
|
|
|
return self.jsonable(ret)
|
|
|
|
|
|
def rss_sources(self):
|
|
|
- return [ p for p in self.col.find({"rss":1}) ]
|
|
|
+ return [ p for p in self.c_sources.find({"rss":1}) ]
|
|
|
|
|
|
+
|
|
|
+ def insert_article(self,o):
|
|
|
+ try:
|
|
|
+ self.articles.insert_one(o)
|
|
|
+ return True
|
|
|
+ except errors.DuplicateKeyError:
|
|
|
+ print("Dup")
|
|
|
+ return False
|
|
|
+
|
|
|
+ def article_exists(self,url):
|
|
|
+ ret=self.articles.find_one({'url':url})
|
|
|
+ if ret is None:
|
|
|
+ return False
|
|
|
+ return True
|