aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCédric Bonhomme <cedric@cedricbonhomme.org>2017-05-24 22:26:47 +0200
committerCédric Bonhomme <cedric@cedricbonhomme.org>2017-05-24 22:26:47 +0200
commit05f30c772b71fd30115ec012a3371f4478b74192 (patch)
tree78582f430ec00fbbfc59fe5164eecf04b702e007
parentA bookmarklet for bookmarks has been added. (diff)
downloadnewspipe-05f30c772b71fd30115ec012a3371f4478b74192.tar.gz
newspipe-05f30c772b71fd30115ec012a3371f4478b74192.tar.bz2
newspipe-05f30c772b71fd30115ec012a3371f4478b74192.zip
Import bookmarks from pinboard.in
-rw-r--r--src/conf/conf.cfg-sample2
-rw-r--r--src/lib/data.py31
-rw-r--r--src/web/templates/edit_bookmark.html14
-rw-r--r--src/web/views/bookmark.py15
4 files changed, 56 insertions, 6 deletions
diff --git a/src/conf/conf.cfg-sample b/src/conf/conf.cfg-sample
index b1dde26f..160c4629 100644
--- a/src/conf/conf.cfg-sample
+++ b/src/conf/conf.cfg-sample
@@ -12,7 +12,7 @@ token_validity_period = 3600
log_path = ./var/log/newspipe.log
log_level = info
[database]
-database_url = postgres://pgsqluser:pgsqlpwd@127.0.0.1:5432/aggregator
+database_url = sqlite:///newspipe.db
[crawler]
crawling_method = default
default_max_error = 6
diff --git a/src/lib/data.py b/src/lib/data.py
index d887c003..e87023c4 100644
--- a/src/lib/data.py
+++ b/src/lib/data.py
@@ -20,9 +20,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__author__ = "Cedric Bonhomme"
-__version__ = "$Revision: 0.1 $"
+__version__ = "$Revision: 0.2 $"
__date__ = "$Date: 2016/11/17 $"
-__revision__ = "$Date: 2016/11/17 $"
+__revision__ = "$Date: 2017/05/14 $"
__copyright__ = "Copyright (c) Cedric Bonhomme"
__license__ = "AGPLv3"
@@ -37,6 +37,7 @@ from flask import jsonify
from bootstrap import db
from web.models import User, Feed, Article
+from web.controllers import BookmarkController, BookmarkTagController
def import_opml(email, opml_content):
@@ -160,3 +161,29 @@ def export_json(user):
} for article in feed.articles ]
})
return jsonify(result=result)
+
+
+def import_pinboard_json(user, json_content):
+ """
+ Import bookmarks from a pinboard JSON export.
+ """
+ bookmark_contr = BookmarkController(user.id)
+ tag_contr = BookmarkTagController(user.id)
+ bookmarks = json.loads(json_content.decode("utf-8"))
+ nb_bookmarks = 0
+ for bookmark in bookmarks[:20]:
+ tags = []
+ for tag in bookmark['tags'].split(' '):
+ new_tag = tag_contr.create(text=tag.strip(), user_id=user.id)
+ tags.append(new_tag)
+ bookmark_attr = {
+ 'href': bookmark['href'],
+ 'description': bookmark['extended'],
+ 'title': bookmark['description'],
+ 'shared': [bookmark['shared']=='yes' and True or False][0],
+ 'to_read': [bookmark['toread']=='yes' and True or False][0],
+ 'tags': tags
+ }
+ new_bookmark = bookmark_contr.create(**bookmark_attr)
+ nb_bookmarks += 1
+ return nb_bookmarks
diff --git a/src/web/templates/edit_bookmark.html b/src/web/templates/edit_bookmark.html
index 573e3d7a..1c6f0c01 100644
--- a/src/web/templates/edit_bookmark.html
+++ b/src/web/templates/edit_bookmark.html
@@ -63,9 +63,17 @@
</form>
</div>
{% if action == "Add a bookmark" %}
- <div>
- <p>{{ _('You can add a bookmark with a bookmarklet. Drag the following button to your browser bookmarks.') }}</p>
- {{ _('<a class="btn btn-default" href="%(bookmarklet)s" rel="bookmark">Manage your bookmarks using Newspipe</a>', bookmarklet='javascript:window.location="%s?href="+encodeURIComponent(document.location)' % url_for('bookmark.bookmarklet', _external=True)) }}
+ <div class="row">
+ <div class="col-md-6 pull-right">
+ <p>{{ _('You can add a bookmark with a bookmarklet. Drag the following button to your browser bookmarks.') }}</p>
+ {{ _('<a class="btn btn-default" href="%(bookmarklet)s" rel="bookmark">Manage your bookmarks using Newspipe</a>', bookmarklet='javascript:window.location="%s?href="+encodeURIComponent(document.location)' % url_for('bookmark.bookmarklet', _external=True)) }}
+ </div>
+ <div class="col-md-6">
+ <form action="{{ url_for('bookmark.import_pinboard') }}" method="post" id="formImportPinboard" enctype="multipart/form-data">
+ <span class="btn btn-default btn-file">{{ _('Import bookmarks from pinboard.in') }} (<span class="text-info">*.json</span>)<input type="file" name="jsonfile" /></span>
+ <button class="btn btn-default" type="submit">OK</button>
+ </form>
+ </div>
</div>
{% endif %}
</div><!-- /.container -->
diff --git a/src/web/views/bookmark.py b/src/web/views/bookmark.py
index 546f4996..87142024 100644
--- a/src/web/views/bookmark.py
+++ b/src/web/views/bookmark.py
@@ -8,6 +8,7 @@ from flask_login import login_required, current_user
import conf
from lib.utils import redirect_url
+from lib.data import import_pinboard_json
from bootstrap import db
from web.forms import BookmarkForm
from web.controllers import BookmarkController, BookmarkTagController
@@ -124,3 +125,17 @@ def bookmarklet():
new_bookmark = bookmark_contr.create(**bookmark_attr)
flash(gettext('Bookmark successfully created.'), 'success')
return redirect(url_for('bookmark.form', bookmark_id=new_bookmark.id))
+
+@bookmark_bp.route('/import_pinboard', methods=['POST'])
+@login_required
+def import_pinboard():
+ bookmarks = request.files.get('jsonfile', None)
+ if bookmarks:
+ try:
+ nb_bookmarks = import_pinboard_json(current_user, bookmarks.read())
+ flash(gettext("%(nb_bookmarks)s bookmarks successfully imported.",
+ nb_bookmarks=nb_bookmarks), 'success')
+ except:
+ flash(gettext('Error when importing bookmarks.'), 'error')
+
+ return redirect(redirect_url())
bgstack15