Skip to content

Commit

Permalink
get works again
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Nov 8, 2012
1 parent 063fe2b commit 4fd4ad8
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 31 deletions.
138 changes: 138 additions & 0 deletions get/base64.js
@@ -0,0 +1,138 @@
steal(function(){
var Base64 = {

// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;

input = Base64._utf8_encode(input);

while (i < input.length) {

chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);

enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;

if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}

output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

}

return output;
},

// public method for decoding
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;

input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

while (i < input.length) {

enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

output = output + String.fromCharCode(chr1);

if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}

}

output = Base64._utf8_decode(output);

return output;

},

// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";

for (var n = 0; n < string.length; n++) {

var c = string.charCodeAt(n);

if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}

}

return utftext;
},

// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;

while ( i < utftext.length ) {

c = utftext.charCodeAt(i);

if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}

}

return string;
}

}
return Base64;
})
8 changes: 5 additions & 3 deletions get/get.js
Expand Up @@ -299,16 +299,18 @@ steal("steal","steal/get/json.js",
*/
fetch : function(url, path, options ){
// make the new folder
if( options.getter.init ) {
options.getter.init(options);
}


var raw = options.getter.raw(url),
var raw = options.getter.raw(url, options),
content = readUrl(raw),
// only make a folder the first time we put a file in the folder
madeFolder = false;

//print("\nfetching "+url+"--------\n\n")

var urls = options.getter.ls(content, raw, url);
var urls = options.getter.ls(content, raw, url, options);

//separate folders and files ...
pathloop:
Expand Down
66 changes: 52 additions & 14 deletions get/git.js
@@ -1,15 +1,15 @@
steal('steal',function(s){
steal('steal','./post','steal/rhino/prompt.js',function(s, post){
/**
* Gets the sha of the most recent commit
*
* @param {Object} info the github repos information
* @return {String} the sha of the most recent commit (ex: 'ba7013dfaee2e503069f594ec271ec9795edb16c')
*/
var lastCommitSha = function(inf){
var lastCommitSha = function(inf, token){
var commitsText = readUrl("https://api.github.com/repos/" +
inf.user + "/" +
inf.repo + "/" +
"commits?sha=" + inf.branch);
"commits?sha=" + inf.branch + (token ? "&access_token="+token : "") );

eval("var commits = " + commitsText);
return commits[0].commit.tree.sha;
Expand All @@ -20,18 +20,26 @@ var lastCommitSha = function(inf){
* @param {Object} info the github repos information
* @param {String} sha the sha of the tree
* @param {Boolean} recursive whether we want to get the tree recursively (i.e. all tress within this tree)
* @param {String} token
* @return {String} the API url of the tree (ex: 'https://api.github.com/repos/jupiterjs/funcunit/git/trees/ba7013dfaee2e503069f594ec271ec9795edb16c')
*/
getTreeUrl = function(inf, sha, recursive){
getTreeUrl = function(inf, sha, recursive, token){
sha = !sha ? lastCommitSha(inf) : sha;
var url = "https://api.github.com/repos/" +
inf.user + "/" +
inf.repo + "/" +
"git/trees/" + sha;
var after = [];

if(recursive) {
url += "?recursive=1";
after.push("recursive=1");
}
if(token){
after.push("access_token="+token)
}
if(after.length){
url +="?"+after.join("&")
}

return url;
},
/**
Expand All @@ -42,8 +50,8 @@ getTreeUrl = function(inf, sha, recursive){
* @param {Object} info the github repos information
* @return {String} the API url of the folder (ex: 'https://api.github.com/repos/jupiterjs/funcunit/git/trees/ba7013dfaee2e503069f594ec271ec9795edb16c')
*/
getFolderUrl = function(inf) {
var repoTreeText = readUrl(getTreeUrl(inf, inf.branch, true)),
getFolderUrl = function(inf, token) {
var repoTreeText = readUrl(getTreeUrl(inf, inf.branch, true, token)),
repoTree,
folderPath = inf.resource.replace(/\/$/, ""),
sha;
Expand All @@ -57,7 +65,7 @@ getFolderUrl = function(inf) {
break;
}
}
return getTreeUrl(inf, sha);
return getTreeUrl(inf, sha, undefined, token);
},
github = s.get.git = {
/**
Expand All @@ -68,7 +76,7 @@ github = s.get.git = {
* @param {String} originalUrl the original url for the repo/folder
* @return {Object} mapping of names to urls
*/
ls : function(content, rawUrl, originalUrl){
ls : function(content, rawUrl, originalUrl, options){
var info = github.info(originalUrl),
tree,
item,
Expand Down Expand Up @@ -106,12 +114,14 @@ github = s.get.git = {
* @param {String} url url to convert
* @return {String} raw place to doanload contents or file
*/
raw : function(url){
raw : function(url, options){
var options = options || {}
github.init(options);
var info = github.info(url);
if(info.resource == "/"){ // root level folder
return getTreeUrl(info)
return getTreeUrl(info, null, null, options.token)
} else if( /\/$/.test(url) ) { // a folder
return getFolderUrl(info)
return getFolderUrl(info,options.token)
} else { // download url
return "https://raw."+info.domain+"/"+info.user+"/"+info.repo+"/"+info.branch+"/"+info.resource
}
Expand Down Expand Up @@ -153,6 +163,34 @@ github = s.get.git = {
return data;
},
getTreeUrl : getTreeUrl,
getFolderUrl : getFolderUrl
getFolderUrl : getFolderUrl,
init: function(options){
if(options.token){
return;
}
var token = readFile(".steal_git_token");
if(!token){
print("You don't have an OAuth token. I will create one for you.\n"+
"I need your github username and password just once to create one.\n"+
"I will not save your password, just the token.")
var user = s.prompt("Github Username: ")
var pass = s.promptPassword("Github Password: ")

var response = post({
url: "https://api.github.com/authorizations",
data: '{"scopes":["public_repo"],"note":"steal get"}',
username: user,
password: pass
});

eval("var data = " + response);
token = data.token;

print("Saving token at .steal_git_token. Delete it if you want to create a new one.");
s.File(".steal_git_token").save(token)

}
options.token = token;
}
};
})()
57 changes: 57 additions & 0 deletions get/post.js
@@ -0,0 +1,57 @@
steal('./base64',function(Base64){

var HttpURLConnection = java.net.HttpURLConnection,
JStr = java.lang.String,
URL = java.net.URL,
DataOutputStream = java.io.DataOutputStream,
DataInputStream = java.io.DataInputStream,
InputStreamReader = java.io.InputStreamReader,
BufferedReader = java.io.BufferedReader;


return function(request, success, error){



var urlParameters = request.data;
var myurl = new URL(request.url);

con = myurl.openConnection();
con.setRequestMethod("POST");

con.setRequestProperty("Content-length", ""+urlParameters.length);
con.setRequestProperty("Content-Type","application/json");

if(request.username){
var auth = "Basic "+ Base64.encode(request.username +":"+request.password);
con.setRequestProperty("Authorization", auth);
}


con.setDoOutput(true);
con.setDoInput(true);

var output = new DataOutputStream(con.getOutputStream());

output.writeBytes(urlParameters);

output.close();


var rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
var result = "";
while ((line = rd.readLine()) != null) {
result += line;
}

rd.close();
return result;

}



})()



0 comments on commit 4fd4ad8

Please sign in to comment.