thumbalizr 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +9 -0
- data/Rakefile +18 -0
- data/lib/thumbalizr.rb +141 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 061c7b5cea1fb9702f9dd4bece06025aa92ab5515cb23cbef6733b414b5e89bd
|
4
|
+
data.tar.gz: 4f44936ae4da2f261c86549d55c1f61315d8ccd32f10177465c594b2733d8160
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 403fac924cfe6a6c70ff61db427babd956263ac39d0e4637a7b567b81c0f8c848f7ea10249cc2ecee02154f5957df1d920504dac4ab140a8bdbe6a931c562eb2
|
7
|
+
data.tar.gz: 8f5f3d62e0023a2562c3dfe391403be00f79c556257cef2d2a2e575ba0ba6ec59eda87ebba528904d38512e46dabf5ff6ee5453b214eb3f90562f36eb63638ba
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
Rake::TestTask.new do |t|
|
4
|
+
t.libs << 'test'
|
5
|
+
end
|
6
|
+
|
7
|
+
desc "Run tests"
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
require 'rdoc/task'
|
11
|
+
Rake::RDocTask.new do |rdoc|
|
12
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
13
|
+
|
14
|
+
rdoc.rdoc_dir = 'rdoc'
|
15
|
+
rdoc.title = "browshot #{version}"
|
16
|
+
rdoc.rdoc_files.include('README*')
|
17
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
18
|
+
end
|
data/lib/thumbalizr.rb
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# The library requires an API key and secret from Thumbalizr.
|
2
|
+
# Sign up for a free account a http://thumbalizr.com/
|
3
|
+
#
|
4
|
+
# See README for more information abouth Thumbalizr
|
5
|
+
# and this library.
|
6
|
+
#
|
7
|
+
# @author Julien Sobrier (mailto:jsobrier@browshot.com)
|
8
|
+
# Copyright: Copyright (c) 2019 Browshot
|
9
|
+
# License: Distributes under the same terms as Ruby
|
10
|
+
|
11
|
+
require 'rubygems'
|
12
|
+
require 'url'
|
13
|
+
require 'net/http'
|
14
|
+
require 'net/https'
|
15
|
+
require "cgi"
|
16
|
+
|
17
|
+
class Thumbalizr
|
18
|
+
# @!attribute [r]
|
19
|
+
# Embed API key
|
20
|
+
attr_reader :key
|
21
|
+
# @!attribute [r]
|
22
|
+
# Thumbalizr secret
|
23
|
+
attr_reader :secret
|
24
|
+
# @!attribute [r]
|
25
|
+
# Base URL for all API requests. You should use the default base provided by the library. Be careful if you decide to use HTTP instead of HTTPS as your API key could be sniffed and your account could be used without your consent.
|
26
|
+
attr_reader :base
|
27
|
+
# @!attribute [r]
|
28
|
+
# print debug output to the standard output
|
29
|
+
attr_reader :debug
|
30
|
+
|
31
|
+
# New client
|
32
|
+
#
|
33
|
+
# @param key [String] Embed API key
|
34
|
+
# @param secret [String] Thumbalizr secret
|
35
|
+
# @param debug [Boolean] Set to true to print debug output to the standard output. false (disabled) by default.
|
36
|
+
# @param base [String] Base URL for all API requests. You should use the default base provided by the library.
|
37
|
+
def initialize(key='', secret='', debug=false, base='https://api.thumbalizr.com/api/v1/embed/')
|
38
|
+
@key = key || ''
|
39
|
+
@secret = secret || ''
|
40
|
+
@base = base || 'https://api.thumbalizr.com/api/v1/embed/'
|
41
|
+
@debug = debug || false
|
42
|
+
end
|
43
|
+
|
44
|
+
# Return the Thumbalizr URL for the screenshot requested
|
45
|
+
#
|
46
|
+
# @param url [String] URL of the web page
|
47
|
+
# @param parameters [Array<Symbol, Symbol>] Additional options
|
48
|
+
# See http://tumbalizr.com/api/documentation for the full list of possible options
|
49
|
+
# @return url [String] Thumbalizr URL fo the thumbnail
|
50
|
+
def url(url='', parameters={})
|
51
|
+
query = 'url=' + CGI::escape(url.to_s)
|
52
|
+
|
53
|
+
parameters.each_pair do |key, value|
|
54
|
+
query += "&#{key}=" + CGI::escape(value.to_s)
|
55
|
+
end
|
56
|
+
|
57
|
+
token = Digest::MD5.hexdigest(query + @secret)
|
58
|
+
|
59
|
+
return "#{@base}#{@key}/#{token}/?#{query}"
|
60
|
+
end
|
61
|
+
|
62
|
+
# Download a Thumbalizr thumbnail
|
63
|
+
#
|
64
|
+
# @param url [String] Thumbalizr URL generated by url()
|
65
|
+
# @param url [String] Option local file name to save the image file to.
|
66
|
+
# @return [Array<Symbol, Symbol>] !{:result => <result>, :image => <content>} if no file is specified, or {:result => <result>, :file => <file namet>} if file is specified
|
67
|
+
# result is either OK, FAILED or QUEUED. If the screenshot failed or is not finished, the image content or file name will be empty
|
68
|
+
def download(url='', file='')
|
69
|
+
begin
|
70
|
+
response = fetch(url.to_s)
|
71
|
+
case response
|
72
|
+
when Net::HTTPSuccess then
|
73
|
+
status = response.header['X-Thumbalizr-Status']
|
74
|
+
data = ''
|
75
|
+
if (status == 'OK')
|
76
|
+
if (file == '')
|
77
|
+
data = response.response.body
|
78
|
+
else
|
79
|
+
File.open(file, 'w') {|f| f.write(response.response.body) }
|
80
|
+
data = file
|
81
|
+
end
|
82
|
+
elsif (status == 'FAILED')
|
83
|
+
puts "Error: {response.header['X-Thumbalizr-Error']}" if (@debug)
|
84
|
+
end
|
85
|
+
|
86
|
+
return {:result => status, :image => data}
|
87
|
+
else
|
88
|
+
puts "Error: #{response.header['X-Thumbalizr-Error']}" if (@debug)
|
89
|
+
return {:result => response.header['X-Thumbalizr-Status'], :image => ''}
|
90
|
+
end
|
91
|
+
rescue Exception => e
|
92
|
+
puts "{e.message}" if (@debug)
|
93
|
+
raise e
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Download a Thumbalizr thumbnail. Unlike the download() function, this function waits until the screenshto is etiehr finished or failed.
|
98
|
+
#
|
99
|
+
# @param url [String] Thumbalizr URL generated by url()
|
100
|
+
# @param url [String] Option local file name to save the image file to.
|
101
|
+
# @return [Array<Symbol, Symbol>] !{:result => <result>, :image => <content>} if no file is specified, or {:result => <result>, :file => <file namet>} if file is specified
|
102
|
+
# result is either OK, FAILED or QUEUED. If the screenshot failed or is not finished, the image content or file name will be empty
|
103
|
+
def download_wait(url='', file='', wait=10)
|
104
|
+
result = download(url, file)
|
105
|
+
while (result[:result] == 'QUEUED')
|
106
|
+
puts "Waiting #{wait}s" if (@debug)
|
107
|
+
sleep(wait)
|
108
|
+
result = download(url, file)
|
109
|
+
end
|
110
|
+
|
111
|
+
return result
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
def fetch(url, limit=3)
|
116
|
+
raise ArgumentError, 'HTTP redirect too deep' if (limit == 0)
|
117
|
+
|
118
|
+
uri = URI.parse(url)
|
119
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
120
|
+
http.open_timeout = 240
|
121
|
+
http.read_timeout = 240
|
122
|
+
|
123
|
+
request = Net::HTTP::Get.new(uri.request_uri, {'User-Agent' => 'Thumbalizr Ruby 1.0'})
|
124
|
+
if (uri.scheme == 'https')
|
125
|
+
http.use_ssl = true
|
126
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
127
|
+
end
|
128
|
+
|
129
|
+
response = http.request(request)
|
130
|
+
|
131
|
+
case response
|
132
|
+
when Net::HTTPRedirection then
|
133
|
+
path = response['location']
|
134
|
+
url = URL.new( URI.join(@base, path).to_s )
|
135
|
+
return fetch(url.to_s, limit - 1)
|
136
|
+
else
|
137
|
+
return response
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thumbalizr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julien Sobrier
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: url
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Generate screenshot of any page.
|
28
|
+
email: jsobrier@thumbalizr.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- lib/thumbalizr.rb
|
36
|
+
homepage: https://thumbalizr.com/
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.0.6
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Client for https://thumbalizr.com/
|
59
|
+
test_files: []
|