diff options
-rw-r--r-- | pyAggr3g470r.py | 61 | ||||
-rw-r--r-- | utils.py | 31 |
2 files changed, 57 insertions, 35 deletions
diff --git a/pyAggr3g470r.py b/pyAggr3g470r.py index 61a66670..2fc0e442 100644 --- a/pyAggr3g470r.py +++ b/pyAggr3g470r.py @@ -167,17 +167,19 @@ class Root: html += "<h1>Statistics</h1>\n" top_words = utils.top_words(self.articles, 10) - utils.create_histogram(top_words) - - nb_french = 0 - nb_english = 0 - for rss_feed_id in self.articles.keys(): - for article in self.articles[rss_feed_id]: - if article[6] == 'french': - nb_french += 1 - elif article[6] == 'english': - nb_english += 1 - nb_other = nb_articles - nb_french - nb_english + if "pylab" not in utils.IMPORT_ERROR: + utils.create_histogram(top_words) + + if "oice" not in utils.IMPORT_ERROR: + nb_french = 0 + nb_english = 0 + for rss_feed_id in self.articles.keys(): + for article in self.articles[rss_feed_id]: + if article[6] == 'french': + nb_french += 1 + elif article[6] == 'english': + nb_english += 1 + nb_other = nb_articles - nb_french - nb_english html += "<table border=0>\n<tr><td>" html += "<h3>Words count</h3>\n" @@ -187,11 +189,16 @@ class Root: (word, word, frequency) html += "</ol>\n" html += "<h3>Languages</h3>\n" - html += "<ul>\n" - for language in ['english', 'french', 'other']: - html += """\t<li>%s articles in <a href="/language/%s">%s</a></li>\n""" % \ - (locals()["nb_"+language], language, language) - html += "</ul>\n</td>\n<td>" + if "oice" in utils.IMPORT_ERROR: + html += "Install the module " + html += """<a href="http://pypi.python.org/pypi/oice.langdet/">oice.langdet</a>""" + html += "</td>\n<td>" + else: + html += "<ul>\n" + for language in ['english', 'french', 'other']: + html += """\t<li>%s articles in <a href="/language/%s">%s</a></li>\n""" % \ + (locals()["nb_"+language], language, language) + html += "</ul>\n</td>\n<td>" html += """<img src="/var/histogram.png" /></td></tr></table>""" html += "<hr />\n" @@ -427,15 +434,19 @@ class Root: html += htmlnav html += """</div> <div class="left inner">""" html += """<h1>Article(s) written in %s</h1>\n<br />\n""" % (lang,) - for rss_feed_id in self.articles.keys(): - for article in self.articles[rss_feed_id]: - if article[6] == lang: - html += article[1].encode('utf-8') + \ - """ - <a href="/description/%s" rel="noreferrer" target="_blank">%s</a> - from <i><a href="%s">%s</a></i><br />\n""" % \ - (article[0].encode('utf-8'), article[2].encode('utf-8'), \ - self.feeds[rss_feed_id][5].encode('utf-8'), \ - self.feeds[rss_feed_id][3].encode('utf-8')) + if "oice" not in utils.IMPORT_ERROR: + for rss_feed_id in self.articles.keys(): + for article in self.articles[rss_feed_id]: + if article[6] == lang: + html += article[1].encode('utf-8') + \ + """ - <a href="/description/%s" rel="noreferrer" target="_blank">%s</a> + from <i><a href="%s">%s</a></i><br />\n""" % \ + (article[0].encode('utf-8'), article[2].encode('utf-8'), \ + self.feeds[rss_feed_id][5].encode('utf-8'), \ + self.feeds[rss_feed_id][3].encode('utf-8')) + else: + html += "Install the module " + html += """<a href="http://pypi.python.org/pypi/oice.langdet/">oice.langdet</a>""" html += "<hr />\n" html += htmlfooter return html @@ -7,8 +7,13 @@ __date__ = "$Date: 2010/02/24 $" __copyright__ = "Copyright (c) 2010 Cedric Bonhomme" __license__ = "GPLv3" +IMPORT_ERROR = [] + import re -import pylab +try: + import pylab +except: + IMPORT_ERROR.append("pylab") import sqlite3 import hashlib @@ -18,9 +23,12 @@ from collections import defaultdict from StringIO import StringIO -from oice.langdet import langdet -from oice.langdet import streams -from oice.langdet import languages +try: + from oice.langdet import langdet + from oice.langdet import streams + from oice.langdet import languages +except: + IMPORT_ERROR.append("oice") def detect_language(text): """ @@ -147,13 +155,13 @@ def load_feed(): # feeds[feed_id] = (nb_article, nb_article_unreaded, feed_image, # feed_title, feed_link, feed_site_link) articles, feeds = {}, {} - if list_of_feeds is not None: + if list_of_feeds != []: for feed in list_of_feeds: list_of_articles = c.execute(\ "SELECT * FROM articles WHERE feed_link='" + \ feed[2] + "'").fetchall() - if list_of_articles is not None: + if list_of_articles != []: for article in list_of_articles: sha1_hash = hashlib.sha1() sha1_hash.update(article[5].encode('utf-8')) @@ -161,11 +169,14 @@ def load_feed(): sha1_hash.update(article[2].encode('utf-8')) article_id = sha1_hash.hexdigest() - if article[3] != "": - language = detect_language(remove_html_tags(article[3][:80]).encode('utf-8') + \ - remove_html_tags(article[1]).encode('utf-8')) + if "oice" not in IMPORT_ERROR: + if article[3] != "": + language = detect_language(remove_html_tags(article[3][:80]).encode('utf-8') + \ + remove_html_tags(article[1]).encode('utf-8')) + else: + language = detect_language(remove_html_tags(article[1]).encode('utf-8')) else: - language = detect_language(remove_html_tags(article[1]).encode('utf-8')) + language = "IMPORT_ERROR" article_list = [article_id, article[0], article[1], \ article[2], article[3], article[4], language] |