Skip to content

Instantly share code, notes, and snippets.

@fairchild
Created July 23, 2009 09:59
Show Gist options
  • Save fairchild/152635 to your computer and use it in GitHub Desktop.
Save fairchild/152635 to your computer and use it in GitHub Desktop.
gist_cloner github_username
#!/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