通过标签和时间范围查询Instagram的post

我试图通过提供哈希标签和时间范围(自和直到date)从Instagram查询post。 我使用最近的标签端点 。

https://api.instagram.com/v1/tags/{tag-name}/media/recent?access_token=ACCESS-TOKEN

我的代码是使用instagram-node库在Node.js中编写的(请参阅行内注释):

 // Require the config file var config = require('../config.js'); // Require and intialize the instagram instance var ig = require('instagram-node').instagram(); // Set the access token ig.use({ access_token: config.instagram.access_token }); // We export this function for public use // hashtag: the hashtag to search for // minDate: the since date // maxDate: the until date // callback: the callback function (err, posts) module.exports = function (hashtag, minDate, maxDate, callback) { // Create the posts array (will be concated with new posts from pagination responses) var posts = []; // Convert the date objects into timestamps (seconds) var sinceTime = Math.floor(minDate.getTime() / 1000); var untilTime = Math.floor(maxDate.getTime() / 1000); // Fetch the IG posts page by page ig.tag_media_recent(hashtag, { count: 50 }, function fetchPosts(err, medias, pagination, remaining, limit) { // Handle error if (err) { return callback(err); } // Manually filter by time var filteredByTime = medias.filter(function (currentPost) { // Convert the created_time string into number (seconds timestamp) var createdTime = +currentPost.created_time; // Check if it's after since date and before until date return createdTime >= sinceTime && createdTime <= untilTime; }); // Get the last post on this page var lastPost = medias[medias.length - 1] || {}; // ...and its timestamp var lastPostTimeStamp = +(lastPost.created_time || -1); // ...and its timestamp date object var lastPostDate = new Date(lastPostTimeStamp * 1000); // Concat the new [filtered] posts to the big array posts = posts.concat(filteredByTime); // Show some output console.log('found ' + filteredByTime.length + ' new items total: ' + posts.length, lastPostDate); // Check if the last post is BEFORE until date and there are no new posts in the provided range if (filteredByTime.length === 0 && lastPostTimeStamp <= untilTime) { // ...if so, we can callback! return callback(null, posts); } // Navigate to the next page pagination.next(fetchPosts); }); }; 

这将开始获取最近的到最近的post,并手动过滤created_time 。 这是有效的,但效率非常低,因为如果我们想要从一年前获得post,我们必须迭代这些页面直到那个时候,这将使用很多请求(可能超过5k /小时这是速率限制)。

有没有更好的方法来做这个查询? 如何通过提供标签和时间范围获得Instagram的post?

我认为这是你正在寻找的基本想法。 我不是很熟悉Node.js,所以这是全部在普通的JavaScript。 你将不得不修改它来适应你的需求,并可能从中做出一个function。

这个想法是将一个instagram id(在这个例子中是1116307519311125603)转换为一个date,反之亦然,以便您快速获取特定时间点,而不是回溯所有结果,直到find您想要的时间戳。 下划线'_'后面的id部分应该删除,因为这在某种程度上是指用户IIRC。 在这个例子中有4个function,我希望能帮助你。

快乐的黑客!

 //static var epoch_hour = 3600, epoch_day = 86400, epoch_month = 2592000, epoch_year = 31557600; //you'll need to set this part up/integrate it with your code var dataId = 1116307519311125603, range = 2 * epoch_hour, count = 1, tagName = 'cars', access = prompt('Enter access token:'), baseUrl = 'https://api.instagram.com/v1/tags/' + tagName + '/media/recent?access_token=' + access; //date && id utilities function idToEpoch(n){ return Math.round((n / 1000000000000 + 11024476.5839159095) / 0.008388608); } function epochToId(n){ return Math.round((n * 0.008388608 - 11024476.5839159095) * 1000000000000); } function newDateFromEpoch(n){ var d = new Date(0); d.setUTCSeconds(n); return d; } function dateToEpoch(d){ return (d.getTime()-d.getMilliseconds())/1000; } //start with your id and range; do the figuring var epoch_time = idToEpoch(dataId), minumumId = epochToId(epoch_time), maximumId = epochToId(epoch_time + range), minDate = newDateFromEpoch(epoch_time), maxDate = newDateFromEpoch(epoch_time + range); var newUrl = baseUrl + '&count=' + count + '&min_tag_id=' + minumumId + '&max_tag_id=' + maximumId; //used for testing /*alert('Start: ' + minDate + ' (' + epoch_time + ')\nEnd: ' + maxDate + ' (' + (epoch_time + range) + ')'); window.location = newUrl;*/ 

为了支持这个优秀的答案,通过plpgSQL函数生成一个instagram ID:

 CREATE OR REPLACE FUNCTION insta5.next_id(OUT result bigint) AS $$ DECLARE our_epoch bigint := 1314220021721; seq_id bigint; now_millis bigint; shard_id int := 5; BEGIN SELECT nextval('insta5.table_id_seq') %% 1024 INTO seq_id; SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis; result := (now_millis - our_epoch) << 23; result := result | (shard_id << 10); result := result | (seq_id); END; $$ LANGUAGE PLPGSQL; 

从Instagram的博客