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 lower_arr(self,arr): return [a.lower() for a in arr] def get_keywords(self,category): kw=self.client[self.DB]["keywords"] #FIXME, split on "|" or regex ret = [ {"weight":k["weight"], "kw": k["keyword"], "pattern": self.lower_arr(k["keyword"].split("|")) } for k in kw.find({}) ]#FIXME, categories return ret