Created
October 4, 2012 10:56
-
-
Save brucemcpherson/3832942 to your computer and use it in GitHub Desktop.
Proxy api query using google apps script to get scraperwiki data
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
// this will return data from a public scraperwiki table as json | |
// you call like this - ?shortname=xyz&limit=x&fielda=x&fieldy=y etc... where fielda refers to the name of a field in the scraperwiki table | |
// see ramblings.mcpher.com for more info | |
function doGet(e) { | |
// lets decode the arguments | |
var content = ""; | |
if (!e.parameter) { | |
content = {error: "parameters not found"} ; | |
} | |
else if (!e.parameter.shortname) { | |
content = {error: "shortname parameter not found", parameters : e } ; | |
} | |
else { | |
// everything else is optional | |
var lim = e.parameter.limit ? e.parameter.limit : 0; | |
var s = ""; | |
for (k in e.parameter) { | |
if (k != "limit" && k != "shortname") { | |
s = (s ? s + " AND " : " WHERE ") + k + " = '" + e.parameter[k] + "'" ; | |
} | |
} | |
// now we can call up the rest entry | |
var cr = mcpher.scraperWikiStuff(e.parameter.shortname, undefined , | |
undefined , lim ? lim : undefined, s ? s : undefined , false); | |
if (!cr) { | |
content = {error: "unable to query " + e.parameter.shortname, parameters: e } ; | |
} | |
else { | |
content = cr.jObject().toNative(); | |
} | |
} | |
return ContentService | |
.createTextOutput(JSON.stringify (content)) | |
.setMimeType(ContentService.MimeType.JSON); ; | |
} | |
// this is just an example of calling directly... | |
function t() { | |
var e = {parameter: | |
{shortname : "interpol_wanted_persons", | |
Nationality : "Malaysia", | |
limit: 10} | |
}; | |
doGet(e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for more information, see ramblings.mcpher.com