-
-
Save jasondavis/89149123f3846a07f5b98aea85280ab6 to your computer and use it in GitHub Desktop.
Insert a personal Google Photo into an Obsidian entry, either as a link, a remote image, or a locally stored thumbnail
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
/* | |
This requires you to set up a curl command in Templater: | |
Function name: | |
curl | |
System command: | |
curl -o %templater_local% %templater_remote% | |
*/ | |
// Thumbnail dimensions in pixels. The image will scale to fit within these | |
// dimensions while keeping the original aspect ratio. | |
const WIDTH = 350 | |
const HEIGHT = 250 | |
async function main(tp) { | |
const link = await tp.system.prompt('Enter the Google Photos share link') | |
const types = { | |
link: 'As a link', | |
remote: 'As an image (remote)', | |
local: 'As an image (saved locally)' | |
} | |
if (link && link.startsWith('https://photos.app.goo.gl/')) { | |
const type = await tp.system.suggester(Object.values(types), Object.keys(types)) | |
if (type === 'link') { | |
return `[Link](${link})` | |
} else { | |
// Fetch the actual photo image URL from the provided Google Photos link | |
try { | |
const raw = await requestUrl({ url: link }) | |
const html = raw.text | |
const image = html.match(/<img[^>]*?src="(https:[^"]+?)=w\d+-h\d+-no"[^>]*?alt=""[^>]*?>/s)[1] | |
if (type === 'remote') { | |
return `[![](${image}=w${WIDTH}-h${HEIGHT}-no)](${link})` | |
} else if (type === 'local') { | |
// Download the image via Curl and save to the current entry folder | |
const folder = tp.file.path().replace(/\\+/g, '/').replace(/[^/]+$/, '') | |
const filename = moment().format('YYYY-MM-DD') + '_google-photo_' + moment().format('HHmmss') + '.jpg' | |
await tp.user.curl({ | |
templater_local: `"${folder}${filename}"`, | |
templater_remote: `"${image}=w${WIDTH}-h${HEIGHT}-no"` | |
}) | |
return `[![](${filename})](${link})` | |
} | |
} catch (e) { | |
// do nothing | |
console.log(e) | |
} | |
} | |
} | |
return '' | |
} | |
module.exports = main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment