Optimizing google script that grabs Instagram post data

冷暖自知 提交于 2019-12-24 19:47:11

问题


Over the past few days/weeks I've been working on a Google Script that imports Instagram data in to a spreadsheet. So far I've successfully managed to grab things like follower, following data, engagement rate and amount of posts and daily changes.

I'm now working to find a way that allows me to grab the data on a post-by-post level and update the data daily. But since i'm really a novice in programming. I'm doing it in a very inefficient way and I don't have an idea how to update existing rows.

This is what I have so far:

// Get Date field filled
function insertPostData(sheetName, instagramAccountName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetName); 
  sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), Post1Comment(instagramAccountName)]); 
  sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), Post2Comment(instagramAccountName)]); 
  Utilities.sleep(200);
}; 



//Write post1 comments to Sheet
function Post1Comment(username) { 
  var url = "https://www.instagram.com/" + username + "/?__a=1", totalCount = 0;
  var response = UrlFetchApp.fetch(url).getContentText();
    for(var i = 0; i < 12; i++) {
      totalCount += parseInt(JSON.parse(response).user.media.nodes[i].comments.count);
     return totalCount;
}}

//Write post2 comments to Sheet
function Post2Comment(username) { 
  var url = "https://www.instagram.com/" + username + "/?__a=1", totalCount = 0;
  var response = UrlFetchApp.fetch(url).getContentText();
    for(var i = 1; i < 12; i++) {
      totalCount += parseInt(JSON.parse(response).user.media.nodes[i].comments.count);
     return totalCount;
}}

And this I would need to for 12 posts (which you can freely grab from the https://www.instagram.com/username/?__a=1 url. Thinking about the fact that I want to grab multiple objects (post date, likes, comments, media id,..) from each post doing it this way would be really inefficient...

Can anybody help find the right direction to do this more efficient?


回答1:


How about using UrlFetchApp.fetchAll()? Recently, this method was added. This method can fetch several requests.

At fetchAll(), the requests can be executed using an array with the requests. The response from fetchAll() is an array. And the indexes in the array of response are corresponding to those of the requested array. I thought that this can be used for your situation.

When this method is used for your situation, the modified script is as follows.

Flow :

  • Create the request array.
  • Fetch using the created requests.
  • Convert the retrieved data to import to Spreadsheet.
  • Import the converted data.

Modified script :

function insertPostData(sheetName) {
  var userNames = [
    "instagramAccountName1",
    "instagramAccountName2",
    "instagramAccountName3",
    ,
    ,
  ]; // Please input instagramAccountNames here.

  var requests = userNames.map(function(e){return {"url": "https://www.instagram.com/" + e + "/?__a=1", "method": "get"}});
  var response = UrlFetchApp.fetchAll(requests);
  var date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
  var totalCounts = [];
  for (var j in response) {
    var totalCount = 0;
    for(var i = 1; i < 12; i++) {
      totalCount += parseInt(JSON.parse(response[j]).user.media.nodes[i].comments.count);
    }
    totalCounts.push([date, totalCount]);
  }
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetName);
  sheet.getRange(sheet.getLastRow() + 1, 1, totalCounts.length, totalCounts[0].length).setValues(totalCounts);
}

Reference :

  • fetchAll()

If I misunderstand your question, I'm sorry. If this didn't work, please tell me. I would like to modify.



来源:https://stackoverflow.com/questions/49161656/optimizing-google-script-that-grabs-instagram-post-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!