Instantly share code, notes, and snippets.

Embed
What would you like to do?
Not looping but grabs 40 rather than 20 instagram items
function addTitles() {
var sheet = SpreadsheetApp.getActiveSheet();
// sets up the header row for the sheet
sheet.appendRow(['User Name', 'Likes Count', 'Comment Count', 'URL', 'Caption', 'Filter', 'Created Time']);
}
// attempt to loop to get next_url
var search = 'dog'; // search term
var client_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var url = 'https://api.instagram.com/v1/tags/' + search + '/media/recent?client_id=' + client_id; // replace this with your API client ID
function getInstagramData(searchTerm) {
var result = UrlFetchApp.fetch(url);
var json = JSON.parse(result.getContentText());
var grams = json.data;
var next_url = json.pagination.next_url
var resultb = UrlFetchApp.fetch(next_url);
var jsonb = JSON.parse(resultb.getContentText());
var gramsb = jsonb.data;
//Logger.log(result);
//Logger.log(next_url);
// Instagram gives you 20 on the first request
for (var i = 0; i <= 20; i++) {
logGram_(grams[i]);
logGram_(gramsb[i]);
}
}
function logGram_(gram) {
var log = [];
// pushes the various elements into the spreadsheet
log.push(gram.user.username);
log.push(gram.likes.count);
log.push(gram.comments.count);
log.push(gram.link);
log.push(gram.caption.text);
log.push(gram.filter);
log.push(gram.created_time);
SpreadsheetApp.getActiveSheet().appendRow(log)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment