Created
May 20, 2024 19:34
-
-
Save arnavpraneet/9798d6f2d33913a544dddbbc90f3df2e 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 (LinkDing, LinkAce, Briefkasten, Pocket, Readwise, Omnivore e…
This file contains hidden or 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 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 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>', | |
'<DL><p>' | |
] | |
for collection in json_data.get('collections', []): | |
for link in collection.get('links', []): | |
try: | |
raw_tags = link.get('tags', []) | |
logging.debug(f'Processing link: {link["name"]} with raw tags: {raw_tags}') | |
tags = [tag['name'] for tag in raw_tags if isinstance(tag, dict) and 'name' in tag] | |
logging.debug(f'Filtered tags: {tags}') | |
tags = ','.join(tags) | |
description = link.get('description', '') | |
add_date = date_to_timestamp(link.get('createdAt', '')) | |
last_modified = date_to_timestamp(link.get('updatedAt', '')) | |
if add_date is not None and last_modified is not None: | |
html_output.append( | |
f' <DT><A HREF="{link["url"]}" ADD_DATE="{add_date}" LAST_MODIFIED="{last_modified}" PRIVATE="1" TOREAD="1" TAGS="{tags}">{link["name"]}</A>' | |
) | |
html_output.append(f' <DD>{description}') | |
else: | |
logging.warning(f'Invalid date for link: {link["url"]}') | |
except KeyError as e: | |
logging.warning(f'Missing key in link: {e}') | |
except Exception as e: | |
logging.warning(f'Error processing link: {e}') | |
html_output.append('</DL><p>') | |
return '\n'.join(html_output) | |
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.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment