Skip to content

Instantly share code, notes, and snippets.

@apocratus
Last active June 14, 2024 00:03
Show Gist options
  • Save apocratus/7054dcb5682c03dd09160cb45b28a978 to your computer and use it in GitHub Desktop.
Save apocratus/7054dcb5682c03dd09160cb45b28a978 to your computer and use it in GitHub Desktop.
Retrieve Instagram Profile & Posts Data with Google Apps Script and save to Google Sheets
/**
* This function retrieves all your latest instagram posts from the Instagram API and saves it to a Google Sheets spreadsheet. It
* replaces all the previous data (Run hourly for up-to-date data).
*
* You need to register a new app at https://www.instagram.com/developer/clients/manage/ and generate a access token to use this.
* (http://bobmckay.com/web/simple-tutorial-for-getting-an-instagram-clientid-and-access-token/).
*
* This can be used to build a Google Data Studio report of your Instagram data.
*/
// the name of the sheet within your document
var sheetNamePosts = "Sheet2";
// a fresh Instagram API Access Token
var accessToken = "instagram_api_access_token";
var url = "https://api.instagram.com/v1/users/self/media/recent?access_token="+accessToken;
function insertPostData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(this.sheetNamePosts);
sheet.clear();
var response = UrlFetchApp.fetch(this.url);
var resultdata = [];
var media = JSON.parse(response).data;
for (i=0; i< media.length; i++){
var c = media[i];
var likes = c.likes.count;
var comments = c.comments.count;
var link = c.link;
var image = c.images.standard_resolution.url;
var text = c.caption.text;
var date = Utilities.formatDate(new Date(c.created_time*1000), "GMT", "yyyy-MM-dd");
// post date, post link, post text, post image, post likes, post comments
resultdata.push([date, link, text, image, likes, comments]);
}
sheet.appendRow(["Date", "Shortcode", "Text", "Image", "Likes", "Comments"]);
for(i=0; i<resultdata.length; i++) {
sheet.appendRow(resultdata[i]);
}
};
/**
* This function retrieves all your relevant profile data from the Instagram API and saves it to a Google Sheets spreadsheet. It
* checks the last inserted column and if the dates are the same it replaces the data. (Run hourly for up-to-date data).
*
* You need to register a new app at https://www.instagram.com/developer/clients/manage/ and generate a access token to use this.
* (http://bobmckay.com/web/simple-tutorial-for-getting-an-instagram-clientid-and-access-token/).
*
* This can be used to build a Google Data Studio report of your Instagram data.
*/
// the name of the sheet within your document
var sheetNameHistorical = "Sheet1";
// a fresh Instagram API Access Token
var accessToken = "instagram_api_access_token";
var url = "https://api.instagram.com/v1/users/self/?access_token="+accessToken;
function insertProfileData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(this.sheetNameHistorical);
var response = UrlFetchApp.fetch(this.url);
var json = JSON.parse(response);
// new data
var followed_by = json.data.counts.followed_by;
var follows = json.data.counts.follows;
var media = json.data.counts.media;
// current data
var data = sheet.getDataRange().getValues();
var lastRow = sheet.getLastRow();
if(lastRow == 0) {
sheet.appendRow(['Date', 'Followers', 'Following', 'Posts']);
}
else {
var lastDate = new Date(sheet.getRange(lastRow, 1).getValues());
lastDate.setHours(lastDate.getHours() + 4); // fix lack of hours
var checkToday = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
var checklastDate = Utilities.formatDate(lastDate, "GMT", "yyyy-MM-dd");
// if the date already exists, remove and replace
if(checkToday === checklastDate ) {
sheet.deleteRow(lastRow);
}
}
sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), followed_by, follows, media]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment