Skip to content

Instantly share code, notes, and snippets.

@jmewes
Last active December 4, 2016 15:09
Show Gist options
  • Save jmewes/4cf18e12a27ced7e65598d885be25571 to your computer and use it in GitHub Desktop.
Save jmewes/4cf18e12a27ced7e65598d885be25571 to your computer and use it in GitHub Desktop.
Transform bookmarks from a Firefox backup into an ORG document.
#!/usr/bin/python
import argparse
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
parser = argparse.ArgumentParser(description='Transform bookmarks from a Firefox backup into an ORG document.')
parser.add_argument('--input', required=True)
parser.add_argument('--output', required=True)
args = parser.parse_args()
def get_file_contents(file_name):
content = ""
with open(file_name) as f:
lines = f.readlines()
for line in lines:
content += line
return content
bookmarks_dict = {}
bookmarks_dict['untagged'] = []
bookmarks_root = json.loads(get_file_contents(args.input))
bookmarks_menu = bookmarks_root['children'][0]['children']
for bookmark in bookmarks_menu:
if not 'children' in bookmark:
if 'tags' in bookmark:
tags = bookmark['tags']
if tags in bookmarks_dict:
bookmarks_dict[tags].append(bookmark)
else:
bookmarks_dict[tags] = [bookmark]
else:
bookmarks_dict['untagged'].append(bookmark)
f = open(args.output,'w')
for tag in bookmarks_dict.keys():
f.write("\n\n* %s" % tag)
for bookmark in bookmarks_dict[tag]:
if 'title' in bookmark:
f.write("\n- [[%s][%s]]" % (bookmark['uri'], bookmark['title']))
else:
pass
# {u'index': 9, u'dateAdded': 1463211349164000, u'lastModified': 1463211349164000, u'guid': u'n7KHcAisXB2-', u'type': u'text/x-moz-place-separator', u'id': 653}
f.close()
MIT License Copyright (c) 2016 Jan Mewes
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment