Skip to content

Instantly share code, notes, and snippets.

@whym
Created April 4, 2010 06:28
Show Gist options
  • Select an option

  • Save whym/355171 to your computer and use it in GitHub Desktop.

Select an option

Save whym/355171 to your computer and use it in GitHub Desktop.
check the availability of twitter screen names
#! /usr/bin/env ruby
require 'rubygems'
require 'json'
require 'open-uri'
require 'optparse'
OPT = Struct.
new(:auto, :wait).
new(false, 0.3)
OptionParser.new do |opts|
opts.on('--[no-]auto') do |v|
OPT.auto = v
end
opts.on('--wait ', Float) do |v|
OPT.wait = v
end
end.parse!
def check(name)
url = "http://twitter.com/users/username_available?username=#{name}"
open(url) do |io|
s = io.read
res = JSON.parse(s)
res.each_pair do |k,_|
k.sub!(/"(.*)"/, '<\1>')
end
# res.to_a.map{|x| x.join(': ')}.join(', ')
STDERR.puts "#{res['valid']?'available':'unavailable'}: #{name}"
end
end
class Array
def lexicographical_each(&block)
if self.length == 0 then
return
end
self.each do |x|
block.call(x)
end
self.lexicographical_each do |y|
self.each do |x|
block.call(y+x)
end
end
end
end
wait = OPT.wait
if OPT.auto then
Enumerable::Enumerator.new(('a'..'z').to_a+('0'..'9').to_a+['_'], :lexicographical_each)
else
ARGF
end.each do |name|
name.strip!
begin
check(name)
rescue Errno::ECONNRESET, OpenURI::HTTPError
sleep(wait)
wait *= 2
redo
end
wait = OPT.wait
sleep(OPT.wait)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment