| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- from pymongo import MongoClient,errors
- from bson.objectid import ObjectId
- class db():
- def __init__(self):
- self.client = MongoClient('localhost', 27017)
- self.c_sources=self.client.alexis["sources"]
- self.c_articles=self.client.alexis["articles"]
- def jsonable(self, el):
- if el is None or "_id" not in el:
- return el
- el["_id"]=str(el["_id"])
- 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):
- if id is None:
- return [ self.jsonable(p) for p in self.c_sources.find() ]
- ret=self.c_sources.find_one({"_id":ObjectId(id)})
- return self.jsonable(ret)
- def rss_sources(self):
- return [ p for p in self.c_sources.find({"rss":1}) ]
- def insert_article(self,o):
- try:
- self.c_articles.insert_one(o)
- return True
- except errors.DuplicateKeyError:
- print("Dup")
- return False
- def article_exists(self,url):
- ret=self.c_articles.find_one({'url':url})
- if ret is None:
- return False
- return True
|