Last active
February 22, 2021 22:44
-
-
Save nocturnae/5658e66c6b9ed85467b30ec479cfc920 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Your sheet name in the document | |
var sheetName = "Data"; | |
// Your instagram user id | |
var user_id = ""; //find your id here : https://codeofaninja.com/tools/find-instagram-user-id | |
var instagram_base_url = "https://www.instagram.com/graphql/query/"; | |
var following = "?query_hash=58712303d941c6855d4e888c5f0cd22f&variables=%7B%22id%22%3A%22" + user_id + "%22%2C%22first%22%3A24%7D" | |
var followers = "?query_hash=37479f2b8209594dde7facb0d904896a&variables=%7B%22id%22%3A%22" + user_id + "%22%2C%22first%22%3A24%7D" | |
var medias = "?query_hash=f2405b236d85e8296cf30347c9f08c2a&variables=%7B%22id%22%3A%22" + user_id + "%22%2C%22first%22%3A12%7D" | |
function insertFollowerCount() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheetByName(this.sheetName); | |
var previousData = prevData(); | |
var followers = getFollowers(); | |
var following = getFollowing(); | |
var medias = getMedias(); | |
var engagement = getEngagement(medias, followers); | |
sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), followers, followers-previousData.followers, following, following-previousData.following, medias.count, engagement.totalLikes, engagement.totalComments, engagement.EngagementRatio, engagement.EngagementRatio - previousData.engagement]); | |
}; | |
function getFollowers() { | |
return parseInt(fetch(instagram_base_url + followers)['data']['user']['edge_followed_by']['count']); | |
} | |
function getFollowing() { | |
return parseInt(fetch(instagram_base_url + following)['data']['user']['edge_follow']['count']); | |
} | |
function getMedias() { | |
return fetch(instagram_base_url + medias)['data']['user']['edge_owner_to_timeline_media']; | |
} | |
function getEngagement(medias, followers) { | |
var totalComments = 0, | |
totalLikes = 0; | |
for (var i = 0; i < 12; i++) { | |
totalComments += parseInt(medias.edges[i].node.edge_media_to_comment.count); | |
}; | |
for (var l = 0; l < 12; l++) { | |
totalLikes += parseInt(medias.edges[l].node.edge_media_preview_like.count); | |
}; | |
var engagementRatio = (((totalLikes + totalComments)) / followers) / 12; | |
return { | |
mediaCount: parseInt(medias.count), | |
totalComments: totalComments, | |
totalLikes: totalLikes, | |
EngagementRatio: engagementRatio | |
} | |
} | |
//paste your cookie value below. | |
var header = {'cookie': ''}; | |
function fetch(url) { | |
var ignoreError = { | |
"muteHttpExceptions": true, | |
"validateHttpsCertificates":false, | |
"headers":header | |
}; | |
var source = UrlFetchApp.fetch(url, ignoreError).getContentText(); | |
var data = JSON.parse(source); | |
return data; | |
} | |
function prevData() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheetByName(this.sheetName); | |
var Avals = sheet.getRange("B1:B").getValues(); | |
var Alast = Avals.filter(String).length; | |
var prevFollowers = sheet.getRange("B"+Alast).getValues(); | |
var prevFollowing = sheet.getRange("D"+Alast).getValues(); | |
var prevEngagement = sheet.getRange("I"+Alast).getValues(); | |
return { | |
followers: parseInt(prevFollowers), | |
following: parseInt(prevFollowing), | |
engagement: prevEngagement | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment