Created
August 14, 2023 21:30
-
-
Save russellbeattie/66eaade2f265841c725d121f6b398382 to your computer and use it in GitHub Desktop.
Grab bookmarks and export as JSON
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 fs from 'fs'; | |
import { execSync } from 'child_process'; | |
import { unfurl } from 'unfurl.js'; | |
let bookmarksFile = '/Users/russell/Library/Application\ Support/Google/Chrome/Default/Bookmarks'; | |
let linksFile = 'links.json'; | |
let copyCommand = 'aws s3 cp ' + linksFile + ' s3://russellbeattie.com/pages/ ; aws cloudfront create-invalidation --distribution-id E3TJ5QZUCJTWJI --paths /pages/*'; | |
fs.copyFileSync(linksFile, 'bak/' + new Date().getTime() + '.json'); | |
let bookmarksStr = fs.readFileSync(bookmarksFile).toString(); | |
let bookmarksJSON = JSON.parse(bookmarksStr); | |
let topNode = bookmarksJSON.roots.bookmark_bar; | |
let linksStr = fs.readFileSync(linksFile).toString(); | |
let linksJSON = JSON.parse(linksStr, (key, val) => { | |
return key === 'date' ? new Date(val) : val; | |
}); | |
let modified = linksJSON[0].date; | |
let bookmarks = getBookmarks(topNode, []); | |
let newLinks = await getMetadata(bookmarks); | |
newLinks.sort((a, b) => { | |
if (a.date > b.date) return -1; | |
if (a.date < b.date) return 1; | |
if (a.date == b.date) return 0; | |
}); | |
fs.writeFileSync(linksFile, JSON.stringify(newLinks.concat(linksJSON), null, ' ')); | |
console.log(copyCommand); | |
let stdout = execSync(copyCommand); | |
console.log(stdout.toString()); | |
console.log('Done'); | |
function getBookmarks(parent, links) { | |
if (parent.children) { | |
parent.children.forEach((child, index) => { | |
if (child.type == 'url' && parent.name == 'AI2') { | |
let date = new Date(Date.UTC(1601, 0, 1) + parseInt(child.date_added) / 1000); | |
links.push({ | |
date: date, | |
title: child.name, | |
url: child.url | |
}); | |
} | |
if (child.children) { | |
getBookmarks(child, links); | |
} | |
}); | |
} | |
return links; | |
} | |
async function getMetadata(bookmarks){ | |
let newLinks = []; | |
for await (let bookmark of bookmarks) { | |
if(bookmark.date > modified){ | |
console.log(bookmark.url); | |
try { | |
let result = await unfurl(bookmark.url, {timeout: 10000}); | |
if(result){ | |
let image = ''; | |
let description = result.description; | |
if(result.open_graph){ | |
if(result.open_graph.images){ | |
image = result.open_graph.images[0].url; | |
} | |
if(result.open_graph.description){ | |
description = result.open_graph.description; | |
} | |
} | |
newLinks.push({ | |
'date': bookmark.date, | |
'title': result.title || bookmark.title, | |
'url': result.canonical_url || bookmark.url, | |
'desc': description || '', | |
'image': image, | |
'favicon': result.favicon || '', | |
'origUrl': bookmark.url | |
}); | |
} else { | |
console.log('No result'); | |
newLinks.push(bookmark); | |
} | |
} catch(err){ | |
console.log('Error', err.name); | |
newLinks.push(bookmark); | |
} | |
} | |
} | |
return newLinks; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment