Skip to content

Instantly share code, notes, and snippets.

@link0ff
Last active November 28, 2021 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save link0ff/3d12ea9517c716609b5de7c60a8ae07e to your computer and use it in GitHub Desktop.
Save link0ff/3d12ea9517c716609b5de7c60a8ae07e to your computer and use it in GitHub Desktop.
Convert Firefox Bookmarks JSON export files to Org-mode format
#!/usr/bin/env ruby
# firefox_bookmarks_json_to_org.rb
# convert JSON files exported from Firefox Bookmarks Backup
# to https://orgmode.org/ format
#
# Copyright (C) 2021 Juri Linkov <juri@linkov.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Usage: ruby firefox_bookmarks_json_to_org.rb bookmarks-date.json > bookmarks-date.org
# where bookmarks-date.json is exported from Firefox by:
# 1. Type Control-Shift-O
# 2. Click "Import and Backup -> Backup..." and "Save"
# It's useful first to export JSON to YAML with:
# ruby -rjson -ryaml -e 'puts(JSON.load(ARGF).to_yaml)' bookmarks-date.json > bookmarks-date.yaml
# to inspect the output, if you need to export more data to org-mode format.
require 'json'
require 'cgi'
json = JSON.parse(ARGF.read, object_class: OpenStruct)
def process(json, level)
case json.typeCode
when 2 # type: text/x-moz-place-container
puts "#{'*' * (level < 1 ? 1 : level)} #{json.title}"
print " [#{Time.at(0, json.dateAdded / 1000.0, :millisecond)}]"
print " [#{Time.at(0, json.lastModified / 1000.0, :millisecond)}]"
puts
json.children.sort_by{|j| [j.typeCode == 2 ? 1 : 0, j.index]}.each do |child|
process(child, level + 1)
end if json.children
when 1 # type: text/x-moz-place
puts "- #{json.title}"
puts " #{begin CGI.unescape(json.uri).gsub(/ /, '%20') rescue json.uri end}"
print " [#{Time.at(0, json.dateAdded / 1000.0, :millisecond)}]"
print " [#{Time.at(0, json.lastModified / 1000.0, :millisecond)}]"
puts
when 3 # type: text/x-moz-place-separator
puts " "
else
STDERR.puts "! Wrong typeCode: #{json.typeCode}"
end
end
process(json, -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment