loading...

How to fetch your public photos from Instagram without the API

teroauralinna profile image Tero Auralinna Originally published at auralinna.blog on ・1 min read

This is a short demo about how you can fetch your Instagram photos without the usage of Instagram API, which requires a user to authenticate.

This technique is based on the <script> tag found from the Instagram profile page. Script tag has the data, which we will parse by regular expression.

const instagramRegExp = new RegExp(/<script type="text\/javascript">window\._sharedData = (.*)<\/script>/)

The following function will fetch the profile page. It parses the Instagram data to JSON and returns an array of objects holding an image page URL, image source URL, and caption.

const fetchInstagramPhotos = async (accountUrl) => {
  const response = await axios.get(accountUrl)
  const json = JSON.parse(response.data.match(instagramRegExp)[1])
  const edges = json.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges.splice(0, 8)
  const photos = edges.map(({ node }) => {
    return {
      url: `https://www.instagram.com/p/${node.shortcode}/`,
      thumbnailUrl: node.thumbnail_src,
      displayUrl: node.display_url,
      caption: node.edge_media_to_caption.edges[0].node.text
    }
  })
  return photos
}

Calling the function:

try {
  const photos = await fetchInstagramPhotos('https://www.instagram.com/sunsetwithbubbles/')
  // Do something with the photos
} catch (e) {
  console.error('Fetching Instagram photos failed', e)
}

Note that you need to wrap this into an async function.

Disclaimer: Obviously, this might break anytime if Instagram changes its source code and works only for the public profile.


This blog post was originally published on Auralinna.blog

Discussion

pic
Editor guide
Collapse
gabbersepp profile image
Josef Biehler

Thanks for this. Unfortunatelly you can not load all posts. But nice idea though!

Collapse
adncodez profile image
Adnan HT

Hey is there an easy route to tweak this an make it load more posts until you get the whole feed? Thanks in advance 😊