Reputation: 1945
I have this code which loads images from Instagram.
public string giveInstagramImage()
{
string strtagName = "Snowy";
string strAccessToken = "<<REDACTED>>";
string nextPageUrl = null;
string imageUrl = null;
do
{
WebRequest webRequest = null;
if (webRequest == null && string.IsNullOrEmpty(nextPageUrl))
webRequest = HttpWebRequest.Create(String.Format("https://api.instagram.com/v1/tags/{0}/media/recent?access_token={1}", strtagName, strAccessToken));
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)
{
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");
return imageUrl;
}
}
}
while (!String.IsNullOrEmpty(nextPageUrl));
return imageUrl;
}
Currently Instagram API gives me the top 20 images. What I need to do is load all images from the last 30 days.
How can we do it?
Upvotes: 0
Views: 295
Reputation: 1590
The Tag Endpoint accepts a count
parameter. If you set the count
parameter greater than 33, it will return 33 images on every call and using the pagination you can continue to get the rest of the images.
webRequest = HttpWebRequest.Create(String.Format("https://api.instagram.com/v1/tags/{0}/media/recent?access_token={1}&count=100000", strtagName, strAccessToken));
To load all images for the last 30 days, set count
to a very high value and check created_time
property of each image to stop wherever you like.
var imageCreatedTime = image.SelectToken("created_time");
Upvotes: 1