java - Grabbing tagged instagram photos in real time -


i'm trying download photos posted specific tag in real time. found real time api pretty useless i'm using long polling strategy. below pseudocode comments of sublte bugs in it

newmediacount = getmediacount(); delta = newmediacount - mediacount; if (delta > 0) {     // if mediacount changed now, realdelta > delta, realdelta - delta photos won't grabbed , on next poll if mediacount didn't change again realdelta - delta duplicated else ...     // if photo posted private account last photo duplicated counter changes nothing added recent     recentmedia = getrecentmedia(delta);     // persist recentmedia     mediacount = newmediacount; } 

second issue can addressed set of sort gueess. first bothers me. i've moved 2 calls instagram api close possible enough?

edit

as amir suggested i've rewritten code use of min/max_tag_ids. still skips photos. couldn't find better way test save images on disk time , compare result instagram.com/explore/tags/.

public class lousyinstagramapitest {     @test     public void testfeedcontinuity() throws exception {         instagram instagram = new instagram(settings.getclientid());         final string tag_name = "portrait";         string id = instagram.getrecentmediatags(tag_name).getpagination().getmintagid();         hashtagendpoint endpoint = new hashtagendpoint(instagram, tag_name, id);          (int = 0; < 10; i++) {             thread.sleep(3000);             endpoint.recentfeed().foreach(d -> {                 try {                     url url = new url(d.getimages().getlowresolution().getimageurl());                     bufferedimage img = imageio.read(url);                     imageio.write(img, "png", new file("d:\\tmp\\" + d.getid() + ".png"));                 } catch (exception e) {                     e.printstacktrace();                 }             });         }     } }  class hashtagendpoint {     private final instagram instagram;     private final string hashtag;     private string mintagid;      public hashtagendpoint(instagram instagram, string hashtag, string mintagid) {         this.instagram = instagram;         this.hashtag = hashtag;         this.mintagid = mintagid;     }      public list<mediafeeddata> recentfeed() throws instagramexception {         tagmediafeed feed = instagram.getrecentmediatags(hashtag, mintagid, null);         list<mediafeeddata> datalist = feed.getdata();         if (datalist.size() == 0) return collections.emptylist();          string maxtagid = feed.getpagination().getnextmaxtagid();         if (maxtagid != null && maxtagid.compareto(mintagid) > 0) datalist.addall(paginatefeed(maxtagid));         collections.reverse(datalist); //        datalist.removeif(d -> d.getid().compareto(mintagid) < 0);          mintagid = feed.getpagination().getmintagid();         return datalist;     }      private collection<? extends mediafeeddata> paginatefeed(string maxtagid) throws instagramexception {         system.out.println("pagination required");          list<mediafeeddata> datalist = new arraylist<>();         {             tagmediafeed feed = instagram.getrecentmediatags(hashtag, null, maxtagid);             maxtagid = feed.getpagination().getnextmaxtagid();             datalist.addall(feed.getdata());         } while (maxtagid.compareto(mintagid) > 0);         return datalist;     }  } 

using tag endpoints recent media desired tag, returns min_tag_id in pagination info, tied tagged media @ time of call. api accepts min_tag_id parameter, can pass number last query receive media tagged after last query.

so based on whatever polling mechanism have, call api new recent media if based on last received min_tag_id.

you need pass large count parameter , follow pagination of response receive data without losing when speed of tagging faster polling.

update:
based on updated code:

public list<mediafeeddata> recentfeed() throws instagramexception {     tagmediafeed feed = instagram.getrecentmediatags(hashtag, mintagid, null, 100000);     list<mediafeeddata> datalist = feed.getdata();     if (datalist.size() == 0) return collections.emptylist();      // follow pagination     mediafeed recentmedianextpage = instagram.getrecentmedianextpage(feed.getpagination());     while (recentmedianextpage.getpagination() != null) {         datalist.addall(recentmedianextpage.getdata());         recentmedianextpage = instagram.getrecentmedianextpage(recentmedianextpage.getpagination());     }      collections.reverse(datalist);      mintagid = feed.getpagination().getmintagid();     return datalist; } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -