Sto cercando di interrogare i post di Instagram fornendo l'hashtag e l'intervallo di tempo (da e fino alle date). Io uso il recent tags endpoint.Invia messaggi di Instagram per hashtag e intervallo di tempo
https://api.instagram.com/v1/tags/{tag-name}/media/recent?access_token=ACCESS-TOKEN
Il mio codice è scritto in Node.js utilizzando la libreria instagram-node
(vedi i commenti inline):
// 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);
});
};
Questo farà partire il recupero dei messaggi con il più recente al meno recenti, e filtrare manualmente il created_time
. Funziona, ma è molto molto inefficiente perché se vogliamo, ad esempio, ottenere i post di un anno fa, dobbiamo iterare le pagine fino a quel momento, e questo userà molte richieste (probabilmente più di 5k/ora che è il limite di velocità).
C'è un modo migliore per fare questa richiesta? Come ottenere i post di Instagram fornendo l'hashtag e l'intervallo di tempo?
Potresti aumentare il 'CONTO' per afferrare una significativamente più alta quantità di foto alla volta per ridurre * l'importo * dei post fetch? Naturalmente sarebbero molto più grandi ma sarebbe qualcosa di simile essere utile? –
@NickZ Abbiamo provato a farlo (stavo eseguendo il debug di questo è l'OP) e il massimo che abbiamo ottenuto è stato di 33 articoli/richiesta. Quindi, il 'count' non aiuta davvero ... :-(Qualche altra idea? Sono felice di assegnare 50 punti a qualcuno che dia una buona risposta.: D –
Hai cercato di utilizzare MIN_TAG_ID e MAX_TAG_ID per iterare rapidamente verso una data di destinazione? Posso immaginare un metodo che richiede un solo post alla volta per trovare un MAX_TAG_ID che è appena prima della data richiesta. – sbozzie