2015-04-23 56 views
0

我有這個代碼,它從Instagram加載圖像。使用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; 
    } 

當前Instagram API給了我前20張圖片。我需要做的是加載過去30天的所有圖像。

我們該怎麼做?

回答

1

Tag Endpoint接受count參數。如果您將count參數設置爲大於33,則每次呼叫時都會返回33個圖像,並使用分頁符繼續獲取其餘圖像。

webRequest = HttpWebRequest.Create(String.Format("https://api.instagram.com/v1/tags/{0}/media/recent?access_token={1}&count=100000", strtagName, strAccessToken)); 

要加載的所有圖像最後30天,設置count到一個很高的值,並檢查每個圖像的created_time屬性來阻止你喜歡的地方。

var imageCreatedTime = image.SelectToken("created_time"); 
+0

我以某種不同的格式獲得時間。如何以日期時間格式獲取它? – Happy

+0

要將它轉換爲datetime:http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa –

+0

那麼instagram在unixtimestamp中返回? did not瞭解使用哪個示例 – Happy