phantom_mechanize 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZWQyMzY1ODA2NWYxOWY4ZWNlNmYzOTNjOGJmNDdjMzliYjBkODhjNw==
5
+ data.tar.gz: !binary |-
6
+ Yjk2OTc1M2UxNjQ0ZjBiN2Q2NWMxZTk1NDQwMWU0Y2EyNTg2ZWUyYQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NjdjNGQxNjJjNjg4MzdhNzQ0YzcyZTY2ZDcyMWU5ZTYzMzExNzhhNjgxZTc5
10
+ Njk5Zjc5YTYzMGVhNjg4NTZmZDVmZTBkZTc3MTZjNWQyYzI1OWZkODQyZTcz
11
+ MjQzY2UwZDRiOTk1NDM1NTAzNGViZjVjZThhOTlmOWY3YjgxOTY=
12
+ data.tar.gz: !binary |-
13
+ ODNhZDVhZTAwOTExNTY1MjlhNTlhNTVjMzE1ZmViMDhhYjRiZWVkMGViOTVm
14
+ ZmFlN2E3MjFhNDljNWIzYjMzMTFlY2ZjMzQzZDNiNjkzZTYxOGZjZjQ5NjM3
15
+ NGE4ODBmMmQ4YWVlZDY2MTIxOGQxZmJmNWUyMWE1MGRmYzNmM2Q=
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in phantom_mechanize.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 P Guardiario
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # PhantomMechanize
2
+
3
+ A Phantomjs addon for Ruby Mechanize
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'phantom_mechanize'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install phantom_mechanize
20
+
21
+ ## Usage
22
+ ```ruby
23
+ require 'phantom_mechanize'
24
+ agent = Mechanize.new
25
+ page = agent.phget 'http://www.google.com', :wait => 10000, :selector => '[name=q]'
26
+ ```
27
+ ## Options
28
+ * :selector - return once this selector is located (jquery)
29
+ * :wait - wait this many milliseconds (default 10,000)
30
+
31
+ ## Faq
32
+ > What about cookies?
33
+
34
+ Cookies get sent to Phantomjs and reloaded into Mechanize when it returns.
35
+ > What about user_agent?
36
+
37
+ Phantomjs will use the same user agent as Mechanize
38
+ > What about http proxy?
39
+
40
+ Phantomjs will use the same http proxy as Mechanize
41
+
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/monkeysuffrage/phantom_mechanize/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/js/phget.js ADDED
@@ -0,0 +1,60 @@
1
+ var args = require('system').args;
2
+
3
+ var url = args[1];
4
+ var timeout = parseInt(args[2]);
5
+ var selector = args[3];
6
+
7
+ var cookies = JSON.parse(args[4]);
8
+ var user_agent = args[5];
9
+
10
+ // var date =
11
+ for(i in cookies){
12
+ cookie = cookies[i]
13
+ phantom.addCookie({
14
+ 'name' : cookie[0],
15
+ 'value' : cookie[1],
16
+ 'domain' : cookie[2],
17
+ 'path' : cookie[3],
18
+ 'httponly' : cookie[4],
19
+ 'secure' : cookie[5],
20
+ 'expires' : new Date(cookie[6] * 1000)
21
+ });
22
+ }
23
+
24
+ var page = require('webpage').create();
25
+
26
+ setInterval(function() {
27
+ console.log(JSON.stringify(phantom.cookies, null, 2) + '<<<phget_separator>>>' + page.content);
28
+ phantom.exit();
29
+ }, timeout);
30
+
31
+ page.settings.userAgent = user_agent;
32
+
33
+ page.onConsoleMessage = function(msg, lineNum, sourceId) {
34
+ console.log(JSON.stringify(phantom.cookies, null, 2) + '<<<phget_separator>>>' + msg);
35
+ phantom.exit();
36
+ };
37
+
38
+ page.onResourceRequested = function(requestData, networkRequest) {
39
+ if(requestData.url.match(/.(gif|jpe?g|png|css)/)) {
40
+ networkRequest.abort();
41
+ }
42
+ };
43
+
44
+ page.onLoadFinished = function() {
45
+ if(selector != ''){
46
+ page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
47
+ page.evaluate(function(selector, page) {
48
+ setInterval(function() {
49
+ if($(selector)[0]){
50
+ console.log(page.content);
51
+ }
52
+ }, 500);
53
+ }, selector, page);
54
+ });
55
+ }
56
+ };
57
+
58
+ page.open(url, function() {
59
+
60
+ });
@@ -0,0 +1,21 @@
1
+ class Mechanize
2
+ def phget url, *args
3
+ args = args[0] || {}
4
+ wait = args[:wait] || 10000
5
+ selector = args[:selector] || ""
6
+
7
+ pc = cookies.map{|c| [c.name, c.value, c.domain, c.path, c.httponly, c.secure, c.expires.to_i]}.to_json
8
+
9
+ ph_args = []
10
+ ph_args << "--proxy=#{proxy_addr}:#{proxy_port}" if proxy_port && proxy_addr
11
+
12
+ response = `phantomjs #{ph_args.join(' ')} "#{PhantomMechanize::JS_FOLDER}/phget.js" "#{url}" "#{wait}" "#{selector.gsub('"', '\"')}" "#{pc.gsub('"', '\"')}" "#{user_agent.gsub('"', '\"')}"`
13
+ mcs, html = response.split '<<<phget_separator>>>'
14
+ JSON.parse(mcs).each do |mc|
15
+ cookie = Cookie.new Hash[mc.map{|k, v| [k.to_sym, v]}]
16
+ cookie_jar << cookie
17
+ end
18
+
19
+ page = Mechanize::Page.new URI.parse(url), [], html, nil, self
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module PhantomMechanize
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ require "mechanize"
2
+ require "phantom_mechanize/version"
3
+ require "phantom_mechanize/ext/mechanize"
4
+
5
+ module PhantomMechanize
6
+ JS_FOLDER = Gem.loaded_specs["phantom_mechanize"].gem_dir + '/js'
7
+ end
8
+
9
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
10
+ unless ENV['PATH'].split(File::PATH_SEPARATOR).any?{|x| exts.find{|ext| File.exists? File.join(x, "phantomjs#{ext}")}}
11
+ raise 'Phantomjs not found in path'
12
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'phantom_mechanize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "phantom_mechanize"
8
+ spec.version = PhantomMechanize::VERSION
9
+ spec.authors = ["P Guardiario"]
10
+ spec.email = ["pguardiario@gmail.com"]
11
+ spec.summary = "A Phantomjs addon for Ruby Mechanize"
12
+ spec.description = "A Phantomjs addon for Ruby Mechanize"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib", "js"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ # spec.add_development_dependency "pry"
24
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phantom_mechanize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - P Guardiario
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A Phantomjs addon for Ruby Mechanize
42
+ email:
43
+ - pguardiario@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - js/phget.js
54
+ - lib/phantom_mechanize.rb
55
+ - lib/phantom_mechanize/ext/mechanize.rb
56
+ - lib/phantom_mechanize/version.rb
57
+ - phantom_mechanize.gemspec
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ - js
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.1.0
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: A Phantomjs addon for Ruby Mechanize
83
+ test_files: []