Skip to content

Instantly share code, notes, and snippets.

@acagastya
Created January 15, 2020 15:39
Show Gist options
  • Save acagastya/684991cbe14db62968dce189a8c93686 to your computer and use it in GitHub Desktop.
Save acagastya/684991cbe14db62968dce189a8c93686 to your computer and use it in GitHub Desktop.
const { URL } = require('url');
import fetch from 'node-fetch';
export const shouldTransform = url => {
const { host, pathname } = new URL(url);
return (
[
'instagram.com',
'www.instagram.com',
'instagr.am',
'www.instagr.am',
].includes(host) &&
pathname.includes('/p/') &&
!pathname.endsWith('/embed')
);
};
export const getHTML = url =>
fetch(`https://api.instagram.com/oembed/?url=${url}`)
.then(r => r.json())
.then(data => {
const w = data.thumbnail_width;
const h = data.thumbnail_height;
const width = w / 2;
const height = h / 2 + 210;
const lastChar = url[url.length - 1];
const iframeUrl = url + (lastChar == '/' ? '' : '') + 'embed';
return `<iframe src="${iframeUrl}" width="${width}" height="${height}" frameborder="0"></iframe>`;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment