Last active
April 4, 2018 09:14
-
-
Save AlexanderMint/b6556af27adf9add54066bb54862914b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require 'faraday' | |
require 'colorize' | |
require 'json' | |
require 'yaml' | |
class DomainChecker | |
LENGTH = 2..3 | |
TLD = %w[com net ru io].freeze | |
LOGIN='<LOGIN>' | |
PASSWORD='<PASSWORD>' | |
API_CALLBACK='https://api.reg.ru/api/regru2' | |
def initialize | |
@conn = Faraday.new url: API_CALLBACK do |config| | |
config.request :url_encoded | |
config.adapter Faraday.default_adapter | |
end | |
@domains = gen_domains.map { |d| { dname: d } } | |
end | |
def perform | |
result = @domains.each_slice(2600).to_a.map do |domain_pack| | |
puts [domain_pack.first[:dname], domain_pack.last[:dname]].join '...' | |
checker(domain_pack) | |
end | |
File.write("#{Time.new}_domains.yml", result.to_yaml) | |
end | |
private | |
def gen_names | |
LENGTH.to_a.map { |i| (('a' * i)..('z' * i)).to_a }.flatten | |
end | |
def gen_domains | |
gen_names.map do |name| | |
TLD.map { |tld| [name, tld].join '.' } | |
end.flatten | |
end | |
def checker(domains) | |
request = @conn.post do |req| | |
req.url 'domain/check' | |
req.options.timeout = 600 | |
req.headers['User-Agent'] = 'Mozilla/5.0' | |
req.body = { | |
username: LOGIN, | |
password: PASSWORD, | |
input_data: { domains: domains }.to_json, | |
input_format: :json, | |
premium_as_taken: 1 | |
} | |
end | |
parse_domain(request) | |
rescue => e | |
puts 'Timeout 5 minuts...'.colorize(:orange) | |
sleep 60*5 | |
checker(domains) | |
end | |
def parse_domain(request) | |
data = JSON.parse(request.body)['answer']['domains'] | |
result = data.map do |domain| | |
next unless domain['result'] == 'Available' | |
puts domain['dname'].colorize(:green) | |
domain['dname'] | |
end.compact | |
end | |
end | |
DomainChecker.new.perform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment