Shell Script to Download Photos Directly from Instagram

If there’s one thing I absolutely fucking hate, it’s modern web interfaces. It used to be that if you wanted to download an image from the Internet, all you had to do was right-click and then select “Save As” from the drop-down menu. Now all these photo upload websites have their own special interfaces that you have to go through that aren’t intuitive at all. The default interface no longer works, because the controls have all been hijacked by the Javascript and twisted according to the whims of the website’s designers in a misguided effort to look chic and high-tech. Sometimes the website won’t even let you download an image at all; it completely hides that functionality from you. There’s no longer a uniform interface for doing anything on the web.

Well, some time ago as I was sitting at my computer ogling thots on Instagram, I ran into this problem, and I decided to find a way around it. After combing through Instagram’s entire interface for a way to download photos directly and finding none, I opened the page source and started doing a little reverse engineering. I determined the exact location within the file at which the image URL was stored, and I got to work writing the following script which I call Instaget (keep in mind this was nearly two years ago and Instagram may have been revamped since then, so it might not actually work anymore)…


 1 #!/usr/bin/env bash
 2 # Argument: Instagram URL
 3 
 4 # Get HTML file
 5 url=`echo "$1" | sed "s/\?.*$//"`
 6 wget "$url"
 7 # Search file for image URL
 8 imgurl=`grep "jpg" "index.html" | head -1 | sed "s/^.*content=\"//" | sed "s/\".*$//"`
 9 # Get image
10 wget "$imgurl"
11 # Cleanup
12 rm "index.html"
13 unset url imgurl

This script is fairly straightforward, with only a couple parts that might warrant explanation. The script downloads the HTML file and operates on it locally, just because it’s easier that way. The sed command in Line 5 is there to remove the query string from the URL, just in case it causes problems (can’t hurt to make sure). Line 8 finds the image URL by extracting it from the first line in the HTML file that has a JPEG filename. This is the location of the image URL, and its form is content="filename.jpg". With that in mind, the two remaining sed commands chop off everything before and after the image URL. After that, we can simply download it directly from Instagram with wget.

The shortcoming of this script is of course that it only works on Instagram, not to mention if Instagram’s code is ever changed significantly, it could break the script. It might be a fun afternoon project to also write versions of the script for Photobucket, Pinterest, and any other image hosting sites with annoying unintuitive interfaces.

4 thoughts on “Shell Script to Download Photos Directly from Instagram

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s