Skip to content

Instantly share code, notes, and snippets.

@jgrevich
Created November 15, 2017 23:09
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 jgrevich/e4bacc25f8fa665f0e2fc74516b33e3e to your computer and use it in GitHub Desktop.
Save jgrevich/e4bacc25f8fa665f0e2fc74516b33e3e to your computer and use it in GitHub Desktop.
Generate phone numbers based on area-codes.com data
#!/usr/bin/env ruby
# area_coder 0.0.1
#
# area code list generator via areacodes.com
#
# USAGE:
# specify criteria for phone number list
# restrict to area code (with smart prefix lookup), city, state, or regex
# http://www.area-codes.com/state/state-CA.asp
# http://soledadpenades.com/2007/06/15/extracting-data-with-hpricot/
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'optparse'
require 'pp'
@area_codes = []
@exchanges = {}
@numbers = ""
options = {}
optparse = OptionParser.new do |opts|
# Set a banner, displayed at the top
# of the help screen.
opts.banner = "Usage: area_coder.rb [options]"
# Define the options, and what they do
options[:verbose] = false
opts.on( '-v', '--verbose', 'Output more information') do
options[:verbose] = true
end
options[:area_code] = ""
opts.on('-a', '--area_code [area_code]', 'Create phone index by area code') do |area_code|
options[:area_code] = area_code || "858"
end
options[:state] = ""
opts.on('-s', '--state [state]', 'Create phone index by state' ) do |state|
if state.length > 2
puts "\nExiting...\n"
puts "Please enter two character state code\n\n"
exit
else
options[:state] = state.gsub(" ","_").upcase || "CA"
end
end
options[:city] = ""
opts.on('-c', '--city [city]', 'Create phone index by city' ) do |city|
options[:city] = city.gsub(" ","_").upcase || "LA_JOLLA"
end
# This displays the help screen, all programs are
# assumed to have this option.
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
optparse.parse!
if ARGV.count > 0
puts "Unknown options:"
ARGV.each {|a| pp "#{a}" }
end
def get_exchanges_and_print_numbers(area_codes)
# add exchanges to array for each area code
area_codes.each do |ac|
@exchanges[('a' + ac.to_s).to_sym] = []
puts "http://www.area-codes.com/area-code/area-code-#{ac}.asp"
doc = open("http://www.area-codes.com/area-code/area-code-#{ac}.asp") {|f| Hpricot(f)}
doc.search("a[@href*=/exchange/]").each {|e| add_thousand_count(ac, e.inner_html)}
end
end
def add_thousand_count(area_code, exchange)
count = 0
until count == 9999
printf("#{area_code}#{exchange}%04d\n", count)
count += 1
end
end
if options[:verbose] == true
pp "ARGV:", ARGV
end
if options[:area_code] != ""
# open html extract for area cod
get_exchanges_and_print_numbers([options[:area_code]])
end
if options[:state] != ""
# open html extract for state
doc = open("http://www.area-codes.com/state/state-#{options[:state]}.asp") {|f| Hpricot(f)}
doc.search("a[@href*=/area-code/area-code]").each {|c| @area_codes << c.inner_html}
get_exchanges_and_print_numbers(@area_codes)
end
if options[:city] != ""
# open html extract for city
doc = open("http://www.area-codes.com/city/city.asp?city=#{options[:city]}") {|f| Hpricot(f)}
get_exchanges_and_print_numbers(@area_codes)
end
# print phone index
def generate_list
@area_codes = []
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment