Skip to content

Instantly share code, notes, and snippets.

@DryIce1
Forked from alangrainger/getGooglePhoto.js
Created October 3, 2022 14:46
Show Gist options
  • Save DryIce1/50a3c8bfc2c7e5860c42245b68179b0f to your computer and use it in GitHub Desktop.
Save DryIce1/50a3c8bfc2c7e5860c42245b68179b0f 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 requires you to set up a curl command in Templater, in the config section:
Example: https://i.imgur.com/IAn3JDX.png
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