Skip to content

Instantly share code, notes, and snippets.

@alotaiba
Created February 19, 2012 23:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alotaiba/1866399 to your computer and use it in GitHub Desktop.
Save alotaiba/1866399 to your computer and use it in GitHub Desktop.
Simple ruby script that fetches all github gists that belong to an authenticated user
#!/usr/bin/env ruby
## Grab Gists
## Copyright (c) Abdulrahman Saleh Khamis Alotaiba
## Licensed under BSD
##
## This is a simple ruby script that fetches all github gists
## that belong to the authenticated user, which is defined
## in GITHUB_USER variable, along with its password GITHUB_PASSWORD
##
## To use it, download it, chmod a+x to make it excutable, then run it
## $ ./grab_gists.rb -d <output_directory>
## where <output_directory> is the directory where you want to save
## your gists. The directory will be created if one doesn't exist.
# -- You may begin your editing now! -- #
GITHUB_USER = "<your_username>"
GITHUB_PASSWORD = "<your_password>"
# -- Hold your horses there knight, you reached the final editing line -- #
require 'httparty'
require 'optparse'
require 'fileutils'
VERSION = "0.0.2"
class OptionsParser
def self.parse(args)
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage: grab_gists -d <output_directory>"
opts.on("-d", "--directory DIRECTORY", "The directory where you will grab all the gists in") do |directory|
options[:directory] = directory
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("--version", "Show version") do
puts "Copyright (c) Abdulrahman Saleh Khamis Alotaiba"
puts "Version #{VERSION}"
exit
end
end
opts.parse!(args)
options
end
end
class Github
include HTTParty
base_uri 'https://api.github.com'
basic_auth GITHUB_USER, GITHUB_PASSWORD
def self.gists
get('/gists')
end
end
class Grabber
attr_accessor :directory
def initialize(directory)
self.directory = directory
Dir.mkdir(self.directory) unless File.directory?(self.directory)
end
def gists
gist_list = []
Github.gists.map{|gist|
# Limit to 15 words, replace spaces with dashes, remove non ASCII words
# puts gist['description'].downcase.strip.split.slice(0, 15).join('-').gsub(/[^\w-]/, '')
git_working_dir = File.join(self.directory, gist['id'])
if File.directory?(git_working_dir)
ENV['GIT_DIR'] = File.join(git_working_dir, '.git')
system("git pull")
ENV['GIT_DIR'] = nil
else
system("git clone #{gist['git_push_url']} #{git_working_dir}")
end
json_info_file = File.join(self.directory, "#{gist['id']}.info.json")
File.open(json_info_file, "w") {|file|
file.write(gist.to_s)
}
gist_list.push(gist['id'])
}
Dir.foreach(self.directory) {|x|
if not x.eql?"." and not x.eql?".." and not gist_list.any? { |item| x.include? item }
FileUtils.rm_r File.join(self.directory, x), :force => true, :secure => true
end
}
end
end
options = OptionsParser.parse(ARGV)
raise OptionParser::MissingArgument if options[:directory].nil?
grab_them = Grabber.new(options[:directory])
grab_them.gists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment