Skip to content

Instantly share code, notes, and snippets.

@ckolumbus
Created December 17, 2018 22:01
Show Gist options
  • Save ckolumbus/c2c03f4759284a41f820a3ff2cddd12b to your computer and use it in GitHub Desktop.
Save ckolumbus/c2c03f4759284a41f820a3ff2cddd12b to your computer and use it in GitHub Desktop.
from enum import Enum
import json
def load_firefox_json(path, unique_tag, add_parent_folder_as_tag):
"""Open Firefox json export file and import data.
Ignore 'SmartBookmark' and 'Separator' entries.
Parameters
----------
path : str
Path to Firefox json bookmarks file.
unique_tag : str
Timestamp tag in YYYYMonDD format.
add_parent_folder_as_tag : bool
True if bookmark parent folders should be added as tags else False.
"""
""" Format
typeCode
1 : uri (type=text/x-moz-place)
2 : subfolder (type=text/x-moz-container)
3 : separator (type=text/x-moz-separator)
"""
class TypeCode(Enum):
uri = 1
folder = 2
separator = 3
def is_smart(entry):
result = False
try:
d = [ anno for anno in entry['annos'] if anno['name'] == "Places/SmartBookmark" ]
if 0 < len(d):
result = True
else:
result = False
except:
result = False
return result
def extract_desc(entry):
try:
d = [ anno for anno in entry['annos'] if anno['name'] == "bookmarkProperties/description" ]
return d[0]['value']
except:
return ""
def extract_tags(entry):
tags = []
try:
tags = i['tags'].split(',')
except:
pass
return tags
def iterate_children(parent_folder, entry_list):
for i in entry_list:
if TypeCode.uri.value == i['typeCode'] :
if is_smart(i):
continue
desc = extract_desc(i)
tags = extract_tags(i)
print('1', parent_folder, i['title'], i['uri'], tags, desc)
elif TypeCode.folder.value == i['typeCode'] :
try:
iterate_children(i['title'], i['children'])
except:
pass
elif TypeCode.separator.value == i['typeCode'] :
pass
with open(path, 'r') as datafile:
data = json.load(datafile)
iterate_children(data['title'], data['children'])
if __name__ == '__main__':
load_firefox_json("b.json", False, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment