Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a file via GitHub APIs

Tags:

github

api

I need to get the contents of a file hosted in a GitHub repo. I'd prefer to get a JSON response with metadata along with it. I've tried numerous URLs with cURL with to only get a response of {"message":"Not Found"}. I just need the URL structure. If it matters, it's from an organization on GitHub. Here's what I think should work but doesn't:

http://api.github.com/repos/<organization>/<repository>/git/branches/<branch>/<file>
like image 332
Tyler Crompton Avatar asked Feb 14 '12 06:02

Tyler Crompton


People also ask

How do I get a file from GitHub?

To download from GitHub, you should navigate to the top level of the project (SDN in this case) and then a green "Code" download button will be visible on the right. Choose the Download ZIP option from the Code pull-down menu. That ZIP file will contain the entire repository content, including the area you wanted.

How do I use GitHub API?

Prepend the base URL for the GitHub REST API, https://api.github.com , to the path to get the full URL: https://api.github.com/octocat . Use the curl command in your command line. Use the --request or -X flag followed by the HTTP method. Use the --url flag followed by the full URL.

How do I get files from GitHub Linux?

In order to get the actual file, you can get a raw file from github instead. Copy the URL of the raw file and then use the wget or curl command to download the file. This will download the raw php file and not the HTML wrapped one.


2 Answers

As the description (located at http://developer.github.com/v3/repos/contents/) says:

/repos/:owner/:repo/contents/:path

An ajax code will be:

$.ajax({
    url: readme_uri,
    dataType: 'jsonp',
    success: function(results)
    {
        var content = results.data.content;
    });

Replace the readme_uri by the proper /repos/:owner/:repo/contents/:path.

like image 123
atejeda Avatar answered Sep 27 '22 19:09

atejeda


This GitHub API page provides the full reference. The API endpoint for reading a file:

https://api.github.com/repos/{username}/{repository_name}/contents/{file_path}
{
  "encoding": "base64",
  "size": 5362,
  "name": "README.md",
  "content": "encoded content ...",
  "sha": "3d21ec53a331a6f037a91c368710b99387d012c1",
  ...
}
  • Consider using a personal access token
    • Rate-limits (up to 60 per-hour for anonymous, up to 5,000 per-hour for authenticated) read more
    • Enable accessing files in private repos
  • The file content in the response is base64 encoded string

Using curl, jq

Reading https://github.com/airbnb/javascript/blob/master/package.json using GitHub's API via curl and jq:

curl https://api.github.com/repos/airbnb/javascript/contents/package.json | jq -r ".content" | base64 --decode

Using Python

Reading https://github.com/airbnb/javascript/blob/master/package.json using GitHub's API in Python:

import base64
import json
import requests
import os


def github_read_file(username, repository_name, file_path, github_token=None):
    headers = {}
    if github_token:
        headers['Authorization'] = f"token {github_token}"
        
    url = f'https://api.github.com/repos/{username}/{repository_name}/contents/{file_path}'
    r = requests.get(url, headers=headers)
    r.raise_for_status()
    data = r.json()
    file_content = data['content']
    file_content_encoding = data.get('encoding')
    if file_content_encoding == 'base64':
        file_content = base64.b64decode(file_content).decode()

    return file_content


def main():
    github_token = os.environ['GITHUB_TOKEN']
    username = 'airbnb'
    repository_name = 'javascript'
    file_path = 'package.json'
    file_content = github_read_file(username, repository_name, file_path, github_token=github_token)
    data = json.loads(file_content)
    print(data['name'])


if __name__ == '__main__':
    main()
  • Define an environment variable GITHUB_TOKEN before running
like image 41
Jossef Harush Kadouri Avatar answered Sep 27 '22 19:09

Jossef Harush Kadouri