Skip to content

Instantly share code, notes, and snippets.

@rexim
Last active January 11, 2024 01:40
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save rexim/8257108 to your computer and use it in GitHub Desktop.
Save rexim/8257108 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Alexey Kutepov a.k.a. rexim
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import codecs
import locale
import sys
import json
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
def export_bookmarks(bookmarks, fp):
# The inner recursive function which does the main work
def export_bookmarks_impl(bookmarks, level):
# Output the node according to its type and the current level
if bookmarks['type'] == 'text/x-moz-place-container':
fp.write('%s %s\n' % ('*' * level, bookmarks['title']))
elif bookmarks['type'] == 'text/x-moz-place':
fp.write('%s - [[%s][%s]]\n' % (' ' * (level - 1),
bookmarks['uri'],
bookmarks['title']))
if bookmarks.has_key('children'):
# Due to the nature of org-mode format, it is important to
# export not folder nodes first. So they are guaranteed to
# be children of the current node.
for child in bookmarks['children']:
if child['type'] == 'text/x-moz-place':
export_bookmarks_impl(child, level + 1)
for child in bookmarks['children']:
if child['type'] == 'text/x-moz-place-container':
export_bookmarks_impl(child, level + 1)
# Starting the traversal from level 1
export_bookmarks_impl(bookmarks, 1)
if __name__ == '__main__':
# Reading JSON from stdin
bookmarks = json.load(sys.stdin)
# Writing org-mode to stdout
export_bookmarks(bookmarks, sys.stdout)
@pauljamesharper
Copy link

How do you use the script? I'm trying "python bookmarks-to-org.py bookmarks.html" without quotes, with no result.

@rexim
Copy link
Author

rexim commented Sep 8, 2015

@gluesniffmonkey, Hello! Sorry for the late response. GitHub Gist doesn't notify me about any comments here for some reason...

I haven't used this script for a while, so I'm not even sure if it's still relevant for the latest version of Firefox. But you still can give it a try.

First of all, it works only with bookmarks that are exported from Firefox in JSON format. Not HTML as in your example. Second, this scripts reads bookmarks from the stdin and outputs org-mode document to the stdout. That means you have to do some basic shell redirection magic. Basically, I use this script like this:

$ cat bookmarks.json | python bookmarks-to-org.py > bookmarks.org

Hope it will help.

Thanks!

@PhilHudson
Copy link

This should work too:

$ python bookmarks-to-org.py < bookmarks.json > bookmarks.org

@edumerco
Copy link

Thanks Rexim, worked as a charm. 👍 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment