RSS feed for Instagram

Instagram is fucking shite. The biggest shite thing about it is that it encourages people to ruin what might otherwise be perfectly good photos with those stupid fucking filters which turn everything to shit, while fooling them into thinking they are being "creative" when in fact they are being destructive. But another giantly shit thing about it is that the only way to sign up for it is to use the fucking instagram app for a fucking mobile device. It is of course possible to take the code apart and work out what it's doing and issue the appropriate requests using wget or whatever, or to run it in an emulator, but it's still a fucking pain in the arse no matter how you do it and the restriction is an excellent example of moronic mindless stupidity on the part of the people who run instagram.

Of course in an ideal world nobody would even want to sign up to it by any method, but unfortunately some people do, and if it is necessary to follow such a person (for instance to operate a news feed) then this requires an instagram account to follow them with. The normal way to get around this sort of shite is by means of an RSS feed. Unfortunately you can't do that with instagram either. They do have an API which apparently provides RSS functions, but you can't use that without fucking signing up either. Which makes most of the answers on this Stack Overflow page, plus various other pages which attempt to address the same problem, completely bleeding useless.

This answer from jonny.milano, however, does work, because it works by retrieving the HTML for the user's page, pulling the JSON out of it, and transforming it into XML RSS format. Since you can read the user's page without fucking signing up and without pissing around spoofing HTTP requests to look like a mobile app, this is a usable method.

The only significant problem is that it requires a PHP web server to make it work. I don't care, since I have one. But even if I didn't it would only take a couple of minutes to install PHP and apache on localhost.

After a few modifications to allow for me using a different version of PHP, to make it report the URL of the actual image instead of the instagram web page with all the pointless badly-written slow-to-load bollocks on it, and add a few fancy bits, this is what I ended up with, based on jonny.milano's code.

<?php # instrss.php: RSS feed for Instagram $errtext = array( 400 => "Bad request" 500 => "Internal server error" ); function errexit($val, $err) { global $errtext; $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); header("$protocol $val $errtext[$val]"); echo "\n\n$val $errtext[$val]\n\n$err\n"; exit; } $user = isset($_GET['u']) ? strval($_GET['u']) : ''; if ($user == '') errexit(400, 'No username specified'); $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'http://your.domain'; $req = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : "/your.path/instrss.php?u=$user"; $now = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); $insturl = "$user"; $html = file_get_contents($insturl); if (!$html) errexit(500, "Failed to retrieve $insturl"); $html = strstr($html, '{"static_root'); $html = substr($html, 0, strpos($html, '</script>') - 1); $data = json_decode($html); if (!$data) errexit(500, "Failed to parse content retrieved from $insturl"); $rss = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; $rss .= "<rss version=\"2.0\" xmlns:atom=\"\">\n"; $rss .= "<channel>\n"; $rss .= " <title>Instagram feed for $user</title>\n"; $rss .= " <atom:link href=\"http://$host$req\" rel=\"self\" type=\"application/rss+xml\" />\n"; $rss .= " <link>$user</link>\n"; $rss .= " <description>Instagram feed for $user</description>\n"; foreach($data->entry_data->UserProfile[0]->userMedia as $user_media) { $imgurl = str_replace('_n.jpg', '_o.jpg', $user_media->images->standard_resolution->url); $caption = htmlspecialchars($user_media->caption ? $user_media->caption->text : '(no caption)'); $imgdate = date("r", $user_media->created_time); $guid = $user . '_' . $user_media->created_time; $rss .= " <item>\n"; $rss .= " <title>$caption</title>\n"; $rss .= " <link>$imgurl</link>\n"; $rss .= " <pubDate>$imgdate</pubDate>\n"; $rss .= " <description><![CDATA[<img src=\"$imgurl\"/>]]></description>\n"; $rss .= " <guid>$guid</guid>\n"; $rss .= " </item>\n"; } $rss .= "</channel>\n"; $rss .= "</rss>\n"; header('Content-Type: text/xml; charset=utf-8'); echo $rss; ?>

Install that on your PHP web server and use http://your.domain/your.path/instrss.php?u=instagram_username as the feed URL in your RSS reader. Works as of 2015-01-04.

Back to Pigeon's Nest

Be kind to pigeons

Valid HTML 4.01!