从facebook组中选择最喜欢的帖子

时间:2015-01-12 22:45:29

标签: javascript json facebook facebook-graph-api google-apps-script

好吧,这是json文件http://herbalista.hol.es/group.json我正在使用JSON.parse();在Google应用脚本上。我通过选择有超过15个喜欢的帖子暂时解决这个代码,但如果有或不超过15个喜欢,我想选择一个喜欢独立的人。

function repost() {
var UsrAccess_token = "xxxxxxxxx"
var graph = "https://graph.facebook.com/xxxxxx/feed/?access_token="+UsrAccess_token+"";
var jsondata = UrlFetchApp.fetch(graph,{method:"get"}).getContentText();
var object = JSON.parse(jsondata);
var item = object.data;
var currentTime = new Date();
var year = currentTime.getUTCFullYear();
var month = (currentTime.getUTCMonth()) + 1;
var day = (currentTime.getUTCDate()) - 1;
if (day <= 9) {var day = "0"+day+"";}
if (month <= 9) {var month = "0"+month+"";}
var utime = ""+year+"-"+month+"-"+day+"T";
try {
var i = null;
for (i = 0; item.length > i; i += 1) {
var pubDate = item[i].created_time;
if (pubDate.match(utime)) { var likesdata = item[i].likes.data; var len = likesdata.length;
                           if (len > 15) {var popular = item[i].link;}}


}} catch(err) {
   var err = "ERROR";
}
}

1 个答案:

答案 0 :(得分:0)

为此,您可以选择var maxLikes = 0;等变量的默认值,并针对len变量进行验证。

代码将是这样的:

function repost() {
var UsrAccess_token = "xxxxxxxxx"
var graph = "https://graph.facebook.com/xxxxxx/feed/?access_token="+UsrAccess_token+"";
var jsondata = UrlFetchApp.fetch(graph,{method:"get"}).getContentText();
var object = JSON.parse(jsondata);
var item = object.data;
var currentTime = new Date();
var year = currentTime.getUTCFullYear();
var month = (currentTime.getUTCMonth()) + 1;
var day = (currentTime.getUTCDate()) - 1;
if (day <= 9) {var day = "0"+day+"";}
if (month <= 9) {var month = "0"+month+"";}
var utime = ""+year+"-"+month+"-"+day+"T";
try {
  var i = null;
  var maxLikes = 0; 
  for (i = 0; item.length > i; i += 1) {
    var pubDate = item[i].created_time;
    if (pubDate.match(utime)) { 
      var likesdata = item[i].likes.data;
      var len = likesdata.length;
      if (len > maxLikes) {
        maxLikes = len;
        var popular = item[i].link;
      }
    }
  }
} catch(err) {
   var err = "ERROR";
}

}

希望有所帮助!