Created
November 8, 2023 18:02
-
-
Save vascobrown/479bb47d3f0138a3f595143d93afa658 to your computer and use it in GitHub Desktop.
Takes a JSON file exported from the Goodlinks read-it-later app (iPhone/macOS) and creates a CSV file suitable for import into Omnivore (multiplatform).
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 | |
import csv | |
import datetime | |
def main(): | |
input_filename = 'GoodLinks-Export.json' | |
output_filename = 'converted-bookmarks.csv' | |
if os.path.isfile(input_filename): | |
if os.path.isfile(output_filename): | |
print(f'Error: A file named "{output_filename}" already exists in this folder. Aborting conversion.') | |
else: | |
with open(input_filename, 'rt') as json_file: | |
bookmarks_json = json.load(json_file) | |
num_of_bookmarks = 0 | |
fieldnames = ['url', 'state', 'labels', 'saved_at', 'published_at'] | |
with open(output_filename, 'w', newline='') as csvfile: | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for bookmark in bookmarks_json: | |
converted_bookmark = convert(bookmark) | |
writer.writerow(converted_bookmark) | |
num_of_bookmarks += 1 | |
print(f'Wrote {num_of_bookmarks} bookmarks to "{output_filename}".') | |
else: | |
print(f'Error: File "{input_filename}" not found in this folder. Unable to start bookmark conversion.') | |
def convert(bookmark): | |
import_label = "imported-from-Goodlinks" | |
converted_bookmark = { | |
'url': bookmark.get('url', ''), | |
'labels': ','.join(bookmark.get('tags', [])) + ',' + import_label if bookmark.get('tags') else import_label, | |
'saved_at': convert_to_unix_timestamp_milliseconds(bookmark.get('addedAt', 0)), | |
} | |
if bookmark.get('starred', False): | |
starred_label = '.favorites' | |
converted_bookmark['labels'] += (',' + starred_label) if bookmark.get('tags') else starred_label | |
return converted_bookmark | |
def convert_to_unix_timestamp_milliseconds(timestamp_with_fractional_seconds): | |
timestamp_float = float(timestamp_with_fractional_seconds) | |
seconds = int(timestamp_float) | |
microseconds = int((timestamp_float - seconds) * 1000000) | |
dt_object = datetime.datetime.fromtimestamp(seconds) | |
dt_object_microseconds = dt_object.replace(microsecond=microseconds) | |
unix_timestamp_milliseconds = int(dt_object_microseconds.timestamp() * 1000) | |
return unix_timestamp_milliseconds | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment