| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- from pymongo import MongoClient,errors
- from bson.objectid import ObjectId
- import re
- class db():
- DB="alexis"
- def __init__(self):
- self.client = MongoClient('localhost', 27017)
- self.c_sources=self.client[self.DB]["sources"]
- self.c_articles=self.client[self.DB]["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
- def get_keywords(self,category):
- kw=self.client[self.DB]["keywords"]
- ret = [ {"weight":k["weight"], "kw": k["keyword"], "pattern": re.compile(k["keyword"]) } for k in kw.find({}) ]#FIXME, categories
- return ret
|