-
-
Save eiffelqiu/979936 to your computer and use it in GitHub Desktop.
gist_cloner github_username
This file contains 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'fileutils' | |
require 'net/http' | |
require 'uri' | |
require "open-uri" | |
require 'nokogiri' | |
=begin rdoc | |
Clone all the public gists of a user. | |
Place each gist in a folder named for the gist_id | |
=end | |
def download_gists(username, page=1) | |
puts "-- Downloading page #{page} of gists --" | |
url = URI.parse("http://gist.github.com") | |
res = Net::HTTP.start(url.host, url.port) do |http| | |
response = http.get("/#{username}?page=#{page}") | |
if response.code == '200' | |
links = get_links(response.body) | |
links.each do |link, gist_id| | |
puts "git://gist.github.com/#{gist_id}.git" | |
if File.directory?(gist_id) | |
`cd #{gist_id} && git pull ; cd ..` | |
else | |
`git clone git://gist.github.com/#{gist_id}.git #{gist_id}` | |
end | |
end | |
download_gists(username, page+1) unless links.empty? | |
end | |
end | |
end | |
def get_links(page, domain=/http:\/\/.*\.\w{2,}\//) | |
doc = Nokogiri::HTML(page) | |
doc.css('a').inject({}) do |links, a| | |
u = a.attributes['href'].content if a.attributes['href'] | |
links[u] = $+ if u.match(/\/(\d+$)/) | |
links | |
end | |
end | |
if ARGV.empty? | |
puts "Usage: gist_cloner.rb github_username" | |
else | |
FileUtils.mkdir ARGV.last | |
FileUtils.chdir ARGV.last | |
download_gists ARGV.first | |
puts '# done' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment