Creates a Netscape bookmark HTML file from a JSON file exported from the app Goodlinks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
def main(): | |
""" | |
Creates a Netscape bookmark HTML file from a JSON file exported from the | |
app Goodlinks (www.goodlinks.app) | |
""" | |
input_filename = 'GoodLinks-Export.json' | |
output_filename = 'converted-bookmarks.html' | |
num_of_bookmarks = 0 | |
if os.path.isfile(input_filename): | |
if os.path.isfile(output_filename): | |
print('Error: A file named "' + output_filename + '" already exists in this folder. Aborting conversion.') | |
else: | |
json_file = open(input_filename, 'rt') | |
bookmarks_json = json.load(json_file) | |
output_text = '' | |
for bookmark in bookmarks_json: | |
output_text += convert(bookmark) + '\n\n' | |
num_of_bookmarks += 1 | |
output_text = header() + output_text + footer() | |
output_file = open(output_filename, 'x', encoding='UTF-8') | |
output_file.write(output_text) | |
output_file.close() | |
print('Wrote ' + str(num_of_bookmarks) + ' bookmarks to "' + output_filename + '".') | |
else: | |
print('Error: File "' + input_filename + ' " not found in this folder. Unable to start bookmark conversion.') | |
def convert(_bookmark): | |
""" | |
Converts the bookmark JSON object into a Netscape bookmark and puts it in a | |
string | |
:param _bookmark: The bookmark JSON object | |
:return: string with the bookmark's details marked up in HTML <DT> and <DD> | |
tags | |
""" | |
converted_bookmark = { | |
'addedAt': '', | |
'tags': '', | |
'title': '', | |
'starred': '', | |
'url': '', | |
'summary':'' | |
} | |
for key in _bookmark: | |
value = _bookmark[key] | |
converted_bookmark[key] = format(value, key) | |
# Since it not a standard part of the Netscape bookmark file format, | |
# if the bookmark is starred, add a '.favorite' tag instead | |
if key == 'starred' and value == True: | |
converted_bookmark['tags'] = converted_bookmark['tags'].removesuffix('"') | |
converted_bookmark['tags'] += ',' + '.favorite' + '"' | |
dt = '<DT><A ' + converted_bookmark['url'] + ' ' + converted_bookmark['addedAt'] + ' PRIVATE="0" ' + converted_bookmark['tags'] + '>' + converted_bookmark['title'] + '</A>' | |
dd = '<DD>' + converted_bookmark['summary'] | |
return ' ' + dt + '\n' + ' ' + dd | |
def format(_value, _key): | |
""" | |
Formats the provided JSON object's key-value pair as an HTML attribute and | |
puts it in a string | |
:param _key: The key of the JSON object | |
:param _value: The value of the JSON object | |
:return: string formatted as an HTML attribute or plain text as appropriate | |
""" | |
result = '' | |
if _key == 'addedAt': | |
result = 'ADD_DATE="' + str(_value) + '"' | |
elif _key == 'tags': | |
result = 'TAGS="' | |
for item in _value: | |
result += str(item) | |
if str(item) != _value[len(_value)-1]: | |
result += ',' | |
result += '"' | |
elif _key == 'url': | |
result = 'HREF="' + str(_value) + '"' | |
else: | |
result = str(_value) | |
return result | |
def header(): | |
""" | |
Returns Netscape bookmark file header HTML | |
:return: string with Netscape bookmark header information formatted in HTML | |
""" | |
return '<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n<TITLE>Bookmarks</TITLE>\n<H1>Bookmarks</H1>\n\n<DL><p>\n\n' | |
def footer(): | |
""" | |
Returns closing HTML tags for a Netscape bookmark file | |
:return: string with closing <DL> and <p> tags | |
""" | |
return '</DL></p>' | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much!