Created
August 18, 2011 20:46
-
-
Save mhawksey/1155158 to your computer and use it in GitHub Desktop.
Get a comment count from a wordpress blog post using Google Apps Script. Used in http://mashe.hawksey.info/2011/08/and-the-most-engaging-ouseful-info-post-is/
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
function getCommentCount(){ | |
for (var rowIdx=0; rowIdx < sheet.getLastRow(); rowIdx++){ | |
var rowNum = rowIdx+TITLE_ROW+1; | |
var url = sheet.getRange(rowNum,URL_COL).getValue(); | |
Utilities.sleep(500); | |
try { | |
var options = | |
{ | |
"method" : "get" | |
}; | |
var response = UrlFetchApp.fetch(url+"feed/", options); | |
if (response.getResponseCode() == 200) { | |
var responseStr = response.getContentText(); | |
var XMLdoc = Xml.parse(responseStr); // parse xml | |
var title = XMLdoc.rss.channel.title.getText().replace("Comments on: ",""); | |
if (XMLdoc.rss.channel.item == undefined){ | |
var commentCount = 0; | |
} else { | |
var commentCount = XMLdoc.rss.channel.item.length; | |
} | |
if (commentCount == undefined) | |
commentCount = 1; | |
sheet.getRange(rowNum, TITLE_COL).setValue(title); | |
sheet.getRange(rowNum, COM_COL).setValue(commentCount); | |
} | |
} catch(e) { | |
Logger.log(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment