proxy_fetcher 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '01894f07ae28eafbb09934aa7a4e52188fb0bf02db7ab458df5c91a93b3e32af'
4
- data.tar.gz: b487f3aeb6b833ab6b6395d0d4c9311294f7894e68f6a51c2e9943ccf2904d09
3
+ metadata.gz: 90fedddf9296d31071b49be0c44736626987ed973b48395ccfed9b04f0c6ae4a
4
+ data.tar.gz: 900464a6378e2d72259e54022df01ec1e50ac95f76f9ec613df803ffd0cf2d78
5
5
  SHA512:
6
- metadata.gz: aef23de20b41467dc2e1cadf65e6e728555f62e3cba0006df55ec62c8b877f8ca924dff9a81441897dc8e4f3efbf346ecefad62cf0b1e5a3bbcbd78272e1ea34
7
- data.tar.gz: de41ae50f5bff9c8b8ae309057695661e76c47d2df703d369df6a2b3b40bffcfc881edc21b9833ca0f303392a480b7562b53b39d3f8c79e8e47cd0595a690366
6
+ metadata.gz: bc14dedb91c2a319aa27e3c03b3a881ac0861c378c5b1d8396ca79755f8a80ae86d28e904b999cddafc3f85ad7805ee6386e1fca327ded8a764d60ce572af4a3
7
+ data.tar.gz: 4d06f65bd36132ae0fbac2c77b12d34b31e5ec851e68995de86457e0d45f29777bcd06ff8c08c6b3e6830a18278f4984e4ca5c3573b7e564c84da155d60a8946
data/CHANGELOG.md CHANGED
@@ -6,6 +6,11 @@ Reverse Chronological Order:
6
6
 
7
7
  * Add your description here
8
8
 
9
+ ## `0.10.1` (2019-03-07)
10
+
11
+ * Fix broken ProxyDocker provider.
12
+ * Refactor gem internals.
13
+
9
14
  ## `0.9.0` (2019-01-22)
10
15
 
11
16
  * Fix a problem with stuck of proxies list loading.
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'proxy_fetcher'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -35,7 +35,11 @@ module ProxyFetcher
35
35
 
36
36
  # @!attribute [r] adapter
37
37
  # @return [Object] HTML parser adapter
38
- attr_reader :adapter
38
+ attr_accessor :adapter
39
+
40
+ # @!attribute [r] adapter_class
41
+ # @return [Object] HTML adapter class
42
+ attr_reader :adapter_class
39
43
 
40
44
  # @!attribute [r] http_client
41
45
  # @return [Object] HTTP client class
@@ -120,14 +124,11 @@ module ProxyFetcher
120
124
  self.providers = self.class.registered_providers
121
125
  end
122
126
 
123
- # Setups HTML parser adapter for all the proxy providers.
124
- #
125
- # @param name_or_class [String, Symbol, Class]
126
- # name of the adapter or it's class
127
- #
128
- def adapter=(name_or_class)
129
- @adapter = ProxyFetcher::Document::Adapters.lookup(name_or_class)
130
- @adapter.setup!
127
+ def adapter_class
128
+ return @adapter_class if defined?(@adapter_class)
129
+
130
+ @adapter_class = ProxyFetcher::Document::Adapters.lookup(adapter)
131
+ @adapter_class.setup!
131
132
  end
132
133
 
133
134
  # Setups collection of providers that will be used to fetch proxies.
@@ -17,7 +17,7 @@ module ProxyFetcher
17
17
  # ProxyFetcher document model
18
18
  #
19
19
  def self.parse(data)
20
- new(ProxyFetcher.config.adapter.parse(data))
20
+ new(ProxyFetcher.config.adapter_class.parse(data))
21
21
  end
22
22
 
23
23
  # Initialize abstract ProxyFetcher HTML Document
@@ -30,7 +30,7 @@ module ProxyFetcher
30
30
 
31
31
  case name_or_class
32
32
  when Symbol, String
33
- adapter_name = name_or_class.to_s.capitalize << ADAPTER
33
+ adapter_name = "#{name_or_class.to_s.capitalize}#{ADAPTER}"
34
34
  ProxyFetcher::Document.const_get(adapter_name)
35
35
  else
36
36
  name_or_class
@@ -52,6 +52,7 @@ module ProxyFetcher
52
52
  #
53
53
  def self.setup!(*args)
54
54
  install_requirements!(*args)
55
+ self
55
56
  rescue LoadError => error
56
57
  raise Exceptions::AdapterSetupError.new(name, error.message)
57
58
  end
@@ -74,7 +74,10 @@ module ProxyFetcher
74
74
  def build_proxy(*args)
75
75
  to_proxy(*args)
76
76
  rescue StandardError => error
77
- ProxyFetcher.logger.warn("Failed to build Proxy object due to error: #{error.message}")
77
+ ProxyFetcher.logger.warn(
78
+ "Failed to build Proxy object for #{self.class.name} due to error: #{error.message}"
79
+ )
80
+
78
81
  nil
79
82
  end
80
83
 
@@ -6,6 +6,8 @@ module ProxyFetcher
6
6
  module Providers
7
7
  # ProxyDocker provider class.
8
8
  class ProxyDocker < Base
9
+ attr_reader :cookie, :token
10
+
9
11
  # Provider URL to fetch proxy list
10
12
  def provider_url
11
13
  'https://www.proxydocker.com/en/api/proxylist/'
@@ -17,7 +19,7 @@ module ProxyFetcher
17
19
 
18
20
  def provider_params
