diff options
-rw-r--r-- | pyaggr3g470r/templates/management.html | 7 | ||||
-rwxr-xr-x | pyaggr3g470r/utils.py | 19 | ||||
-rw-r--r-- | pyaggr3g470r/views.py | 17 | ||||
-rw-r--r-- | requirements.txt | 1 |
4 files changed, 42 insertions, 2 deletions
diff --git a/pyaggr3g470r/templates/management.html b/pyaggr3g470r/templates/management.html index a6319b37..77cfd2bd 100644 --- a/pyaggr3g470r/templates/management.html +++ b/pyaggr3g470r/templates/management.html @@ -12,6 +12,13 @@ <p>Update your <a href="/profile/">profile</a>.</p> </div> <div class="jumbotron"> + <h1>Import feeds</h1> + <form action="" method="post" id="formImportOPML" enctype="multipart/form-data"> + <span class="btn btn-default btn-file">Import OPM<input type="file" name="opmlfile" /></span> + <button class="btn btn-default" type="submit">OK</button> + </form> + </div> + <div class="jumbotron"> <h1>Export articles</h1> <a href="/export/" class="btn btn-default">HTML</a> </div> diff --git a/pyaggr3g470r/utils.py b/pyaggr3g470r/utils.py index c214cf71..0b112615 100755 --- a/pyaggr3g470r/utils.py +++ b/pyaggr3g470r/utils.py @@ -37,6 +37,7 @@ __license__ = "GPLv3" import os import re import glob +import opml import operator import calendar @@ -46,6 +47,7 @@ from collections import Counter from contextlib import contextmanager import conf +import models # regular expression to check URL url_finders = [ \ @@ -70,6 +72,23 @@ def opened_w_error(filename, mode="r"): finally: f.close() +def import_opml(email, opml_file): + """ + Import new feeds from an OPML file. + """ + user = models.User.objects(email=email).first() + try: + subscriptions = opml.parse(opml_file) + except Exception as e: + raise e + for subscription in subscriptions: + existing_feed = [feed for feed in user.feeds if feed.link == subscription.xmlUrl] + if len(existing_feed) == 0: + new_feed = models.Feed(title=subscription.title, description=subscription.description, link=subscription.xmlUrl, \ + site_link=subscription.htmlUrl, email=False, enabled=True) + user.feeds.append(new_feed) + user.save() + def open_url(url): """ Open an URL with the proxy and the user-agent diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index 47a95d0f..c8616d60 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -26,6 +26,7 @@ __revision__ = "$Date: 2014/02/09 $" __copyright__ = "Copyright (c) Cedric Bonhomme" __license__ = "GPLv3" +import os import datetime from flask import render_template, request, make_response, flash, session, url_for, redirect, g from flask.ext.login import LoginManager, login_user, logout_user, login_required, current_user, AnonymousUserMixin @@ -329,7 +330,7 @@ def index_database(): fastsearch.create_index(user.feeds) flash('Database indexed.', 'success') return redirect(url_for('home')) - + @app.route('/export/', methods=['GET']) @login_required def export_articles(): @@ -369,12 +370,24 @@ def search(): result.append(feed) return render_template('search.html', feeds=result, nb_articles=nb_articles, query=query) -@app.route('/management/', methods=['GET']) +@app.route('/management/', methods=['GET', 'POST']) @login_required def management(): """ Display the management page. """ + if request.method == 'POST': + # Import an OPML file + data = request.files['opmlfile'] + opml_path = os.path.join("./pyaggr3g470r/var/", data.filename) + data.save(opml_path) + try: + utils.import_opml(g.user.email, opml_path) + flash("New feeds imported", "success") + except: + flash("Impossible to import the new feeds.", "danger") + + form = AddFeedForm() user = models.User.objects(email=g.user.email).first() nb_feeds = len(user.feeds) diff --git a/requirements.txt b/requirements.txt index 622dcb52..4938021d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ feedparser +opml requests whoosh BeautifulSoup |