Instagram realtime get post from callback

2020-06-12 03:40发布

Right, this is really working on my nerves, but Instagram has to do something about their bloody documentation.

I am already trying for a week to live update my website with new instagram posts without refreshing the page. Twitter was fairly easy, but instagram is just one big mess. Basically I use the realtime Instagram API, the callback and all that stuff is working fine, but thanks to Instagram it does not return me an ID from the post that is new, the callback only returns some basic stuff:

[{"changed_aspect": "media", "object": "tag", "object_id": "nofilter", "time": 1391091743, "subscription_id": xxxxx, "data": {}}]

with this data you are nothing, except for the Tag, but I knew the tag before this callback too so doesn't matter. It actually only tells me that there is a new post. I have tried doing the same request as when the page loads, when this callback occurs, and get the posts that are newer than those already on the page. Unfortunately I have not succeeded in this yet. I have picked the ID from the last posted instagram post, and checked if it is in the callback request, and it's not.

What am I doing wrong?

I'd appreciate some help, thanks!

Edit:

I'd like to note that this is not only a problem with the realtime api, but also with the normal API. I just don't know how to compare data so I don't get duplicates in my database(normal api), or on my website (realtime). I can't find any tutorial or documentation (Yes, I might be blind), that explains to me how to compare data. I can only find the min_id and max_id, but no explanation what these id's contains. I checked these id's with id's from results, and they do not match. It's not an ID from a media item.

I also checked the next_url, and in my logic thinking, this should be a URL to the next page (like Twitter).

Am I looking at this all wrong?

3条回答
Luminary・发光体
2楼-- · 2020-06-12 03:49

When you get a subscription push, you need to query that endpoint (tag / recent).

I normally start an synchronous thread to perform this so I can answer in under 2 seconds to Instagram.

Then you parse that endpoint and look for a "next url" value.

Keep querying that end point, parsing the media and going to the next url until you find your stop condition.

For me I try to match 10 consecutive records in my DB. Basically from the tag, I store media when then meet my business rules.

查看更多
太酷不给撩
3楼-- · 2020-06-12 03:57

The Instagram documentation is accurate and actually well written.

The realtime API is working correctly. As stated in the documentation:

The changed data is not included in the payload, so it is up to you how you'd like to fetch the new data. For example, you may decide only to fetch new data for specific users, or after a certain number of photos have been posted.

http://instagram.com/developer/realtime/

You only receive a notification that an update has happened to your subscribed object. It is up to you to call the API to find out what that data is.

You can call the /tags/[tag-name]/media/recent with an access token that you have previously stored on your own server or DB. Then, you should be able to compare the data returned from that endpoint with any data you have retrieved prior, and just pull the objects that you do not yet have.

查看更多
看我几分像从前
4楼-- · 2020-06-12 04:08

Ok strike my old answer, I changed the way I do this. Here's how I'll do it now.

I still wait for 10 hits on my Real-time subscription, when I reach 10 I send off a new thread (if one is not already running).

The sync thread queries my DB for a value, I need the last min_tag_id I used. Then I query:

https://api.instagram.com/v1/tags/*/media/recent?access_token=*&min_tag_id=*

Try it out here: https://api.instagram.com/v1/tags/montreal/media/recent?access_token=*

You'll get 20 results, and a min_tag_id value. Append that to your url, you'll see you get no results. Wait a couple of seconds and refresh. Eventually you'll get some media, and a new min_tag_id.

(You can ignore the "next_url" value they give you, you won't be using that).

Basically you only need to store that min_tag_id and query until you have no more results, that means you're done then.

查看更多
登录 后发表回答