19
21
  {
20
- token: 'GmZyl0OJmmgrWakdzO7AFf6AWfkdledR6xmKvGmwmJg',
22
+ token: @token,
21
23
  country: 'all',
22
24
  city: 'all',
23
25
  state: 'all',
@@ -31,7 +33,7 @@ module ProxyFetcher
31
33
 
32
34
  def provider_headers
33
35
  {
34
- cookie: 'PHPSESSID=7f59558ee58b1e4352c4ab4c2f1a3c11'
36
+ cookie: @cookie
35
37
  }
36
38
  end
37
39
 
@@ -44,6 +46,8 @@ module ProxyFetcher
44
46
  #
45
47
  # [NOTE] Doesn't support direct filters
46
48
  def load_proxy_list(*)
49
+ load_dependencies
50
+
47
51
  json = JSON.parse(load_html(provider_url, {}))
48
52
  json.fetch('proxies', [])
49
53
  rescue JSON::ParserError
@@ -70,6 +74,8 @@ module ProxyFetcher
70
74
  end
71
75
  end
72
76
 
77
+ private
78
+
73
79
  def types_mapping
74
80
  {
75
81
  '16' => ProxyFetcher::Proxy::HTTP,
@@ -80,6 +86,24 @@ module ProxyFetcher
80
86
  '6' => ProxyFetcher::Proxy::HTTP # CON80
81
87
  }
82
88
  end
89
+
90
+ def load_dependencies
91
+ client = ProxyFetcher.config.http_client.new('https://www.proxydocker.com')
92
+ response = client.fetch_with_headers
93
+
94
+ @cookie = load_cookie_from(response)
95
+ @token = load_token_from(response)
96
+ end
97
+
98
+ def load_cookie_from(response)
99
+ cookie_headers = (response.headers['Set-Cookie'] || [])
100
+ cookie_headers.join('; ')
101
+ end
102
+
103
+ def load_token_from(response)
104
+ html = response.body.to_s
105
+ html[/meta\s+name\s*=["']_token["']\s+content.+["'](.+?)["']\s*>/i, 1]
106
+ end
83
107
  end
84
108
 
85
109
  ProxyFetcher::Configuration.register_provider(:proxy_docker, ProxyDocker)
@@ -68,21 +68,32 @@ module ProxyFetcher
68
68
  # response body
69
69
  #
70
70
  def fetch
71
- # TODO: must be more generic
72
- response = if method == :post
73
- http.post(url, form: params, ssl_context: ssl_ctx)
74
- else
75
- http.get(url, ssl_context: ssl_ctx)
76
- end
77
-
71
+ response = process_http_request
78
72
  response.body.to_s
79
73
  rescue StandardError => error
80
- ProxyFetcher.logger.warn("Failed to load proxy list for #{url} (#{error.message})")
74
+ ProxyFetcher.logger.warn("Failed to process request to #{url} (#{error.message})")
81
75
  ''
82
76
  end
83
77
 
78
+ def fetch_with_headers
79
+ process_http_request
80
+ rescue StandardError => error
81
+ ProxyFetcher.logger.warn("Failed to process request to #{url} (#{error.message})")
82
+ HTTP::Response.new(version: '1.1', status: 500, body: '')
83
+ end
84
+
84
85
  protected
85
86
 
87
+ def process_http_request(http_method: method, http_params: params)
88
+ raise ArgumentError, 'wrong http method name!' unless HTTP::Request::METHODS.include?(http_method)
89
+
90
+ http.public_send(
91
+ http_method.to_sym, url,
92
+ form: http_params,
93
+ ssl_context: ssl_ctx
94
+ )
95
+ end
96
+
86
97
  # Default HTTP client headers
87
98
  #
88
99
  # @return [Hash]
@@ -15,7 +15,7 @@ module ProxyFetcher
15
15
  # Minor version number
16
16
  MINOR = 10
17
17
  # Smallest version number
18
- TINY = 0
18
+ TINY = 1
19
19
 
20
20
  # Full version number
21
21
  STRING = [MAJOR, MINOR, TINY].compact.join('.')
@@ -68,7 +68,9 @@ describe ProxyFetcher::Configuration do
68
68
  end
69
69
  end
70
70
 
71
- expect { ProxyFetcher.config.adapter = CustomAdapter }
71
+ ProxyFetcher.config.adapter = CustomAdapter
72
+
73
+ expect { ProxyFetcher::Manager.new }
72
74
  .to raise_error(ProxyFetcher::Exceptions::AdapterSetupError)
73
75
 
74
76
  ProxyFetcher.instance_variable_set('@config', old_config)
@@ -44,7 +44,7 @@ describe ProxyFetcher::Providers::Base do
44
44
 
45
45
  allow_any_instance_of(HTTP::Client).to receive(:get).and_raise(StandardError)
46
46
 
47
- expect(logger).to receive(:warn).with(/Failed to load proxy list for http[s:\/]/)
47
+ expect(logger).to receive(:warn).with(/Failed to process request to http[s:\/]/)
48
48
 
49
49
  ProxyFetcher::Manager.new
50
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proxy_fetcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikita Bulai
@@ -56,6 +56,7 @@ description: This gem can help your Ruby application to make HTTP(S) requests us
56
56
  proxies by fetching and validating proxy lists from the different providers.
57
57
  email: bulajnikita@gmail.com
58
58
  executables:
59
+ - console
59
60
  - proxy_fetcher
60
61
  extensions: []
61
62
  extra_rdoc_files: []
@@ -66,6 +67,7 @@ files:
66
67
  - Gemfile
67
68
  - LICENSE
68
69
  - Rakefile
70
+ - bin/console
69
71
  - bin/proxy_fetcher
70
72
  - gemfiles/nokogiri.gemfile
71
73
  - gemfiles/oga.gemfile