Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kitallis
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitallis/88d68e2acb0da38cf975 to your computer and use it in GitHub Desktop.
Save kitallis/88d68e2acb0da38cf975 to your computer and use it in GitHub Desktop.
populates a list of domains and their statuses by joining dictionary words and tlds
#!/usr/bin/env ruby
require 'securerandom'
require 'fileutils'
require 'open-uri'
require 'whois'
def domain_status(domain)
sleep(1.5) # to prevent throttling, if any
begin
r = Whois.whois(domain)
r.available? ? 'green' : 'red'
rescue Whois::ParserError, Whois::NoInterfaceError, Whois::WebInterfaceError,
Whois::ServerNotFound, Timeout::Error, Whois::ResponseIsThrottled, Whois::ConnectionError
'darkorange'
end
end
def find_useful_tlds
tld_filters = [/^#/, /^XN--/]
tlds = open('http://data.iana.org/TLD/tlds-alpha-by-domain.txt').read.split("\n")
tlds.inject([]) do |acc, tld|
tld = tld.strip
unless tld_filters.any? { |filter| tld =~ filter }
acc << tld.downcase
end
acc
end
end
def read_word_file(path)
words = []
return words unless File.exist?(path)
File.open(path, 'r') do |f|
f.each_line do |line|
words << line.strip.downcase
end
end
words
end
#
# /usr/share/dict/propernames only appears to exist on OS X
# most other *nix have those in the regular /usr/share/dict/words file itself
#
def load_words
read_word_file('/usr/share/dict/words') +
read_word_file('/usr/share/dict/propernames')
end
def write_file_with_lock(path, ext)
store_path = path + '.' + SecureRandom.hex(5) + ext
file = File.open(store_path, 'w:UTF-8')
file.flock(File::LOCK_EX)
trap('INT') do
puts 'Shutting down...'
file.flock(File::LOCK_UN)
file.close
exit
end
yield(file)
basename = File.basename(file)
FileUtils.cp basename, path + ext
file.flock(File::LOCK_UN)
file.close
FileUtils.rm basename, :force => true
end
def print_view_file(useful_tlds, words)
write_file_with_lock('derange', '.html') do |file|
file.write('<!DOCTYPE html><head><meta charset="UTF-8">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"><title>dérange</title></head>')
heading = <<-HEAD
<div style="margin:0 auto;">
<div style="border-bottom:1px dotted black;margin-bottom:25px">
<h1 style="margin:0;padding:0">dérange</h1>
<div style="margin-bottom:15px">
populates a list of domains and their statuses by joining dictionary words and top-level domains
</div>
<span style="color:green">available</span>
<span>•</span>
<span style="color:red;margin-left:1px">unavailable</span>
<span>•</span>
<span style="color:darkorange;margin-left:1px">dunno</span>
<span style="margin-left:5px;float:right">
<a href='https://gist.github.com/kitallis/88d68e2acb0da38cf975'>code</a>
</span>
<span style="margin-left:5px;float:right">•</span>
<span style="margin-left:5px;float:right">#{Time.now.strftime('%d/%b/%Y')} (will rerun every 15 days)</span>
<span style="margin-left:5px;float:right;font-weight:600">
last update started on:
</span>
</div>
</div>
HEAD
file.write(heading)
file.flush
words.each do |word|
useful_tlds.each do |tld|
if word.strip.end_with? tld.strip
domain = String.new(word).insert(-1 - tld.length, '.')
next if domain.split('.').first.length < 1
line = <<-LINE
<div style="color:#{domain_status(domain)};margin-left:5px;">
#{domain}
</div>
LINE
file.write(line)
file.flush # flush immediately so that STDOUT doesn't buffer and a page refresh can show new results
end
end
end
end
end
print_view_file(find_useful_tlds, load_words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment