Created
June 21, 2017 15:13
-
-
Save scripting/371bcc9ef608a785068421e724cfb263 to your computer and use it in GitHub Desktop.
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
<html> | |
<head> | |
<title>GitHub API: Get repo directory</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<script src="http://fargo.io/code/jquery-1.9.1.min.js"></script> | |
<link href="http://fargo.io/code/bootstrap.css" rel="stylesheet"> | |
<script src="http://fargo.io/code/bootstrap.min.js"></script> | |
<link href="http://fargo.io/code/ubuntuFont.css" rel="stylesheet" type="text/css"> | |
<script> | |
/* | |
A sample we app that travels through the River5 repository on my GitHub account, | |
producing a directory structure that reflects the structure of the repo. | |
I couldn't find sample code that does this simple thing, now I won't have to hunt | |
for it, and neither will you. ;-) | |
You can run this app here: | |
http://scripting.com/misc/code/githubapi/directory.html | |
Dave Winer | |
June 21, 2017 | |
*/ | |
function readHttpFile (url, callback, timeoutInMilliseconds, headers) { //5/27/14 by DW | |
Changes | |
7/17/15; 10:43:16 AM by DW | |
New optional param, headers. | |
12/14/14; 5:38:18 PM by DW | |
Add optional timeoutInMilliseconds param. | |
5/29/14; 11:13:28 AM by DW | |
On error, call the callback with an undefined parameter. | |
5/27/14; 8:31:21 AM by DW | |
Simple asynchronous file read over http. | |
if (timeoutInMilliseconds === undefined) { | |
timeoutInMilliseconds = 30000; | |
} | |
var jxhr = $.ajax ({ | |
url: url, | |
dataType: "text", | |
headers: headers, | |
timeout: timeoutInMilliseconds | |
}) | |
.success (function (data, status) { | |
callback (data); | |
}) | |
.error (function (status) { | |
for info about timeous see this page. | |
http://stackoverflow.com/questions/3543683/determine-if-ajax-error-is-a-timeout | |
console.log ("readHttpFile: url == " + url + ", error == " + jsonStringify (status)); | |
callback (undefined); | |
}); | |
} | |
function getDirectoryStruct (userName, repositoryName, callback) { | |
var baseUrl = "https://api.github.com/repos/" + userName + "/" + repositoryName + "/contents"; | |
function readDirectory (url, callback) { | |
readHttpFile (url, function (jsontext) { | |
var jstruct = JSON.parse (jsontext), theDirectory = new Array (); | |
function doNextFile (ix) { | |
if (ix < jstruct.length) { | |
var file = jstruct [ix]; | |
if (file.type == "dir") { | |
readDirectory (file.url, function (theSubDirectory) { | |
var obj = { | |
name: file.name, | |
subs: theSubDirectory | |
}; | |
theDirectory.push (obj); | |
doNextFile (ix + 1); | |
}); | |
} | |
else { | |
var obj = { | |
name: file.name, | |
url: file.download_url | |
}; | |
theDirectory.push (obj); | |
doNextFile (ix + 1); | |
} | |
} | |
else { | |
callback (theDirectory); | |
} | |
} | |
doNextFile (0); | |
}); | |
} | |
readDirectory (baseUrl, function (theDirectory) { | |
callback (theDirectory); | |
}); | |
} | |
function startup () { | |
console.log ("startup"); | |
getDirectoryStruct ("scripting", "river5", function (jstruct) { | |
$("#idDirectoryJson").text (JSON.stringify (jstruct, undefined, 4)); | |
}); | |
} | |
</script> | |
<style> | |
body { | |
font-family: Ubuntu; | |
font-size: 18px; | |
background-color: whitesmoke; | |
} | |
.divPageBody { | |
width: 60%; | |
margin-top: 10px; | |
margin-left: auto; | |
margin-right: auto; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="divPageBody"> | |
<pre id="idDirectoryJson"> | |
</pre> | |
</div> | |
<script> | |
$(document).ready (function () { | |
startup (); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment