From 09741d99fff3b1ef14749b0acf517994f9acf96f Mon Sep 17 00:00:00 2001 From: cedricbonhomme Date: Fri, 11 Jun 2010 08:08:58 +0200 Subject: Faster and safer code. --- pyAggr3g470r.py | 14 ++++++++---- utils.py | 71 ++++++++++++++++++++++++++++++--------------------------- 2 files changed, 46 insertions(+), 39 deletions(-) diff --git a/pyAggr3g470r.py b/pyAggr3g470r.py index 787a4c69..3b758484 100755 --- a/pyAggr3g470r.py +++ b/pyAggr3g470r.py @@ -187,7 +187,7 @@ class Root: if self.articles: html += "

Delete Feeds

\n" - html += """
\n""" for feed_id in self.articles.keys(): html += """\t\n""" % \ (feed_id, self.feeds[feed_id][3].encode('utf-8')) @@ -773,15 +773,19 @@ class Root: add_feed.exposed = True - def remove_feed(self, url): + def remove_feed(self, feed_id): """ - Remove a feed from the file fee.lst. + Remove a feed from the file feed.lst and from the SQLite base. """ html = htmlheader html += htmlnav html += """
""" - utils.remove_feed(self.feeds[url][4]) - html+= """

All articles from this feed are now removed from the base.


""" + if feed_id in self.feeds.keys(): + utils.remove_feed(self.feeds[feed_id][4]) + html+= """

All articles from the feed %s are now removed from the base.


""" % \ + (self.feeds[feed_id][3].encode('utf-8'),) + else: + return self.error_page("This feed do not exists.") html += """Back to the management page.
\n""" html += "
\n" html += htmlfooter diff --git a/utils.py b/utils.py index d2ae88e0..38be53ef 100755 --- a/utils.py +++ b/utils.py @@ -167,25 +167,22 @@ def send_mail(mfrom, mto, feed_title, message): mail.as_string()) server.quit() +def string_to_datetime(stringtime): + """ + Convert a string to datetime. + """ + date, time = stringtime.split(' ') + year, month, day = date.split('-') + hour, minute, second = time.split(':') + return datetime(year=int(year), month=int(month), day=int(day), \ + hour=int(hour), minute=int(minute), second=int(second)) + def compare(stringtime1, stringtime2): """ Compare two dates in the format 'yyyy-mm-dd hh:mm:ss'. """ - date1, time1 = stringtime1.split(' ') - date2, time2 = stringtime2.split(' ') - - year1, month1, day1 = date1.split('-') - year2, month2, day2 = date2.split('-') - - hour1, minute1, second1 = time1.split(':') - hour2, minute2, second2 = time2.split(':') - - datetime1 = datetime(year=int(year1), month=int(month1), day=int(day1), \ - hour=int(hour1), minute=int(minute1), second=int(second1)) - - datetime2 = datetime(year=int(year2), month=int(month2), day=int(day2), \ - hour=int(hour2), minute=int(minute2), second=int(second2)) - + datetime1 = string_to_datetime(stringtime1) + datetime2 = string_to_datetime(stringtime2) if datetime1 < datetime2: return -1 elif datetime1 > datetime2: @@ -196,12 +193,13 @@ def add_feed(feed_url): """ Add the URL feed_url in the file feed.lst. """ - for line in open("./var/feed.lst", "r"): - if feed_url in line: - return False + if os.path.exists("./var/feed.lst"): + for line in open("./var/feed.lst", "r"): + if feed_url in line: + return False with open("./var/feed.lst", "a") as f: f.write(feed_url + "\n") - return True + return True def remove_feed(feed_url): """ @@ -209,18 +207,22 @@ def remove_feed(feed_url): """ feeds = [] # Remove the URL from the file feed.lst - for line in open("./var/feed.lst", "r"): - if feed_url not in line: - feeds.append(line.replace("\n", "")) - with open("./var/feed.lst", "w") as f: - f.write("\n".join(feeds)) - # Remove articles from this feed from the SQLite base. - conn = sqlite3.connect(sqlite_base, isolation_level = None) - c = conn.cursor() - c.execute("DELETE FROM feeds WHERE feed_link='" + feed_url +"'") - c.execute("DELETE FROM articles WHERE feed_link='" + feed_url +"'") - conn.commit() - c.close() + if os.path.exists("./var/feed.lst"): + for line in open("./var/feed.lst", "r"): + if feed_url not in line: + feeds.append(line.replace("\n", "")) + with open("./var/feed.lst", "w") as f: + f.write("\n".join(feeds)) + # Remove articles from this feed from the SQLite base. + try: + conn = sqlite3.connect(sqlite_base, isolation_level = None) + c = conn.cursor() + c.execute("DELETE FROM feeds WHERE feed_link='" + feed_url +"'") + c.execute("DELETE FROM articles WHERE feed_link='" + feed_url +"'") + conn.commit() + c.close() + except: + pass def search_feed(url): """ @@ -275,16 +277,17 @@ def load_feed(): # feed_title, feed_link, feed_site_link, mail) articles, feeds = {}, {} if list_of_feeds != []: + sha1_hash = hashlib.sha1() for feed in list_of_feeds: list_of_articles = c.execute(\ "SELECT * FROM articles WHERE feed_link='" + \ feed[2] + "'").fetchall() + sha1_hash.update(feed[2].encode('utf-8')) + feed_id = sha1_hash.hexdigest() + if list_of_articles != []: for article in list_of_articles: - sha1_hash = hashlib.sha1() - sha1_hash.update(article[5].encode('utf-8')) - feed_id = sha1_hash.hexdigest() sha1_hash.update(article[2].encode('utf-8')) article_id = sha1_hash.hexdigest() -- cgit