Created
November 19, 2012 04:10
-
-
Save fraga/4108898 to your computer and use it in GitHub Desktop.
Get all images from instagram (api v1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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