Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download directory from GitHub through API
class GitHub
class << self
def download_file_to_string(github_token, setup={}, raw=true, link=nil)
if link
url = link
else
sql_repo_name = setup['repo_name']
sql_repo_owner = setup['repo_owner']
# URI join won't make it:
sql_path = URI.escape(setup['path'])
sql_path = "/#{sql_path}" if sql_path[0] != '/'
revision = setup['revision']
url = "https://api.github.com/repos/#{sql_repo_owner}/#{sql_repo_name}/contents#{sql_path}"
url = "#{url}?ref=#{revision}" if revision
end
puts "Fetching from #{url}..."
format = raw ? 'raw' : 'json'
# get the file content from github
begin
downloaded_string = RestClient.get(url, {:accept => "application/vnd.github.v3.#{format}", :authorization => "token #{github_token}"})
rescue RestClient::ResourceNotFound => e
raise_arg_error(e, "The url '#{url}' doesn't exist in github. Check your repo_name '#{setup['repo_name']}', repo_owner '#{setup['repo_owner']}' and/or path '#{setup['path']}'.")
rescue RestClient::Unauthorized => e
raise_arg_error(e, "The github_token is wrong.")
end
downloaded_string
end
def download_directory(github_token, setup, target_dir, link=nil)
# create a dir for the contents
# create the target dir and make sure it's absolute path
FileUtils.mkdir_p(target_dir)
target_dir = File.expand_path(target_dir)
# see what is there
json_str = download_file_to_string(github_token, setup, false, link)
# parse it
contents = JSON.parse(json_str)
contents.each do |dir_item|
# if it's a file download it and write it to file
if dir_item['type'] == 'file'
str = download_file_to_string(github_token, {}, true, dir_item['url'])
target_filename = File.join(target_dir, dir_item['name'])
IO.write(target_filename, str)
elsif dir_item['type'] == 'dir'
download_directory(github_token, {}, File.join(target_dir, dir_item['name']), dir_item['url'])
else
fail "Some weird directory item type: #{dir_item}"
end
end
target_dir
end
def raise_arg_error(e, message='')
raise ArgumentError, "#{e.message}\n #{message}"
end
end
end
# Usage:
GitHub.download_directory('my github token', {'repo_name' => 'Test', 'repo_owner' => 'foobar', 'path' => '/foo'}, 'here_please')
@cvengros

This comment has been minimized.

Copy link
Owner Author

commented Nov 15, 2014

To get your github token: https://help.github.com/articles/creating-an-access-token-for-command-line-use/
If you're downloading a public repo pass nil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.