Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save osorionicolas/bac8fb3f0e3cdf38107a803ba7606035 to your computer and use it in GitHub Desktop.

Select an option

Save osorionicolas/bac8fb3f0e3cdf38107a803ba7606035 to your computer and use it in GitHub Desktop.
A simple convertor in Python to take the backup.json file that is supplied by LinkWarden on clicking export or through the migration API and convert it into the Netscape bookmark HTML format which is supported by most other major bookmarking, read-it-later and link-saving applications
import json
import logging
from datetime import datetime
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s')
def date_to_timestamp(date_str):
try:
dt = datetime.fromisoformat(date_str.replace('Z', '+00:00'))
return int(dt.timestamp())
except ValueError as e:
logging.warning(f'Error converting date: {e}')
return None
def build_collection_tree(collections):
# Create a dictionary mapping collection IDs to collections with empty children arrays
id_to_collection = {col['id']: {**col, 'children': []} for col in collections}
root_collections = []
# First pass: add collections to their parent's children array
for col in id_to_collection.values():
parent_id = col.get('parentId')
if parent_id and parent_id in id_to_collection:
id_to_collection[parent_id]['children'].append(col)
else:
root_collections.append(col)
return root_collections
def render_collection(collection):
html = []
html.append(f'<DT><H3>{collection["name"]}</H3>')
html.append('<DL><p>')
# Add links
for link in collection.get('links', []):
try:
html.append(
f' <DT><A HREF="{link["url"]}">{link["name"]}</A>'
)
except KeyError as e:
logging.warning(f'Missing key in link: {e}')
except Exception as e:
logging.warning(f'Error processing link: {e}')
# Recursively add children collections
for child in collection.get('children', []):
html.append(render_collection(child))
html.append('</DL><p>')
return '\n'.join(html)
def json_to_netscape_html(json_data):
html_output = [
'<!DOCTYPE NETSCAPE-Bookmark-file-1>',
'<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">',
'<TITLE>Bookmarks</TITLE>',
'<H1>Bookmarks</H1>'
]
collections = json_data.get('collections', [])
tree = build_collection_tree(collections)
for collection in tree:
html_output.append(render_collection(collection))
return '\n'.join(html_output)
# Main
with open('backup.json', 'r', encoding='utf-8') as file:
json_data = json.load(file)
html_content = json_to_netscape_html(json_data)
with open('bookmarks.html', 'w', encoding='utf-8') as file:
file.write(html_content)
print("NetScape HTML file generated successfully.")
@Howdoesthisworkplease
Copy link

How do i use this together with my backup file? I have no idea sorry noob here

@osorionicolas
Copy link
Author

How do i use this together with my backup file? I have no idea sorry noob here

Pre-requisites: Python

You have to export the sites from linkwarden, that will generate a backup.json. In the same directory you have to create this LinkWardenExportjson2htmlconverter.py file. Afterwards you have to run in the command line "python LinkWardenExportjson2htmlconverter.py" in the directory that you have both files. If python is not found try "python3 LinkWardenExportjson2htmlconverter.py"

@Howdoesthisworkplease
Copy link

How do i use this together with my backup file? I have no idea sorry noob here

Pre-requisites: Python

You have to export the sites from linkwarden, that will generate a backup.json. In the same directory you have to create this LinkWardenExportjson2htmlconverter.py file. Afterwards you have to run in the command line "python LinkWardenExportjson2htmlconverter.py" in the directory that you have both files. If python is not found try "python3 LinkWardenExportjson2htmlconverter.py"

Thank you so much for your reply!

I do have Python installed. However, I am not able to copy both files into the folder where pyton is installed / running (WIN11) . The problem is that the directory (C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.2544.0_x64__qbz5n2kfra8p0\python3.13.exe) is locked. So when I copy/paste your script into python I always get the following result:

Traceback (most recent call last):
File "", line 70, in
with open('backup.json', 'r', encoding='utf-8') as file:
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'backup.json'

Tried to point it to the directory (in my case both your file and the backup.json are located in 1 folder) by changing the path

Main

with open('backup.json', 'r', encoding='utf-8') as file:
json_data = json.load(open("C:\Users\User\Desktop\Linkwarden bookmarks")))

but get error when I hit enter :o(

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