Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickynicolson/808ed3da1a7cbaa46134 to your computer and use it in GitHub Desktop.
Save nickynicolson/808ed3da1a7cbaa46134 to your computer and use it in GitHub Desktop.
import urllib
import urllib2
try:
# Base url for the TNRS match_names API
url = 'http://api.opentreeoflife.org/v2/tnrs/match_names'
# Encode data value to be looked up as an array of names:
data = '{"names": ["'+value+'"]}'
print "Looking up: " + data
# Set HTTP headers:
headers = { 'Content-Type' : 'application/json' }
# Make the call using HTTP POST
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
# Return the JSON value
return response.read()
except (Error):
print 'Exception occurred'
@nickynicolson
Copy link
Author

This is a Jython snippet that can be used for a column addition operation to call the OpenTreeOfLife TNRS API and store the JSON result.
It's a bit more complex than a regular OpenRefine column addition as the OpenTree API only responds to HTTP POST. OpenRefine's function to add a column by fetching URLs can only use HTTP GET.

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