diff options
author | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-05-27 18:22:55 +0200 |
---|---|---|
committer | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-05-27 18:22:55 +0200 |
commit | a83a5447f67fc0eae8cf44a19d97de2c0f2ace45 (patch) | |
tree | c2b72711df6ee5f5a6aa15c619798348e9a12e5b | |
parent | Updated JSON export. (diff) | |
download | newspipe-a83a5447f67fc0eae8cf44a19d97de2c0f2ace45.tar.gz newspipe-a83a5447f67fc0eae8cf44a19d97de2c0f2ace45.tar.bz2 newspipe-a83a5447f67fc0eae8cf44a19d97de2c0f2ace45.zip |
Updated JSON export.
-rw-r--r-- | pyaggr3g470r/__init__.py | 2 | ||||
-rw-r--r-- | pyaggr3g470r/export.py | 6 | ||||
-rw-r--r-- | pyaggr3g470r/templates/management.html | 2 | ||||
-rwxr-xr-x | pyaggr3g470r/utils.py | 40 | ||||
-rw-r--r-- | pyaggr3g470r/views.py | 10 |
5 files changed, 48 insertions, 12 deletions
diff --git a/pyaggr3g470r/__init__.py b/pyaggr3g470r/__init__.py index fd08ffc6..9dae02cb 100644 --- a/pyaggr3g470r/__init__.py +++ b/pyaggr3g470r/__init__.py @@ -23,7 +23,7 @@ app.config['RECAPTCHA_USE_SSL'] = True app.config['RECAPTCHA_PUBLIC_KEY'] = conf.RECAPTCHA_PUBLIC_KEY app.config['RECAPTCHA_PRIVATE_KEY'] = conf.RECAPTCHA_PRIVATE_KEY -ALLOWED_EXTENSIONS = set(['xml', 'opml']) +ALLOWED_EXTENSIONS = set(['xml', 'opml', 'json']) def allowed_file(filename): """ diff --git a/pyaggr3g470r/export.py b/pyaggr3g470r/export.py index 812739f2..1341e58f 100644 --- a/pyaggr3g470r/export.py +++ b/pyaggr3g470r/export.py @@ -214,15 +214,15 @@ def export_json(user): "site_link": feed.site_link, "email_notification": feed.email_notification, "enabled": feed.enabled, - "created_date": feed.created_date, + "created_date": feed.created_date.strftime('%s'), "articles": [ { "title": article.title, "link": article.link, "content": article.content, "readed": article.readed, "like": article.like, - "date": article.date, - "retrieved_date": article.retrieved_date + "date": article.date.strftime('%s'), + "retrieved_date": article.retrieved_date.strftime('%s') } for article in feed.articles ] diff --git a/pyaggr3g470r/templates/management.html b/pyaggr3g470r/templates/management.html index afdad9b6..8cadeb3d 100644 --- a/pyaggr3g470r/templates/management.html +++ b/pyaggr3g470r/templates/management.html @@ -37,7 +37,7 @@ <a href="/export_opml/" class="btn btn-default">{{ _('Export feeds to OPML') }}</a> <h1>{{ _('Data liberation') }}</h1> <form action="" method="post" id="formImportJSON" enctype="multipart/form-data"> - <span class="btn btn-default btn-file">{{ _('Import account') }} (<span class="text-info">*.xml {{ _('or') }} *.opml</span>)<input type="file" name="jsonfile" /></span> + <span class="btn btn-default btn-file">{{ _('Import account') }} (<span class="text-info">*.jsonl</span>)<input type="file" name="jsonfile" /></span> <button class="btn btn-default" type="submit">OK</button> </form> <br /> diff --git a/pyaggr3g470r/utils.py b/pyaggr3g470r/utils.py index 6e23e12c..22b0aa08 100755 --- a/pyaggr3g470r/utils.py +++ b/pyaggr3g470r/utils.py @@ -37,6 +37,8 @@ __license__ = "AGPLv3" import re import glob import opml +import json +import datetime import operator from urllib import urlencode from urlparse import urlparse, parse_qs, urlunparse @@ -47,7 +49,7 @@ from contextlib import contextmanager import conf from pyaggr3g470r import db -from pyaggr3g470r.models import User, Feed +from pyaggr3g470r.models import User, Feed, Article # regular expression to check URL url_finders = [ @@ -110,7 +112,7 @@ def import_opml(email, opml_file): except: continue - if None != Feed.query.filter(Feed.link == link).first(): + if None != Feed.query.filter(Feed.user_id == user.id, Feed.link == link).first(): continue try: @@ -135,6 +137,40 @@ def import_json(email, json_file): Import an account from a JSON file. """ user = User.query.filter(User.email == email).first() + json_string = "" + with open(json_file, "r") as account: + json_string = account.read() + json_account = json.loads(json_string) + nb_feeds, nb_articles = 0, 0 + + for feed in json_account["result"]: + + if None != Feed.query.filter(Feed.user_id == user.id, Feed.link == feed["link"]).first(): + continue + + new_feed = Feed(title=feed["title"], description="", link=feed["link"], \ + site_link=feed["site_link"], email_notification=feed["email_notification"], \ + created_date=datetime.datetime.fromtimestamp(int(feed["created_date"])), + enabled=feed["enabled"]) + user.feeds.append(new_feed) + nb_feeds += 1 + db.session.commit() + + for feed in json_account["result"]: + user_feed = Feed.query.filter(Feed.user_id == user.id, Feed.link == feed["link"]).first() + if None != user_feed: + for article in feed["articles"]: + new_article = Article(link=article["link"], title=article["title"], \ + content=article["content"], readed=article["readed"], like=article["like"], \ + retrieved_date=datetime.datetime.fromtimestamp(int(article["retrieved_date"])), + date=datetime.datetime.fromtimestamp(int(article["date"])), + user_id=user.id, feed_id=user_feed.id) + + user_feed.articles.append(new_article) + nb_articles += 1 + db.session.commit() + + return nb_feeds, nb_articles def clean_url(url): diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index e4fa9963..289764a3 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -491,7 +491,7 @@ def export_articles(): return redirect(redirect_url()) response = make_response(json_result) response.mimetype = 'application/json' - response.headers["Content-Disposition"] = 'attachment; filename=articles.json' + response.headers["Content-Disposition"] = 'attachment; filename=account.json' else: flash(gettext('Export format not supported.'), 'warning') return redirect(redirect_url()) @@ -573,7 +573,7 @@ def management(): json_path = os.path.join("./pyaggr3g470r/var/", data.filename) data.save(json_path) try: - utils.import_json(g.user.email, json_path) + nb = utils.import_json(g.user.email, json_path) flash(gettext('Account imported.'), "success") except: flash(gettext("Impossible to import the account."), "danger") @@ -582,11 +582,11 @@ def management(): form = AddFeedForm() - user = User.query.filter(User.id == g.user.id).first() - nb_feeds = len(user.feeds.all()) + #user = User.query.filter(User.id == g.user.id).first() + nb_feeds = len(g.user.feeds.all()) nb_articles = len(Article.query.filter(Article.user_id == g.user.id).all()) nb_unread_articles = len(Article.query.filter(Article.user_id == g.user.id, Article.readed == False).all()) - return render_template('management.html', user=user, form=form, \ + return render_template('management.html', user=g.user, form=form, \ nb_feeds=nb_feeds, nb_articles=nb_articles, nb_unread_articles=nb_unread_articles, \ not_on_heroku = not conf.ON_HEROKU) |