glebtv-screencap 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c009987882aa5b8c1b83331603aa852e84856b2f
4
+ data.tar.gz: 9436799c4d60a7af5d2e0c628264f43ffd4e5a89
5
+ SHA512:
6
+ metadata.gz: 470749f6504d31e4b1126aa1ba720cefbb195824e7b9d5967c22ff3ef3357f12ac610d8b6103b820404c943cead54beb03cfb3667eac89f3ca9988e7d613bbe1
7
+ data.tar.gz: a5ef19f475ebc823096946ce19f6927a3e745e2e86ed66e84896380b996f45d1ee6a06b763a739b1dc587a54d5bf4ec36c25c690ceac45916d9ea854b1f18436
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ screencap
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.3
3
+ script: "bundle exec rake"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in glebtv-screencap.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard 'bundler' do
2
+ watch('Gemfile')
3
+ watch(/^.+\.gemspec/)
4
+ end
5
+
6
+ guard 'rspec', :version => 2 do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
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,57 @@
1
+ # Screencap
2
+
3
+ This forks contains an adaptation for our needs
4
+
5
+ 100% height, fixed width, configurable delay after page is loaded
6
+
7
+ Some options were removed.
8
+
9
+ Please see original gem as it is much more generic than our usecase.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'glebtv-screencap'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install glebtv-screencap
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require 'screencap'
29
+
30
+ f = Screencap::Fetcher.new('http://google.com')
31
+ screenshot = f.fetch
32
+ ```
33
+
34
+ it also currently supports a couple of options
35
+
36
+ ```ruby
37
+ f = Screencap::Fetcher.new('http://google.com')
38
+ screenshot = f.fetch(
39
+ :output => '~/my_directory.png', #dont forget the extension!
40
+ # optional:
41
+ :height => 1024,
42
+ :width => 1024,
43
+ :delay => 15000,
44
+ )
45
+
46
+ ```
47
+
48
+ ## To-do
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
57
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/screencap/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["glebtv", "Maxwell Salzberg","David Spurr"]
6
+ gem.email = ["glebtv@gmail.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/glebtv/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 = "glebtv-screencap"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Screencap::VERSION
17
+
18
+ gem.add_development_dependency 'rspec', '~> 2.14'
19
+ gem.add_development_dependency 'rake'
20
+ gem.add_development_dependency 'fastimage'
21
+
22
+ gem.add_runtime_dependency 'phantomjs'
23
+ end
@@ -0,0 +1,2 @@
1
+ require 'screencap'
2
+
data/lib/screencap.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "screencap/version"
2
+
3
+ require 'pathname'
4
+
5
+ require 'phantomjs'
6
+
7
+ module Screencap
8
+ SCREENCAP_ROOT = Pathname.new(File.dirname(__FILE__))
9
+
10
+ class Error < StandardError; end
11
+ end
12
+
13
+ require 'screencap/fetcher'
14
+ require 'screencap/phantom'
@@ -0,0 +1,31 @@
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)
10
+ fetched_file
11
+ end
12
+
13
+ def filename
14
+ @filename
15
+ end
16
+
17
+ def fetched_file
18
+ if File.exists?(filename)
19
+ File.open(filename)
20
+ end
21
+ end
22
+
23
+ def raster(*args)
24
+ Screencap::Phantom.rasterize(*args)
25
+ end
26
+
27
+ def clean_filename
28
+ "#{@url.delete('/.:?!')}.png"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module Screencap
2
+ class Phantom
3
+ RASTERIZE = SCREENCAP_ROOT.join('screencap', 'raster.js')
4
+
5
+ def self.rasterize(url, path, args = {})
6
+ params = {
7
+ url: CGI::escape(url),
8
+ output: path
9
+ }.merge(args).collect {|k,v| "#{k}=#{v}"}
10
+ #puts RASTERIZE.to_s, params
11
+ result = Phantomjs.run(RASTERIZE.to_s, *params)
12
+ p result
13
+ raise Screencap::Error, "Could not load URL #{url}" if result.match /Unable to load/
14
+ end
15
+
16
+ def quoted_args(args)
17
+ args.map{|x| quoted_arg(x)}
18
+ end
19
+
20
+ def quoted_arg(arg)
21
+ return arg if arg.starts_with?("'") && arg.ends_with?("'")
22
+ arg = "'" + arg unless arg.starts_with?("'")
23
+ arg = arg + "'" unless arg.ends_with?("'")
24
+ arg
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ //
2
+ // Takes a screenshot of the given URL, uses named arguments passed in like so: phantomjs raster.js arg=value arg2=value2
3
+ //
4
+ // Arguments:
5
+ // - url - URL to screenshot
6
+ // - output - page to output (e.g. /tmp/output.png)
7
+ // - width [optional] - default 1400 - viewport width
8
+ // - height [optional] - viewport height (defaults to page height)
9
+ // - delay [optional] - default 2000 - delay in msec before saving screenshot
10
+
11
+ var page = new WebPage(), address, output, div, i, pair;
12
+
13
+ // use named arguments
14
+ var args = {};
15
+ for(i = 0; i < phantom.args.length; i++) {
16
+ pair = phantom.args[i].split(/=(.*)/);
17
+ args[pair[0]] = pair[1];
18
+ }
19
+
20
+ if( args.url ) {
21
+ args.url = decodeURIComponent(args.url);
22
+ }
23
+
24
+ // console.log(JSON.stringify(args));
25
+
26
+ if( !args.url || !args.output ) {
27
+ console.log('Usage: rasterize.js url=URL output=filename delay=delay');
28
+ phantom.exit();
29
+ }
30
+
31
+ page.onConsoleMessage = function (msg) {
32
+ console.log("from page: " + msg);
33
+ };
34
+
35
+ page.open(args.url, function (status) {
36
+ console.log(status, args);
37
+ if(status !== 'success') {
38
+ console.log('Unable to load:' + args.url);
39
+ phantom.exit();
40
+ }
41
+ var width = args.width || 1400;
42
+ var height = args.height || page.evaluate(function () {
43
+ return document.body.clientHeight;
44
+ });
45
+ var delay = args.delay || 2000;
46
+
47
+ page.viewportSize = {width: args.width, height: height};
48
+
49
+ // console.log("Waiting " + delay + "ms");
50
+
51
+ setTimeout(function() {
52
+ page.render(args.output);
53
+ phantom.exit();
54
+ }, delay)
55
+ });
56
+
@@ -0,0 +1,3 @@
1
+ module Screencap
2
+ VERSION = "0.0.8"
3
+ end
@@ -0,0 +1,27 @@
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 => TMP_DIRECTORY + 'kats.png')
10
+ File.exists?(screenshot).should == true
11
+ end
12
+
13
+ it 'supports a custom width' do
14
+ screenshot = Screencap::Fetcher.new('http://google.com').fetch(:output => TMP_DIRECTORY + 'foo.jpg', :width => 800)
15
+ FastImage.size(screenshot)[0].should == 800
16
+ end
17
+
18
+ #it 'captures a given element' do
19
+ #screenshot = Screencap::Fetcher.new('http://placehold.it').fetch(:output => TMP_DIRECTORY + 'bar.jpg', :div => 'img.image')
20
+ #FastImage.size(screenshot)[0].should == 140
21
+ #end
22
+
23
+ it 'should work when given a query string with ampersand in it' do
24
+ screenshot = Screencap::Fetcher.new('http://google.com?1=2&3=4').fetch(:output => TMP_DIRECTORY + 'foo.jpg', :width => 800)
25
+ FastImage.size(screenshot)[0].should == 800
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Screencap do
4
+ it 'works' do
5
+ screenshot = Screencap::Fetcher.new('http://google.com').fetch(output: TMP_DIRECTORY + 'google.png')
6
+ FastImage.size(screenshot)[0].should == 602
7
+ end
8
+
9
+ it 'throws error when phantom could not load page' do
10
+ expect {
11
+ Screencap::Fetcher.new('http://doesnotexistatallipromise.com/').fetch(output: TMP_DIRECTORY + 'foo.png')
12
+ }.to raise_error Screencap::Error, "Could not load URL http://doesnotexistatallipromise.com/"
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'screencap'
3
+ require 'fastimage'
4
+
5
+ TMP_DIRECTORY = Screencap::SCREENCAP_ROOT.join('..', 'tmp')
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ config.before(:all) do
13
+ # system("rm #{TMP_DIRECTORY}/*.png") leave them for now
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: glebtv-screencap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - glebtv
8
+ - Maxwell Salzberg
9
+ - David Spurr
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-08-22 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '2.14'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '2.14'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: fastimage
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: phantomjs
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ description: a gem to grab screenshots of webpages, or just parts of webpages
72
+ email:
73
+ - glebtv@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".ruby-gemset"
81
+ - ".ruby-version"
82
+ - ".travis.yml"
83
+ - Gemfile
84
+ - Guardfile
85
+ - LICENSE
86
+ - README.md
87
+ - Rakefile
88
+ - glebtv-screencap.gemspec
89
+ - lib/glebtv-screencap.rb
90
+ - lib/screencap.rb
91
+ - lib/screencap/fetcher.rb
92
+ - lib/screencap/phantom.rb
93
+ - lib/screencap/raster.js
94
+ - lib/screencap/version.rb
95
+ - spec/fetcher_spec.rb
96
+ - spec/screencap_spec.rb
97
+ - spec/spec_helper.rb
98
+ homepage: http://github.com/glebtv/screencap
99
+ licenses: []
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.2.2
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: uses Phantom.js to grab pages, or parts of pages. Simple API.
121
+ test_files:
122
+ - spec/fetcher_spec.rb
123
+ - spec/screencap_spec.rb
124
+ - spec/spec_helper.rb