Skip to content

Instantly share code, notes, and snippets.

@fraga
Created November 19, 2012 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fraga/4108898 to your computer and use it in GitHub Desktop.
Save fraga/4108898 to your computer and use it in GitHub Desktop.
Get all images from instagram (api v1)
do
{
if (webRequest == null && string.IsNullOrEmpty(nextPageUrl))
webRequest = HttpWebRequest.Create(String.Format("https://api.instagram.com/v1/tags/{0}/media/recent?client_id={1}", tagName, clientId));
else
webRequest = HttpWebRequest.Create(nextPageUrl);
var responseStream = webRequest.GetResponse().GetResponseStream();
Encoding encode = System.Text.Encoding.Default;
using (StreamReader reader = new StreamReader(responseStream, encode))
{
JToken token = JObject.Parse(reader.ReadToEnd());
var pagination = token.SelectToken("pagination");
if (pagination != null && pagination.SelectToken("next_url") != null)
{
nextPageUrl = pagination.SelectToken("next_url").ToString();
}
else
{
nextPageUrl = null;
}
var images = token.SelectToken("data").ToArray();
foreach (var image in images)
{
var imageUrl = image.SelectToken("images").SelectToken("standard_resolution").SelectToken("url").ToString();
if (String.IsNullOrEmpty(imageUrl))
Console.WriteLine("broken image URL");
var imageResponse = HttpWebRequest.Create(imageUrl).GetResponse().GetResponseStream();
var imageId = image.SelectToken("id");
using(var imageWriter = new StreamWriter(String.Format("{0}\\{1}.jpg", outputDir, imageId)))
{
imageResponse.CopyTo(imageWriter.BaseStream);
imageResponse.Flush();
Console.WriteLine("copied {0}", imageId);
}
}
}
}
while (!String.IsNullOrEmpty(nextPageUrl));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment