Does GitHub have a jsonp api for the source of a file? I know BitBucket has, but I can't find any information for GitHubs (assuming they have one).
Do they not? If not, then bummer...
Does GitHub have a jsonp api for the source of a file? I know BitBucket has, but I can't find any information for GitHubs (assuming they have one).
Do they not? If not, then bummer...
There is an API to get the contents of data from github. It's part of the v3 github API.
You make a request to
https://api.github.com/repos/{username}/{repository name}/contents/{filepath and name}
e.g. https://api.github.com/repos/mono/monodevelop/contents/README
Unless you set the accepts header, you'll receive back some JSON with the file contents encoded in base64. You'll have to decode this, something that is very easy in node.js, but more of a pain in the browser. You can find base64 decoders in javascript in other questions on stackoverflow fairly easily. One thing to notice, the base64 code you get back from github has newline characters in it to make it format nicely and many base64 decoders can't cope with newlines, so you might need to remove them or modify the decoder.
You probably just want the content and don't need the other stuff in the json (such as sha and length, etc), so you can make your life easier by setting the Accept header to application/vnd.github.3.raw
.
Here's an example with the accepts header using curl:
curl -i https://api.github.com/repos/mono/monodevelop/contents/README --header "Accept: application/vnd.github.3.raw"
Now, if you're using node or curl, that's probably fine, but if you're running inside the browser, to do that you'll need to use CORS. Github only allow access from hosts that are registered as OAuth Applications. It's not particularly difficult to do this, but for my usecase (a bookmarketlet), that wasn't an option.
There is a way to get access without using CORS, and that is with JSONP, you can add e.g. ?callback=_processGithubResponse
to get javascript output suitable for including with a script tag (that calls a function called _processGithubResponse with the response). Unfortunately you can't set an accepts header on that, so you're stuck with decoding base64 in this case.
If you are using node.js, I would recommend you use node-github which makes the API slightly easier to use.
This is an old question nowadays you can get to the source through: https://raw.githubusercontent.com
https://raw.githubusercontent.com/ user / repository / branch / directory / file
A stated in the GitHub API documentation, any call supports the JSONP invokation mode:
You can send a
?callback
parameter to any GET call to have the results wrapped in a JSON function. This is typically used when browsers want to embed GitHub content in web pages by getting around cross domain issues. The response includes the same data output as the regular API, plus the relevant HTTP Header information.
If order to retrieve the current source of a file (or any version of it), you have to either know the SHA
of the Blob
it is being stored in. See the Git Database API for further information about this topic.
More than often, one doesn't know the SHA, but only the relative path to the file in the working directory.
Then you'll have to follow the following steps
Pick a commit sha from the list
Retrieve the tree it points to and recursively list every entry (Trees and Blobs)
Find your the Blob matching the searched path, find out its SHA and apply first process
kybernetikos has a great answer, but if you want a quicker and easier set up, you can also try RawGit https://rawgit.com/
It serves raw files directly from GitHub with proper Content-Type headers. Just paste in your file or gist url, and it gives you API urls for both production and development.
I don't think GitHub has an API to extract the source of a file. They do have jsonp callbacks though by specifying the callback=funciton after any API call (i.e., curl https://api.github.com?callback=foo
)
You could probably use the Trees API to find the source files present in a repository at a certain commit (HEAD of the master branch). Then you can just acquire the source by using the URL for the raw version of the file (i.e., https://raw.github.com/robbyrussell/oh-my-zsh/master/lib/directories.zsh)