Getting images from Instagram

Cuz custom Christmas presents call for it

Posted by Janne Cederberg on Dec. 23, 2017
Reading time: approx. 2 minute(s)

Wanting to make a family video for Christmas I found myself wanting to extract images from my own Instagram account. Here’s quick notes (for myself for future reference) on how I did it. This is not really a tutorial, a technical skill is expected nor do I think this is the best possible solution available. It’s just the fastest reasonable approach I came up with:

  1. Log into Instagram on the browser (only needed if the images you want are non-public).
  2. Browse to the profile/tag you want to extract images from.
  3. Open Developer Tools (F12 on Windows and Linux, Cmd+I on Mac, I guess).
  4. Go to Console view in the Developer Tools.
  5. Run the code under heading Code A until the images you want are loaded as top-most images (in case they were not the latest images).
    • Instagram loads images in 12 image batches
    • After the first 12 Instagram will automatically load more if you keep scrolling down but you want to get to where the images you want are at the top
  6. Once you’ve got the first images you want to extract as the ones at the top, start running under heading Code B and copy-paste the output from the Developer Tools’ Console view to a text editor, notepad etc.
  7. Once you’ve completed the list in the notepad application, remove unnecessary debugging headers (such as VM123:1) preferrably using regular expressions and then save the listing as plaintext.
  8. Run wget -i list.txt where list.txt is the name of the file you created in step 7.

Code A

const USERNAME = 'username';
location.href = `https://www.instagram.com/${USERNAME}/?max_id=${_sharedData.entry_data.ProfilePage[0].user.media.page_info.end_cursor}`;

Code B

const USERNAME = 'username' // set this
const DELAY    = 5 // seconds before moving to next part in pagination

for (let n of _sharedData.entry_data.ProfilePage[0].user.media.nodes) {
	if (n.is_video) { // video
		let code = n.code
		fetch(`https://www.instagram.com/p/${code}/?__a=1`, {credentials: 'same-origin'}).then(function (response) {
			return response.json()
		}).then(function(data) {
			console.log(data.graphql.shortcode_media.video_url)
		})
	} else if (n.__typename === 'GraphSidecar') { // multiple images in a single post
		let code = n.code
		fetch(`https://www.instagram.com/p/${code}/?__a=1`, {credentials: 'same-origin'}).then(function (response) {
			return response.json()
		}).then(function (data) {
			for(let edges of data.graphql.shortcode_media.edge_sidecar_to_children.edges) {
				console.log(edges.node.display_url)
			}
		})
	} else {
		console.log(n.thumbnail_src.replace(/\/s[0-9]{3}x[0-9]{3}\//, '/s1440x1440/'))
	}
}

setTimeout(function() {
	let end_cursor = _sharedData.entry_data.ProfilePage[0].user.media.page_info.end_cursor
	location.href = `https://www.instagram.com/${USERNAME}/?max_id=${end_cursor}`
}, DELAY*1000)

So now you’ve got your images on your computer and you can create collage pictures, videos or whatever you come up with from them.

Known limitations

  • Currently if there are multiple videos in a single post (can there be?), then this will not get their URLs
comments powered by Disqus