aboutsummaryrefslogtreecommitdiff
path: root/source/mongodb.py
diff options
context:
space:
mode:
authorCédric Bonhomme <kimble.mandel@gmail.com>2012-11-30 17:42:06 +0100
committerCédric Bonhomme <kimble.mandel@gmail.com>2012-11-30 17:42:06 +0100
commit3e2df796e230aa67371594cddd699a4e67ecd0e9 (patch)
tree32f25d944168966429e01420038d14d30b6f1f31 /source/mongodb.py
parentList of favorites articles is now always sorted by date. (diff)
downloadnewspipe-3e2df796e230aa67371594cddd699a4e67ecd0e9.tar.gz
newspipe-3e2df796e230aa67371594cddd699a4e67ecd0e9.tar.bz2
newspipe-3e2df796e230aa67371594cddd699a4e67ecd0e9.zip
Code factorization.
Diffstat (limited to 'source/mongodb.py')
-rw-r--r--source/mongodb.py56
1 files changed, 27 insertions, 29 deletions
diff --git a/source/mongodb.py b/source/mongodb.py
index 1ba69304..84e81a04 100644
--- a/source/mongodb.py
+++ b/source/mongodb.py
@@ -97,35 +97,33 @@ class Articles(object):
feeds.sort(key = lambda elem: elem['feed_title'].lower())
return feeds
- def get_all_articles(self):
- """
- Return articles of all feeds object (articles of all MongoDB articles collections).
- All articles collections are of type 1.
- """
- articles = []
- collections = self.db.collection_names()
- for collection_name in collections:
- collection = self.db[collection_name]
- articles.extend(collection.find({'type':1}))
- return articles
-
- def get_article(self, feed_id, article_id):
- """
- Get an article of a specified feed.
- """
- collection = self.db[str(feed_id)]
- return next(collection.find({"article_id":article_id}))
-
- def get_articles_from_collection(self, feed_id, condition=None, limit=1000000000):
- """
- Return all the articles of a collection.
- """
- collection = self.db[str(feed_id)]
- if condition is None:
- cursor = collection.find({"type":1}, limit=limit)
- else:
- cursor = collection.find({"type":1, condition[0]:condition[1]}, limit=limit)
- return cursor.sort([("article_date", pymongo.DESCENDING)])
+ def get_articles(self, feed_id=None, article_id=None, condition=None, limit=1000000000):
+ """
+ Return one or several articles.
+ The parameter "condition" is an optional requirement, for example:
+ get_articles(feed["feed_id"], condition=("article_readed", False)) will
+ return all unread articles of feed.
+ """
+ if feed_id == None:
+ # Return all articles.
+ articles = []
+ collections = self.db.collection_names()
+ for collection_name in collections:
+ collection = self.db[collection_name]
+ articles.extend(collection.find({'type':1}))
+ return articles
+ elif feed_id != None and article_id == None:
+ # Return all the articles of a collection.
+ collection = self.db[str(feed_id)]
+ if condition is None:
+ cursor = collection.find({"type":1}, limit=limit)
+ else:
+ cursor = collection.find({"type":1, condition[0]:condition[1]}, limit=limit)
+ return cursor.sort([("article_date", pymongo.DESCENDING)])
+ elif feed_id != None and article_id != None:
+ # Return a precise article.
+ collection = self.db[str(feed_id)]
+ return next(collection.find({"article_id":article_id}))
def nb_articles(self, feed_id=None):
"""
bgstack15