screencap 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +8 -0
- data/Guardfile +10 -0
- data/LICENSE +22 -0
- data/README.md +46 -0
- data/Rakefile +6 -0
- data/lib/screencap.rb +18 -0
- data/lib/screencap/fetcher.rb +29 -0
- data/lib/screencap/phantom.rb +22 -0
- data/lib/screencap/raster.js +52 -0
- data/lib/screencap/version.rb +3 -0
- data/screencap.gemspec +21 -0
- data/spec/fetcher_spec.rb +12 -0
- data/spec/screencap_spec.rb +8 -0
- data/spec/spec_helper.rb +12 -0
- metadata +99 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Maxwell Salzberg
|
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,46 @@
|
|
1
|
+
# Screencap
|
2
|
+
|
3
|
+
A screenshot gem you can use from your ruby application. Uses Phantom.js under the hood.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'screencap'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install screencap
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'screencap'
|
23
|
+
|
24
|
+
f = Screencap::Fetcher.new('http://google.com')
|
25
|
+
screenshot = f.fetch
|
26
|
+
```
|
27
|
+
|
28
|
+
it also currently supports a couple of options
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
f = Screencap::Fetcher.new('http://google.com')
|
32
|
+
screenshot = f.fetch(:div => '.header', :output => '~/my_directory.png') #dont forget the extension!
|
33
|
+
```
|
34
|
+
|
35
|
+
## To-do
|
36
|
+
|
37
|
+
more tests
|
38
|
+
better configuration
|
39
|
+
expose more options
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/screencap.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "screencap/version"
|
2
|
+
require 'phantomjs.rb'
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
module Screencap
|
7
|
+
SCREENCAP_ROOT = Pathname.new(File.dirname(__FILE__))
|
8
|
+
TMP_DIRECTORY = SCREENCAP_ROOT.join('..', 'tmp')
|
9
|
+
end
|
10
|
+
|
11
|
+
#config
|
12
|
+
|
13
|
+
#tmp directory to store files
|
14
|
+
|
15
|
+
#should return a file handle to tmp director where it is stored
|
16
|
+
|
17
|
+
require 'screencap/fetcher'
|
18
|
+
require 'screencap/phantom'
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Screencap
|
2
|
+
class Fetcher
|
3
|
+
def initialize(url)
|
4
|
+
@url = url
|
5
|
+
end
|
6
|
+
|
7
|
+
def fetch(opts = {})
|
8
|
+
@filename = opts.fetch(:output, clean_filename)
|
9
|
+
raster(@url, @filename, opts[:div])
|
10
|
+
fetched_file
|
11
|
+
end
|
12
|
+
|
13
|
+
def filename
|
14
|
+
@filename
|
15
|
+
end
|
16
|
+
|
17
|
+
def fetched_file
|
18
|
+
File.open(filename)
|
19
|
+
end
|
20
|
+
|
21
|
+
def raster(*args)
|
22
|
+
Screencap::Phantom.rasterize(*args)
|
23
|
+
end
|
24
|
+
|
25
|
+
def clean_filename
|
26
|
+
"#{TMP_DIRECTORY}/#{@url.delete('/.:?!')}.png"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Screencap
|
2
|
+
class Phantom
|
3
|
+
RASTERIZE = SCREENCAP_ROOT.join('screencap', 'raster.js')
|
4
|
+
|
5
|
+
def self.rasterize(url, path, *args)
|
6
|
+
# puts RASTERIZE.to_s, url, path, *args # Your code goes here...
|
7
|
+
Phantomjs.run(RASTERIZE.to_s, url, path, *args)
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
def quoted_args(args)
|
12
|
+
args.map{|x| quoted_arg(x)}
|
13
|
+
end
|
14
|
+
|
15
|
+
def quoted_arg(arg)
|
16
|
+
return arg if arg.starts_with?("'") && arg.ends_with?("'")
|
17
|
+
arg = "'" + arg unless arg.starts_with?("'")
|
18
|
+
arg = arg + "'" unless arg.ends_with?("'")
|
19
|
+
arg
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
var page = new WebPage(),
|
2
|
+
address, output, div;
|
3
|
+
|
4
|
+
if (phantom.args.length < 2 || phantom.args.length > 4) {
|
5
|
+
console.log('Usage: rasterize.js URL filename div(optional)');
|
6
|
+
phantom.exit();
|
7
|
+
}
|
8
|
+
|
9
|
+
address = phantom.args[0];
|
10
|
+
output = phantom.args[1];
|
11
|
+
div = phantom.args[2];
|
12
|
+
|
13
|
+
//console.log(address, output, div)
|
14
|
+
|
15
|
+
function evaluate(page, func) {
|
16
|
+
var args = [].slice.call(arguments, 2);
|
17
|
+
var fn = "function() { return (" + func.toString() + ").apply(this, " + JSON.stringify(args) + ");}";
|
18
|
+
return page.evaluate(fn);
|
19
|
+
}
|
20
|
+
|
21
|
+
function returnDivDimensions(div){
|
22
|
+
var $el = jQuery(div);
|
23
|
+
var box = $el.offset()
|
24
|
+
box.height = $el.height();
|
25
|
+
box.width = $el.width();
|
26
|
+
return box;
|
27
|
+
}
|
28
|
+
|
29
|
+
page.onConsoleMessage = function (msg) {
|
30
|
+
//console.log("from page:" + msg);
|
31
|
+
};
|
32
|
+
|
33
|
+
page.viewportSize = { width: 1000, height: 550 }
|
34
|
+
|
35
|
+
page.open(address, function (status) {
|
36
|
+
if (status !== 'success') {
|
37
|
+
console.log('Unable to load:' + address);
|
38
|
+
phantom.exit();
|
39
|
+
}
|
40
|
+
//once page loaded, include jQuery from cdn
|
41
|
+
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
|
42
|
+
page.evaluate(function(){jQuery.noConflict()})
|
43
|
+
|
44
|
+
if(div) {
|
45
|
+
var clip = evaluate(page, returnDivDimensions, div);
|
46
|
+
page.clipRect = clip;
|
47
|
+
}
|
48
|
+
|
49
|
+
page.render(output);
|
50
|
+
phantom.exit();
|
51
|
+
});
|
52
|
+
});
|
data/screencap.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/screencap/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Maxwell Salzberg"]
|
6
|
+
gem.email = ["maxwell@joindiaspora.com"]
|
7
|
+
gem.description = %q{a gem to grab screenshots of webpages, or just parts of webpages}
|
8
|
+
gem.summary = %q{uses Phantom.js to grab pages, or parts of pages. Simple API.}
|
9
|
+
gem.homepage = "http://github.com/maxwell/screencap"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "screencap"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Screencap::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'phantomjs.rb'
|
19
|
+
gem.add_development_dependency 'rspec', '~> 2.10'
|
20
|
+
gem.add_development_dependency 'rake'
|
21
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Screencap::Fetcher do
|
4
|
+
it 'takes a url' do
|
5
|
+
Screencap::Fetcher.new('http://google.com').should_not be_nil
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'supports a custom filename' do
|
9
|
+
screenshot = Screencap::Fetcher.new('http://yahoo.com').fetch(:output => Screencap::TMP_DIRECTORY + 'kats.png')
|
10
|
+
File.exists?(screenshot).should == true
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
2
|
+
require 'screencap'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
config.filter_run :focus
|
8
|
+
|
9
|
+
config.before(:all) do
|
10
|
+
system("rm #{Screencap::TMP_DIRECTORY}/*.png")
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: screencap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maxwell Salzberg
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: phantomjs.rb
|
16
|
+
requirement: &70163076869600 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70163076869600
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70163076869000 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.10'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70163076869000
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70163076868540 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70163076868540
|
47
|
+
description: a gem to grab screenshots of webpages, or just parts of webpages
|
48
|
+
email:
|
49
|
+
- maxwell@joindiaspora.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rspec
|
56
|
+
- .travis.yml
|
57
|
+
- Gemfile
|
58
|
+
- Guardfile
|
59
|
+
- LICENSE
|
60
|
+
- README.md
|
61
|
+
- Rakefile
|
62
|
+
- lib/screencap.rb
|
63
|
+
- lib/screencap/fetcher.rb
|
64
|
+
- lib/screencap/phantom.rb
|
65
|
+
- lib/screencap/raster.js
|
66
|
+
- lib/screencap/version.rb
|
67
|
+
- screencap.gemspec
|
68
|
+
- spec/fetcher_spec.rb
|
69
|
+
- spec/screencap_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
homepage: http://github.com/maxwell/screencap
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.17
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: uses Phantom.js to grab pages, or parts of pages. Simple API.
|
95
|
+
test_files:
|
96
|
+
- spec/fetcher_spec.rb
|
97
|
+
- spec/screencap_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
has_rdoc:
|