diff options
author | Cédric Bonhomme <kimble.mandel@gmail.com> | 2012-11-30 17:42:06 +0100 |
---|---|---|
committer | Cédric Bonhomme <kimble.mandel@gmail.com> | 2012-11-30 17:42:06 +0100 |
commit | 3e2df796e230aa67371594cddd699a4e67ecd0e9 (patch) | |
tree | 32f25d944168966429e01420038d14d30b6f1f31 /source | |
parent | List of favorites articles is now always sorted by date. (diff) | |
download | newspipe-3e2df796e230aa67371594cddd699a4e67ecd0e9.tar.gz newspipe-3e2df796e230aa67371594cddd699a4e67ecd0e9.tar.bz2 newspipe-3e2df796e230aa67371594cddd699a4e67ecd0e9.zip |
Code factorization.
Diffstat (limited to 'source')
-rw-r--r-- | source/mongodb.py | 56 | ||||
-rwxr-xr-x | source/pyAggr3g470r.py | 34 | ||||
-rw-r--r-- | source/templates/index.html | 2 |
3 files changed, 45 insertions, 47 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): """ diff --git a/source/pyAggr3g470r.py b/source/pyAggr3g470r.py index 7455bc1c..184c2e45 100755 --- a/source/pyAggr3g470r.py +++ b/source/pyAggr3g470r.py @@ -173,7 +173,7 @@ class pyAggr3g470r(object): """ More advanced statistics. """ - articles = self.mongo.get_all_articles() + articles = self.mongo.get_articless() top_words = utils.top_words(articles, n=50, size=int(word_size)) tag_cloud = utils.tag_cloud(top_words) tmpl = lookup.get_template("statistics.html") @@ -198,7 +198,7 @@ class pyAggr3g470r(object): html += """<h1>Articles containing the string <i>%s</i></h1><br />""" % (query,) if feed_id is not None: - for article in self.mongo.get_articles_from_collection(feed_id): + for article in self.mongo.get_articles(feed_id): article_content = utils.clear_string(article.article_description) if not article_content: utils.clear_string(article.article_title) @@ -217,7 +217,7 @@ class pyAggr3g470r(object): feeds = self.mongo.get_all_feeds() for feed in feeds: new_feed_section = True - for article in self.mongo.get_articles_from_collection(feed["feed_id"]): + for article in self.mongo.get_articles(feed["feed_id"]): article_content = utils.clear_string(article["article_content"]) if not article_content: utils.clear_string(article["article_title"]) @@ -277,8 +277,8 @@ class pyAggr3g470r(object): try: feed_id, article_id = param.split(':') feed = self.mongo.get_feed(feed_id) - articles = self.mongo.get_articles_from_collection(feed_id) - article = self.mongo.get_article(feed_id, article_id) + articles = self.mongo.get_articles(feed_id) + article = self.mongo.get_articles(feed_id, article_id) except: return self.error("Bad URL. This article do not exists.") @@ -315,8 +315,8 @@ class pyAggr3g470r(object): # Previous and following articles previous, following = None, None - liste = self.mongo.get_articles_from_collection(feed_id) - for current_article in self.mongo.get_articles_from_collection(feed_id): + liste = self.mongo.get_articles(feed_id) + for current_article in self.mongo.get_articles(feed_id): next(articles) if current_article["article_id"] == article_id: break @@ -343,7 +343,7 @@ class pyAggr3g470r(object): """ try: feed = self.mongo.get_feed(feed_id) - articles = self.mongo.get_articles_from_collection(feed_id, limit=10) + articles = self.mongo.get_articles(feed_id, limit=10) nb_articles_feed = self.mongo.nb_articles(feed_id) nb_articles_total = self.mongo.nb_articles() nb_unread_articles_feed = self.mongo.nb_unread_articles(feed_id) @@ -450,7 +450,7 @@ class pyAggr3g470r(object): (feed["feed_id"],) dic = {} - top_words = utils.top_words(articles = self.mongo.get_articles_from_collection(feed_id), n=50, size=int(word_size)) + top_words = utils.top_words(articles = self.mongo.get_articles(feed_id), n=50, size=int(word_size)) html += "</br />\n<h1>Tag cloud</h1>\n" # Tags cloud html += """<form method=get action="/feed/%s">\n""" % (feed["feed_id"],) @@ -473,7 +473,7 @@ class pyAggr3g470r(object): """ try: feed = self.mongo.get_feed(feed_id) - articles = self.mongo.get_articles_from_collection(feed_id) + articles = self.mongo.get_articles(feed_id) except KeyError: return self.error("This feed do not exists.") tmpl = lookup.get_template("articles.html") @@ -501,7 +501,7 @@ class pyAggr3g470r(object): nb_unread = 0 # For all unread article of the current feed. - for article in self.mongo.get_articles_from_collection(feed["feed_id"], condition=("article_readed", False)): + for article in self.mongo.get_articles(feed["feed_id"], condition=("article_readed", False)): nb_unread += 1 if new_feed_section is True: new_feed_section = False @@ -536,7 +536,7 @@ class pyAggr3g470r(object): <br />""" % (feed_id, feed["feed_title"]) # For all unread article of the feed. - for article in self.mongo.get_articles_from_collection(feed_id, condition=("article_readed", False)): + for article in self.mongo.get_articles(feed_id, condition=("article_readed", False)): # descrition for the CSS ToolTips article_content = utils.clear_string(article["article_content"]) if article_content: @@ -592,7 +592,7 @@ class pyAggr3g470r(object): timeline = Counter() for feed in feeds: new_feed_section = True - for article in self.mongo.get_articles_from_collection(feed["feed_id"]): + for article in self.mongo.get_articles(feed["feed_id"]): if query == "all": timeline[str(article["article_date"]).split(' ')[0].split('-')[0]] += 1 @@ -659,7 +659,7 @@ class pyAggr3g470r(object): try: feed_id, article_id = target.split(':') feed = self.mongo.get_feed(feed_id) - article = self.mongo.get_article(feed_id, article_id) + article = self.mongo.get_articles(feed_id, article_id) except: return self.error("Bad URL. This article do not exists.") description = utils.clear_string(article["article_content"]) @@ -734,7 +734,7 @@ class pyAggr3g470r(object): """ try: like, feed_id, article_id = param.split(':') - articles = self.mongo.get_article(feed_id, article_id) + articles = self.mongo.get_articles(feed_id, article_id) except: return self.error("Bad URL. This article do not exists.") self.mongo.like_article("1"==like, feed_id, article_id) @@ -912,8 +912,8 @@ class pyAggr3g470r(object): try: feed_id, article_id = param.split(':') feed = self.mongo.get_feed(feed_id) - articles = self.mongo.get_articles_from_collection(feed_id) - article = self.mongo.get_article(feed_id, article_id) + articles = self.mongo.get_articles(feed_id) + article = self.mongo.get_articles(feed_id, article_id) except: self.error("This article do not exists.") try: diff --git a/source/templates/index.html b/source/templates/index.html index 69d124a2..e850ece9 100644 --- a/source/templates/index.html +++ b/source/templates/index.html @@ -57,7 +57,7 @@ import utils feed["feed_link"], feed["feed_image"]) # The main page display only 10 articles by feeds. - for article in mongo.get_articles_from_collection(feed["feed_id"], limit=10): + for article in mongo.get_articles(feed["feed_id"], limit=10): if article["article_readed"] == False: # not readed articles are in bold not_read_begin, not_read_end = "<b>", "</b>" |