Sha256: 9622a745e6bcfeecc433863848d504f447fcab4696258fd2bc7bcf4784218255

Contents?: true

Size: 1.66 KB

Versions: 7

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require 'json'

module ProxyFetcher
  module Providers
    # GatherProxy provider class.
    class GatherProxy < Base
      # Provider URL to fetch proxy list
      PROVIDER_URL = 'http://www.gatherproxy.com/'.freeze

      # Fetches HTML content by sending HTTP request to the provider URL and
      # parses the document (built as abstract <code>ProxyFetcher::Document</code>)
      # to return all the proxy entries (HTML nodes).
      #
      # @return [Array<ProxyFetcher::Document::Node>]
      #   Collection of extracted HTML nodes with full proxy info
      #
      def load_proxy_list(*)
        doc = load_document(PROVIDER_URL)
        doc.xpath('//div[@class="proxy-list"]/table/script')
      end

      # Converts HTML node (entry of N tags) to <code>ProxyFetcher::Proxy</code>
      # object.
      #
      # @param html_node [Object]
      #   HTML node from the <code>ProxyFetcher::Document</code> DOM model.
      #
      # @return [ProxyFetcher::Proxy]
      #   Proxy object
      #
      def to_proxy(html_node)
        json = parse_json(html_node)

        ProxyFetcher::Proxy.new.tap do |proxy|
          proxy.addr = json['PROXY_IP']
          proxy.port = json['PROXY_PORT'].to_i(16)
          proxy.anonymity = json['PROXY_TYPE']
          proxy.country = json['PROXY_COUNTRY']
          proxy.response_time = json['PROXY_TIME'].to_i
          proxy.type = ProxyFetcher::Proxy::HTTP
        end
      end

      private

      def parse_json(html_node)
        javascript = html_node.content[/{.+}/im]
        JSON.parse(javascript)
      end
    end

    ProxyFetcher::Configuration.register_provider(:gather_proxy, GatherProxy)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
proxy_fetcher-0.9.0 lib/proxy_fetcher/providers/gather_proxy.rb
proxy_fetcher-0.8.0 lib/proxy_fetcher/providers/gather_proxy.rb
proxy_fetcher-0.7.1 lib/proxy_fetcher/providers/gather_proxy.rb
proxy_fetcher-0.7.0 lib/proxy_fetcher/providers/gather_proxy.rb
proxy_fetcher-0.6.5 lib/proxy_fetcher/providers/gather_proxy.rb
proxy_fetcher-0.6.4 lib/proxy_fetcher/providers/gather_proxy.rb
proxy_fetcher-0.6.3 lib/proxy_fetcher/providers/gather_proxy.rb