#!/usr/bin/env ruby begin require 'nokogiri' rescue LoadError puts "Seems like you haven't installed 'nokogiri' gem that is used by ProxyFetcher for HTML parsing.\n" \ "Install it with the command: `gem install nokogiri` or check out it's documentation:\n" \ 'http://www.nokogiri.org/tutorials/installing_nokogiri.html' exit(1) end require 'optparse' require 'proxy_fetcher' options = { filters: {}, validate: true, json: false } OptionParser.new do |opts| opts.banner = 'Usage: proxy_fetcher [OPTIONS]' opts.on('-h', '--help', '# Show this help message and quit') do puts opts exit(0) end opts.on('-p', '--providers=NAME1,NAME2', Array, '# Use specific proxy providers') do |values| options[:providers] = values end opts.on('-n', '--no-validate', '# Dump all the proxies without validation') do options[:validate] = false end opts.on('-f', '--filters={}', String, '# Filters for proxy provider in JSON format') do |filters| require 'json' options[:filters] = JSON.parse(filters) end opts.on('-t', '--timeout=SECONDS', Integer, '# Connection timeout in seconds') do |value| options[:timeout] = value end opts.on('-j', '--json', '# Dump proxies to the JSON format') do options[:json] = true end opts.on('-v', '--version', '# Shows gem version') do puts ProxyFetcher.gem_version.to_s exit(0) end end.parse! ProxyFetcher.config.providers = options[:providers] if options[:providers] ProxyFetcher.config.timeout = options[:timeout] if options[:timeout] manager = ProxyFetcher::Manager.new(filters: options[:filters]) manager.validate! if options[:validate] if options[:json] require 'json' puts JSON.generate(proxies: manager.raw_proxies) else puts manager.raw_proxies end