Skip to content

Instantly share code, notes, and snippets.

@tomcritchlow
Last active May 23, 2019 18:46
Show Gist options
  • Save tomcritchlow/50fba1a5af3f1c8e6dfab0a5e2e31294 to your computer and use it in GitHub Desktop.
Save tomcritchlow/50fba1a5af3f1c8e6dfab0a5e2e31294 to your computer and use it in GitHub Desktop.
annotation-tracker-google-scripts
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var outputsheet = ss.getSheets()[0];
// get the existing IDs already in our spreadsheet in column A, sheet 1
var existingfeeddata = outputsheet.getRange("A:A").getValues();
var ids = [];
for(var i in existingfeeddata){
ids.push(existingfeeddata[i][0]); // convert 2d getvalues array to 1d array of IDs
}
var fetch = UrlFetchApp.fetch("https://api.hypothes.is/api/search?wildcard_uri=https%3A%2F%2Ftomcritchlow.com%2F%2A&limit=50").getContentText();
var json = JSON.parse(fetch);
var newitems = [];
// add the new annotations to the spreadsheet
for (var i in json.rows){
var id = json.rows[i].id;
var source = json.rows[i].target[0].source;
var user = json.rows[i].user_info.display_name;
var date = json.rows[i].updated;
var link = json.rows[i].links.incontext;
if(ids.indexOf(id) == -1){ //checks to see if new id found is already in the spreadsheet
outputsheet.appendRow([id, date, user, source,link]); // appends new annotations to the spreadsheet
newitems.push([id, date, user, source, link]); //adds new annotations to an array to be emailed
}
}
// email the new ones
if(newitems.length > 0){
content = []
for(var i in newitems){
content.push("New annotation from "+newitems[i][2]+" on "+newitems[i][3]+" view <a href='"+newitems[i][4]+"'>here</a><br><br>");
}
var message = content.join("");
MailApp.sendEmail({
to: "email", //add your own email here.
subject: "New Annotation on tomcritchlow.com",
htmlBody: message,
});
}
//sort the final output by date to ensure final data is sorted
var newdata = outputsheet.getRange("A:E").sort({column: 2, ascending: false});
}
@tomcritchlow
Copy link
Author

Use this code in Google Scripts to monitor annotations on a given domain. Set this script to run once/day and get an email notification when new annotations are posted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment