Skip to content

Commit

Permalink
Support instagram posts with multiple images
Browse files Browse the repository at this point in the history
  • Loading branch information
schulzetenberg committed Jan 2, 2022
1 parent d2a3e79 commit 93fa3ff
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions nodejs/instagram.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// NOTE: This only works for public instagram accounts. Also, Instagram blocks profile requests from GCP and AWS
// NOTE: This only works for public instagram accounts. Also, Instagram blocks profile requests from GCP and AWS, etc.
// Run this locally after already logged into Instagram from the browser

const { promisify } = require('util');
const cloudinary = require('cloudinary').v2;
Expand All @@ -25,18 +26,42 @@ exports.save = (userId) => {
};

function getProfile(config) {
if (!config.instagram.user) {
return Promise.reject('Missing Instagram user config');
}

return api
.get({ url: `https://www.instagram.com/${config.instagram.user}/?__a=1` })
.then((data) => {
const images = data.graphql.user.edge_owner_to_timeline_media.edges
.map(({ node }) => ({
const mediaArr = [];

data.graphql.user.edge_owner_to_timeline_media.edges.forEach(({ node }) => {
mediaArr.push({
caption: node.edge_media_to_caption.edges[0].node.text,
imgUrl: node.display_url,
postUrl: `https://www.instagram.com/p/${node.shortcode}/`,
id: node.id,
is_video: node.is_video,
}))
.filter((x) => x.is_video === false);
});

const enableSecondaryImages = true; // TODO: Can make this configurable

if (enableSecondaryImages && node.edge_sidecar_to_children && node.edge_sidecar_to_children.edges.length > 1) {
node.edge_sidecar_to_children.edges.forEach(({ node: child }, index) => {
if (index !== 0) {
mediaArr.push({
caption: node.edge_media_to_caption.edges[0].node.text,
imgUrl: child.display_url,
postUrl: `https://www.instagram.com/p/${child.shortcode}/`,
id: child.id,
is_video: child.is_video,
});
}
});
}
});

const images = mediaArr.filter((x) => x.is_video === false);

return { config, images };
})
Expand Down

0 comments on commit 93fa3ff

Please sign in to comment.