From bcb58822eaa56fb5c41548431d3d8ad2e315ee86 Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Wed, 23 Apr 2014 22:39:32 +0200 Subject: This fixes #2. --- pyaggr3g470r/utils.py | 64 ++++++++++++++++++++++++++++++++------------------- pyaggr3g470r/views.py | 5 ++-- 2 files changed, 42 insertions(+), 27 deletions(-) (limited to 'pyaggr3g470r') diff --git a/pyaggr3g470r/utils.py b/pyaggr3g470r/utils.py index 6f0bab33..ce210c20 100755 --- a/pyaggr3g470r/utils.py +++ b/pyaggr3g470r/utils.py @@ -83,39 +83,50 @@ def import_opml(email, opml_file): subscriptions = opml.parse(opml_file) except Exception as e: raise e - nb = 0 - for subscription in subscriptions: - try: - title = subscription.text - except: - title = "" + def read(subsubscription, nb=0): + """ + Parse recursively through the categories and sub-categories. + """ + for subscription in subsubscription: + + if len(subscription) != 0: + nb = read(subscription, nb) + else: + + try: + title = subscription.text - try: - description = subscription.description - except: - description = "" + except: + title = "" - try: - link = subscription.xmlUrl - except: - continue + try: + description = subscription.description + except: + description = "" - if None != Feed.query.filter(Feed.link == link).first(): - continue + try: + link = subscription.xmlUrl + except: + continue - try: - site_link = subscription.htmlUrl - except: - site_link = "" + if None != Feed.query.filter(Feed.link == link).first(): + continue + + try: + site_link = subscription.htmlUrl + except: + site_link = "" - new_feed = Feed(title=title, description=description, link=link, site_link=site_link, email_notification=False, enabled=True) + new_feed = Feed(title=title, description=description, link=link, site_link=site_link, email_notification=False, enabled=True) - user.feeds.append(new_feed) - nb += 1 + user.feeds.append(new_feed) + nb += 1 + return nb + nb = read(subscriptions) db.session.commit() - return nb, len(subscriptions)-nb + return nb def clean_url(url): """ @@ -232,3 +243,8 @@ def search_feed(url): #return urllib.parse.urljoin(url, feed_link['href']) return feed_link['href'] return None + + +if __name__ == "__main__": + import_opml("root@pyAggr3g470r.localhost", "./var/feeds_test.opml") + #import_opml("root@pyAggr3g470r.localhost", "./var/pyAggr3g470r.opml") \ No newline at end of file diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index 1afcecb2..a00fcb2a 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -499,9 +499,8 @@ def management(): opml_path = os.path.join("./pyaggr3g470r/var/", data.filename) data.save(opml_path) try: - nb, nb_already = utils.import_opml(g.user.email, opml_path) - flash(str(nb) + " feeds imported (" + str(nb_already) + \ - " already in the database).", "success") + nb = utils.import_opml(g.user.email, opml_path) + flash(str(nb) + " feeds imported.", "success") except Exception as e: flash("Impossible to import the new feeds.", "danger") -- cgit