db.py 641 B

12345678910111213141516171819202122232425
  1. from pymongo import MongoClient
  2. from bson.objectid import ObjectId
  3. class db():
  4. def __init__(self):
  5. self.client = MongoClient('localhost', 27017)
  6. self.col=self.client.alexis["sources"]
  7. def jsonable(self, el):
  8. if el is None or "_id" not in el:
  9. return el
  10. el["_id"]=str(el["_id"])
  11. return el
  12. def sources(self,id=None):
  13. if id is None:
  14. return [ self.jsonable(p) for p in self.col.find() ]
  15. ret=self.col.find_one({"_id":ObjectId(id)})
  16. return self.jsonable(ret)
  17. def rss_sources(self):
  18. return [ p for p in self.col.find({"rss":1}) ]