gastly 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +69 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/gastly.gemspec +27 -0
- data/gastly.png +0 -0
- data/lib/gastly.rb +16 -0
- data/lib/gastly/image.rb +28 -0
- data/lib/gastly/screenshot.rb +60 -0
- data/lib/gastly/script.js +62 -0
- data/lib/gastly/version.rb +3 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b909060aee09401279b4c0745463834c652c296c
|
4
|
+
data.tar.gz: c604a639311a7ce2058705a02e67af139beb8f60
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1bfa49c1fb26bf1c62b4802f0be811b2b3e777bbe4348b70c0b8d820116d2b6cc8aa3a7696ffaf8328dc5d6f5a4e2056889d1d526ba4d21f4d130fb97b9bf42a
|
7
|
+
data.tar.gz: bc13634e04a8c794da3d4bae62b3ab2de28fbb7b8bd3769a5c7ac8cf281fa7622c43ac159e7775d1cdc8843f2d4f1e3c26797c83f587e60c84edcc27e02b3358
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Gastly
|
2
|
+
|
3
|
+
Create screenshots or previews of web pages using Gastly. Under the hood Phantom.js and MiniMagick. Gastly, I choose you!
|
4
|
+
|
5
|
+
![Gastly](https://github.com/mgrachev/gastly/raw/master/gastly.png)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'gastly'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install gastly
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Gastly.capture('http://google.com', 'output.png')
|
27
|
+
```
|
28
|
+
|
29
|
+
It is also possible to further customize the creation of screenshots and further processing of the resulting image:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
screenshot = Gastly.screenshot('http://google.com')
|
33
|
+
screenshot.selector = '#hplogo' # By default, the full screen is captured
|
34
|
+
screenshot.browser_width = 1280 # Default: 1440px
|
35
|
+
screenshot.browser_height = 780 # Default: 900px
|
36
|
+
screenshot.timeout = 1000 # Default: 0 seconds
|
37
|
+
screenshot.cookies = { user_id: 1, auth_token: 'abcd' } # If you need
|
38
|
+
image = screenshot.capture
|
39
|
+
```
|
40
|
+
|
41
|
+
Or
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
screenshot = Gastly.screenshot('http://google.com', selector: '#hplogo', timeout: 1000)
|
45
|
+
image = screenshot.capture
|
46
|
+
```
|
47
|
+
|
48
|
+
You can resize or change the format of the screenshot:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
image = screenshot.capture
|
52
|
+
image.resize(width: 110, height: 110) # Creates a previews of web-page
|
53
|
+
image.format('png')
|
54
|
+
image.save('output.png')
|
55
|
+
```
|
56
|
+
|
57
|
+
## Development
|
58
|
+
|
59
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
60
|
+
|
61
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it ( https://github.com/[my-github-username]/gastly/fork )
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gastly"
|
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
|
data/bin/setup
ADDED
data/gastly.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require_relative 'lib/gastly/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'gastly'
|
8
|
+
spec.version = Gastly::VERSION
|
9
|
+
spec.authors = ['Mikhail Grachev']
|
10
|
+
spec.email = ['work@mgrachev.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Create screenshots or previews of web pages using Gastly. Gastly, I choose you!}
|
13
|
+
spec.description = %q{Create screenshots or previews of web pages using Gastly. Under the hood Phantom.js and MiniMagick.}
|
14
|
+
spec.homepage = 'https://github.com/mgrachev/gastly'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.9'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
|
24
|
+
spec.add_dependency 'phantomjs', '~> 1.9'
|
25
|
+
spec.add_dependency 'mini_magick', '~> 4.2'
|
26
|
+
spec.add_dependency 'activesupport', '>= 3.1'
|
27
|
+
end
|
data/gastly.png
ADDED
Binary file
|
data/lib/gastly.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative 'gastly/image'
|
2
|
+
require_relative 'gastly/screenshot'
|
3
|
+
require_relative 'gastly/version'
|
4
|
+
|
5
|
+
module Gastly
|
6
|
+
|
7
|
+
def screenshot(url, **kwargs)
|
8
|
+
Screenshot.new(url, **kwargs)
|
9
|
+
end
|
10
|
+
|
11
|
+
def capture(url, path)
|
12
|
+
screenshot(url).capture.save(path)
|
13
|
+
end
|
14
|
+
|
15
|
+
module_function :screenshot, :capture
|
16
|
+
end
|
data/lib/gastly/image.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'mini_magick'
|
2
|
+
|
3
|
+
module Gastly
|
4
|
+
class Image
|
5
|
+
|
6
|
+
attr_reader :file
|
7
|
+
|
8
|
+
def initialize(tempfile)
|
9
|
+
@file = MiniMagick::Image.open(tempfile.path, 'png')
|
10
|
+
tempfile.unlink
|
11
|
+
end
|
12
|
+
|
13
|
+
def resize(width:, height:)
|
14
|
+
dimensions = "#{width}x#{height}"
|
15
|
+
@file.resize(dimensions)
|
16
|
+
end
|
17
|
+
|
18
|
+
def format(ext)
|
19
|
+
@file.format(ext)
|
20
|
+
end
|
21
|
+
|
22
|
+
def save(output)
|
23
|
+
@file.write(output)
|
24
|
+
output
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'phantomjs'
|
2
|
+
require 'active_support/core_ext/hash/keys'
|
3
|
+
require 'active_support/core_ext/object/blank'
|
4
|
+
|
5
|
+
module Gastly
|
6
|
+
class Screenshot
|
7
|
+
|
8
|
+
SCRIPT_PATH = File.expand_path('../script.js', __FILE__)
|
9
|
+
DEFAULT_TIMEOUT = 0
|
10
|
+
DEFAULT_BROWSER_WIDTH = 1440
|
11
|
+
DEFAULT_BROWSER_HEIGHT = 900
|
12
|
+
DEFAULT_FILE_NAME = 'output'.freeze
|
13
|
+
DEFAULT_FILE_FORMAT = '.png'.freeze
|
14
|
+
|
15
|
+
attr_writer :timeout, :browser_width, :browser_height
|
16
|
+
attr_accessor :url, :selector, :cookies
|
17
|
+
|
18
|
+
def initialize(url, **kwargs)
|
19
|
+
kwargs.assert_valid_keys(:timeout, :browser_width, :browser_height, :selector, :cookies)
|
20
|
+
|
21
|
+
@url = url
|
22
|
+
self.cookies = kwargs.delete(:cookies)
|
23
|
+
|
24
|
+
kwargs.each { |key, value| instance_variable_set(:"@#{key}", value) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def timeout
|
28
|
+
@timeout || DEFAULT_TIMEOUT
|
29
|
+
end
|
30
|
+
|
31
|
+
def browser_width
|
32
|
+
@browser_width || DEFAULT_BROWSER_WIDTH
|
33
|
+
end
|
34
|
+
|
35
|
+
def browser_height
|
36
|
+
@browser_height || DEFAULT_BROWSER_HEIGHT
|
37
|
+
end
|
38
|
+
|
39
|
+
def capture
|
40
|
+
tempfile = Tempfile.new([DEFAULT_FILE_NAME, DEFAULT_FILE_FORMAT])
|
41
|
+
|
42
|
+
params = {
|
43
|
+
url: url,
|
44
|
+
timeout: timeout,
|
45
|
+
width: browser_width,
|
46
|
+
height: browser_height,
|
47
|
+
output: tempfile.path
|
48
|
+
}
|
49
|
+
|
50
|
+
params[:selector] = selector if selector.present?
|
51
|
+
params[:cookies] = cookies.map { |k, v| "#{k}=#{v}" }.join(',') if cookies.present?
|
52
|
+
prepared_params = params.map { |k, v| "#{k}=#{v}" }
|
53
|
+
|
54
|
+
Phantomjs.run(SCRIPT_PATH.to_s, *prepared_params)
|
55
|
+
|
56
|
+
Gastly::Image.new(tempfile)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
function extractDomain(url){
|
2
|
+
var domain;
|
3
|
+
// Remove protocol
|
4
|
+
if (url.indexOf("://") > -1){
|
5
|
+
domain = url.split('/')[2];
|
6
|
+
}
|
7
|
+
else {
|
8
|
+
domain = url.split('/')[0];
|
9
|
+
}
|
10
|
+
|
11
|
+
// Remove port number
|
12
|
+
domain = domain.split(':')[0];
|
13
|
+
|
14
|
+
return domain;
|
15
|
+
}
|
16
|
+
|
17
|
+
var i, pair,
|
18
|
+
args = {},
|
19
|
+
system = require('system'),
|
20
|
+
page = require('webpage').create();
|
21
|
+
|
22
|
+
for(i = 1; i < system.args.length; i++){
|
23
|
+
pair = system.args[i].split(/=(.*)/);
|
24
|
+
args[pair[0]] = pair[1];
|
25
|
+
}
|
26
|
+
|
27
|
+
if (args.cookies !== undefined){
|
28
|
+
var cookiesPair = args.cookies.split(',');
|
29
|
+
|
30
|
+
for(i = 0; i < cookiesPair.length; i++){
|
31
|
+
pair = cookiesPair[i].split(/=(.*)/);
|
32
|
+
|
33
|
+
page.addCookie({
|
34
|
+
'name': pair[0],
|
35
|
+
'value': pair[1],
|
36
|
+
'path': '/',
|
37
|
+
'domain': extractDomain(args.url)
|
38
|
+
});
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
page.open(args.url, function(){
|
43
|
+
window.setTimeout(function(){
|
44
|
+
page.viewportSize = { width: args.width, height: args.height };
|
45
|
+
|
46
|
+
if (args.selector !== undefined){
|
47
|
+
var clipRect = page.evaluate(function(s){
|
48
|
+
return document.querySelector(s).getBoundingClientRect();
|
49
|
+
}, args.selector);
|
50
|
+
|
51
|
+
page.clipRect = {
|
52
|
+
top: clipRect.top,
|
53
|
+
left: clipRect.left,
|
54
|
+
width: clipRect.width,
|
55
|
+
height: clipRect.height
|
56
|
+
};
|
57
|
+
}
|
58
|
+
|
59
|
+
page.render(args.output);
|
60
|
+
phantom.exit();
|
61
|
+
}, args.timeout);
|
62
|
+
});
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gastly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mikhail Grachev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-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.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: phantomjs
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.9'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mini_magick
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.2'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.1'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.1'
|
83
|
+
description: Create screenshots or previews of web pages using Gastly. Under the hood
|
84
|
+
Phantom.js and MiniMagick.
|
85
|
+
email:
|
86
|
+
- work@mgrachev.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- gastly.gemspec
|
99
|
+
- gastly.png
|
100
|
+
- lib/gastly.rb
|
101
|
+
- lib/gastly/image.rb
|
102
|
+
- lib/gastly/screenshot.rb
|
103
|
+
- lib/gastly/script.js
|
104
|
+
- lib/gastly/version.rb
|
105
|
+
homepage: https://github.com/mgrachev/gastly
|
106
|
+
licenses: []
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.7
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Create screenshots or previews of web pages using Gastly. Gastly, I choose
|
128
|
+
you!
|
129
|
+
test_files: []
|