Created
July 27, 2011 18:06
-
-
Save LFSaw/1110003 to your computer and use it in GitHub Desktop.
accessing the Gist API from within SuperCollider
This file contains hidden or 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
+ String { | |
getUnquotedTextIndices {|quoteChar = "\""| | |
var quoteIndices; | |
quoteIndices = this.findAll(quoteChar.asString); | |
quoteIndices = quoteIndices.select{|idx, i| | |
this[idx-1] != $\\ | |
}; | |
^((([-1] ++ quoteIndices ++ [this.size]).clump(2)) +.t [1, -1]) | |
} | |
getStructuredTextIndices { | |
var unquotedTextIndices; | |
unquotedTextIndices = this.getUnquotedTextIndices; | |
unquotedTextIndices = unquotedTextIndices.collect{|idxs| | |
this.copyRange(*idxs).getUnquotedTextIndices($') + idxs.first | |
}.flat.clump(2); | |
^unquotedTextIndices | |
} | |
prepareForJSonDict { | |
var newString = this.deepCopy; | |
var idxs; | |
idxs = newString.getStructuredTextIndices; | |
idxs.do{|pairs, i| | |
Interval(*pairs).do{|idx| | |
(newString[idx] == ${).if({newString[idx] = $(}); | |
(newString[idx] == $}).if({newString[idx] = $)}); | |
(newString[idx] == $:).if({ | |
[(idxs[i-1].last)+1, pairs.first-1].do{|quoteIdx| | |
newString[quoteIdx] = $' | |
} | |
}); | |
} | |
}; | |
^newString | |
} | |
jsonToDict { | |
^(this.prepareForJSonDict.interpret) | |
} | |
} |
This file contains hidden or 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
Gist { | |
*allGistsFor {|user, username, password| | |
var options; | |
options = password.notNil.if({ | |
"-u %:%".format(username, password) | |
}, { | |
"" | |
}); | |
^("https://api.github.com/users/%/gists".format(user).curl(options: options)).jsonToDict | |
} | |
*getGist {|id, username, password| | |
var options; | |
options = username.notNil.if({ | |
"-u %:%".format(username, password) | |
}, { | |
"" | |
}); | |
^("https://api.github.com/gists/%".format(id).curl(options: options)).jsonToDict | |
} | |
*contentAsJsonString {|contentDict| | |
var result = "{\n\n"; | |
var numCommas = contentDict.size - 1; | |
contentDict.keysValuesDo({|key, val, i| | |
result = result ++ "%: {\"content\":\n %}%\n".format(key.asString.quote, | |
val | |
.replace( | |
"\\", | |
"\\\\" | |
) | |
.replace( | |
"'", | |
"'\\''" | |
) | |
.escapeChar($").quote, (i < numCommas).if({","}, {""})) | |
}); | |
result = result++ "\n}"; | |
^result | |
} | |
*postGist {|descr, content, public = true, username, password| | |
var options, jsonString; | |
jsonString = "\'{\"description\": \"%\",\n\"public\": %,\n\"files\": %}\'" | |
.format(descr, public, this.contentAsJsonString(content)); | |
options = username.notNil.if({ | |
"-u %:%".format(username, password) | |
}, { | |
"" | |
}); | |
options = options + "-d %".format(jsonString); | |
^"https://api.github.com/gists".curl(options: options) | |
} | |
} |
This file contains hidden or 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
json and gist | |
// if the following fails for you, please check if you have to update wslib's curl method by the one in | |
// json/extString-curl.scd | |
q = (); | |
// set your username / password here if you wanna create personalized gists, but its optional. | |
q.username = nil; | |
q.password = nil; | |
////////////////////////////// get existing gists ///////////// | |
q.list = Gist.allGistsFor("LFSaw", q.username, q.password); | |
q.list.first.id | |
// the gist dict for the latest gist of the user "LFSaw" | |
q.gist = Gist.getGist(q.list.first.id) | |
q.gist.files.asArray.first.content | |
// the gist dict for an example gist for the OpenSynthDef world | |
q.gist = Gist.getGist(1109924) | |
q.gist.files.asArray.first.content.postln.interpret.play | |
////////////////////////////// post new gists ///////////// | |
( // test | |
var content = ( | |
'SynthDef.sc': "SynthDef(\out, {Out.ar(0, FSinOsc.ar(247))})", | |
'SynthDef2.sc': "SynthDef(\out, {|a = \"12\"| var c = 'b'.kr; Out.ar(0, FSinOsc.ar(b))})" | |
); | |
Gist.contentAsJsonString(content) | |
) | |
( // post new gist | |
var descr = "My 44th API submittet SynthDef gist"; | |
var public = true; | |
var content = ( | |
'SynthDef.sc': "SynthDef(\"out\", {Out.ar(0, FSinOsc.ar(247); \\haha)})" | |
); | |
q.postGist(descr, content, public, q.username, q.password); | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment