Last active
November 8, 2020 22:48
-
-
Save carpogoryanin/58de9272ce91de276a1044d554ce84ea to your computer and use it in GitHub Desktop.
Instagrabber is a scraper of Instagram accounts information (by default: user name, followers, avatar) using Google Spreadsheet & Google Apps scripts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instagrabber is scraper of Instagram accounts information (by default: user name, followers, avatar) using Google Spreadsheet & Google Apps scripts. | |
// Inspired by this article: https://alanyang.com/blog/instagram-analytics-with-google-sheets | |
// =========================================================================================================== | |
// THIS CODE USES YOUR INSTAGRAM SESSION ID. BE CAREFUL, DON'T SHARE THIS CODE WITH YOUR SESSION ID IN PUBLIC. | |
// =========================================================================================================== | |
// GET STARTED: | |
// 1. Create New Google Spreadsheet | |
// 2. Fill first column with URLs of instagram accounts | |
// 2. Open Script Editor: Menu -> Tools -> Script Editor | |
// 3. In Script Editor replace existing code with this code of this file | |
// 4. Change name of Sheet you want to use (or keep default value): | |
var sheetName = 'Sheet1'; | |
// 5. Provide your sessionId. It's needed to get Instagram data. I suggest to create new account for this script to prevent possible blocking of your real account. | |
var sessionId = '%your-instagram-session-id%'; | |
// How to get Instagram session id (example for Google Chrome): | |
// a. Make sure you are logged in your Instagram account. | |
// b. Right-click on any part of the page and choose “Inspect” | |
// c. Navigate to the “application” tab. | |
// d. Navigate to the “Cookies” tab then choose Instagram. | |
// e. Double-click on the “sessionid” cookie value to copy the complete value. It looks like '2234242%3aDFslfhsdlfhWweSlrje'. | |
// 6. Save project. Use any name. | |
// 7. Choose "runInstagrabber" in dropdown nearby the Bug icon | |
// 8. Click Play button. First running asks for permissions. Choose unsafe and approve. It's regular Google Apps script permission approving process, nothing special. | |
// 9. Open spreadsheet you created before. It should start be filling. | |
// 10. PROFIT. | |
// Caution: The more data in your Google Spreadsheet, the longer it'll take to load | |
// HOW TO ADD NEW ACCOUNTS | |
// Insert account URL in a new row, and run script. Only rows without data will be filled - kind of optimization to skip cells already filled. | |
// HOW TO UPDATE EXISTING ACCOUNTS | |
// If you want to rerender sheet completelly, just select 3 or 4 column and remove the data and run script again. | |
// SHORTCUT | |
// Add shortcut to run this script right from your Spreadsheet: | |
// 1. Open: Menu -> Tools -> Macros -> Import | |
// 2. Choose function "runInstagrabber" | |
// 3. Open: Menu -> Tools -> Macros -> Manage macros | |
// 4. Set up your shortcut, ex: cmd + opt + shift + 0 | |
// EXTENDING | |
// This script can be easily extended by any field from this URL: 'https://www.instagram.com/account_name/?__a=1' | |
// (c) 2020 carpogoryanin. Instagrabber is MIT-licensed. | |
function runInstagrabber(index) { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheetByName(sheetName); | |
var values = sheet.getDataRange().getValues(); | |
var accountsColIndex = 0; | |
var i = 0; | |
for(n=0;n<values.length;++n){ | |
var currentRow = n+1; | |
var url = values[n][accountsColIndex]; | |
var accountName = getAccountNameFromUrl(url); | |
var startColumn = 3; | |
var fillingCells = 2; | |
var picColumn = 2; | |
var currentRange = sheet.getRange(currentRow,startColumn,1,fillingCells); | |
var currentRangeValues = currentRange.getValues(); | |
var currentRowValues = currentRangeValues[0]; | |
if (!hasEmptyCell(currentRowValues)) continue; | |
var account = getAccount(accountName); | |
var followers = account['edge_followed_by']['count']; | |
var fullName = account['full_name']; | |
var picUrl = account['profile_pic_url']; | |
var picSize = 40; | |
Logger.log('Updated:', accountName); | |
Logger.log('Followers:', followers); | |
Logger.log('Full name:', fullName); | |
Logger.log(''); | |
sheet.setRowHeight(currentRow, picSize); | |
sheet.setColumnWidth(picColumn, picSize); | |
sheet.getRange(currentRow,picColumn).setFormula(`=image("${picUrl}")`); | |
currentRange.setValues([[followers, fullName]]); | |
i++; | |
} | |
if (!i) { | |
Logger.log('Nothing changed.'); | |
} else { | |
Logger.log('Sheet has been updated!'); | |
} | |
} | |
function fetch(url) { | |
var cookie = 'sessionid=' + sessionId; | |
var options = { | |
muteHttpExceptions: true, | |
headers: { | |
'Cookie': cookie | |
} | |
}; | |
var source = UrlFetchApp.fetch(url, options).getContentText(); | |
var data = JSON.parse(source); | |
return data; | |
} | |
function getAccountId(name) { | |
var res = fetch('https://www.instagram.com/web/search/topsearch/?query=' + name)['users'][0]['user']['pk']; | |
return res; | |
} | |
function getAccount(name) { | |
var res = fetch('https://www.instagram.com/' + name + '/?__a=1')['graphql']['user']; | |
return res; | |
} | |
function getAccountNameFromUrl(url) { | |
var res = url.split('/'); | |
res = res.pop() || res.pop(); | |
return res; | |
} | |
function hasEmptyCell(arr) { | |
for(var i=0; i<arr.length; ++i) if(!arr[i]) return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment