Skip to content

Instantly share code, notes, and snippets.

@bennettscience
Last active June 9, 2016 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennettscience/32f456dd7415e58ea8773923cf08bfd4 to your computer and use it in GitHub Desktop.
Save bennettscience/32f456dd7415e58ea8773923cf08bfd4 to your computer and use it in GitHub Desktop.
Google Apps Script for interacting with the Hypothes.is API
function hypothesis() {
// Check Drive for an existing document with annotations. If one isn't present, create it.
var docs = DriveApp.getFilesByName('Hypothes.is Annotations');
if(docs.hasNext()) {
var doc = docs.next();
var id = doc.getId();
} else {
var doc = DocumentApp.create('Hypothes.is Annotations');
var id = doc.getId();
}
// Hypothes.is API options. Modelded off of Kris Shaffer's Pypothesis
// https://github.com/kshaffer/pypothesis
var source = "http://hypothes.is/api/search?";
var conn = "&";
// Change this to search for something other than my username
// Valid parameters are outlined in the docs
// http://h.readthedocs.io/en/latest/api/#search
var term = "user=bennettscience";
var url = source + term;
var options = {
"method": "GET",
"contentType": "application/json"
}
// Call the API for the search term
var response = UrlFetchApp.fetch(url, options);
var data = JSON.parse(response);
// Logger.log(data.total);
// Clear the document body to avoid duplicating annotations
var doc = DocumentApp.openById(id);
doc.getBody().clear();
// Grab the JSON data returned by the API call and convert it to strings.
for(var i=0;i<data.total;i++) {
var item = data.rows[i].text;
var link = data.rows[i].uri;
doc.getBody().appendParagraph(item + "\r\r" + link).appendHorizontalRule();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment