grabzit 1.0.0
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.
- data/lib/grabzitclient.rb +266 -0
- data/lib/grabzitcookie.rb +53 -0
- data/lib/screenshotstatus.rb +32 -0
- metadata +48 -0
@@ -0,0 +1,266 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'net/http'
|
3
|
+
require 'rexml/document'
|
4
|
+
require 'uri'
|
5
|
+
require File.join(File.dirname(__FILE__), 'screenshotstatus')
|
6
|
+
require File.join(File.dirname(__FILE__), 'grabzitcookie')
|
7
|
+
|
8
|
+
class GrabzItClient
|
9
|
+
|
10
|
+
WebServicesBaseURL = "http://grabz.it/services/"
|
11
|
+
TrueString = "True"
|
12
|
+
|
13
|
+
|
14
|
+
def initialize(applicationKey, applicationSecret)
|
15
|
+
@applicationKey = applicationKey
|
16
|
+
@applicationSecret = applicationSecret
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
#
|
21
|
+
#This method calls the GrabzIt web service to take the screenshot.
|
22
|
+
#
|
23
|
+
#url - The URL that the screenshot should be made of
|
24
|
+
#callback - The handler the GrabzIt web service should call after it has completed its work
|
25
|
+
#browserWidth - The width of the browser in pixels
|
26
|
+
#browserHeight - The height of the browser in pixels
|
27
|
+
#outputHeight - The height of the resulting thumbnail in pixels
|
28
|
+
#outputWidth - The width of the resulting thumbnail in pixels
|
29
|
+
#customId - A custom identifier that you can pass through to the screenshot webservice. This will be returned with the callback URL you have specified.
|
30
|
+
#format - The format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
31
|
+
#delay - The number of milliseconds to wait before taking the screenshot
|
32
|
+
#
|
33
|
+
#This function returns the unique identifier of the screenshot. This can be used to get the screenshot with the GetPicture method.
|
34
|
+
#
|
35
|
+
def take_picture(url, callback = nil, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil)
|
36
|
+
qs = "key=" + URI.escape(@applicationKey) + "&url=" + URI.escape(url) + "&width=" + nil_check(width) + "&height=" + nil_check(height) + "&format=" + nil_check(format) + "&bwidth=" + nil_check(browserWidth) + "&bheight=" + nil_check(browserHeight) + "&callback=" + URI.escape(nil_check(callback)) + "&customid=" + URI.escape(nil_check(customId)) + "&delay=" + nil_check(delay)
|
37
|
+
sig = Digest::MD5.hexdigest(@applicationSecret+"|"+url+"|"+nil_check(callback)+"|"+nil_check(format)+"|"+nil_check(height)+"|"+nil_check(width)+"|"+nil_check(browserHeight)+"|"+nil_check(browserWidth)+"|"+nil_check(customId)+"|"+nil_check(delay))
|
38
|
+
qs = qs + "&sig=" + sig
|
39
|
+
|
40
|
+
result = get(WebServicesBaseURL + "takepicture.ashx?" + qs)
|
41
|
+
|
42
|
+
doc = REXML::Document.new(result)
|
43
|
+
|
44
|
+
message = doc.root.elements["Message"].text()
|
45
|
+
id = doc.root.elements["ID"].text()
|
46
|
+
|
47
|
+
if message != nil
|
48
|
+
raise message
|
49
|
+
end
|
50
|
+
|
51
|
+
return id
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
#This method takes the screenshot and then saves the result to a file. WARNING this method is synchronous.
|
56
|
+
#
|
57
|
+
#url - The URL that the screenshot should be made of
|
58
|
+
#saveToFile - The file path that the screenshot should saved to: e.g. images/test.jpg
|
59
|
+
#browserWidth - The width of the browser in pixels
|
60
|
+
#browserHeight - The height of the browser in pixels
|
61
|
+
#outputHeight - The height of the resulting thumbnail in pixels
|
62
|
+
#outputWidth - The width of the resulting thumbnail in pixels
|
63
|
+
#format - The format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
64
|
+
#delay - The number of milliseconds to wait before taking the screenshot
|
65
|
+
#
|
66
|
+
#This function returns the true if it is successfull otherwise it throws an exception.
|
67
|
+
#
|
68
|
+
def save_picture(url, saveToFile, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil)
|
69
|
+
id = take_picture(url, nil, nil, browserWidth, browserHeight, width, height, format, delay)
|
70
|
+
|
71
|
+
#Wait for it to be ready.
|
72
|
+
while true do
|
73
|
+
status = get_status(id)
|
74
|
+
|
75
|
+
if !status.cached && !status.processing
|
76
|
+
raise "The screenshot did not complete with the error: " + status.Message
|
77
|
+
break;
|
78
|
+
elsif status.cached
|
79
|
+
result = get_picture(id)
|
80
|
+
if !result
|
81
|
+
raise "The screenshot image could not be found on GrabzIt."
|
82
|
+
break
|
83
|
+
end
|
84
|
+
|
85
|
+
screenshot = File.new(saveToFile, "wb")
|
86
|
+
screenshot.write(result)
|
87
|
+
screenshot.close
|
88
|
+
|
89
|
+
break
|
90
|
+
end
|
91
|
+
|
92
|
+
sleep(1)
|
93
|
+
end
|
94
|
+
|
95
|
+
return true
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
#Get the current status of a GrabzIt screenshot
|
100
|
+
#
|
101
|
+
#id - The id of the screenshot
|
102
|
+
#
|
103
|
+
#This function returns a Status object representing the screenshot
|
104
|
+
#
|
105
|
+
def get_status(id)
|
106
|
+
result = get(WebServicesBaseURL + "getstatus.ashx?id=" + id)
|
107
|
+
|
108
|
+
doc = REXML::Document.new(result)
|
109
|
+
|
110
|
+
processing = doc.root.elements["Processing"].text()
|
111
|
+
cached = doc.root.elements["Cached"].text()
|
112
|
+
expired = doc.root.elements["Expired"].text()
|
113
|
+
message = doc.root.elements["Message"].text()
|
114
|
+
|
115
|
+
status = ScreenShotStatus.new()
|
116
|
+
status.processing = (processing == TrueString)
|
117
|
+
status.cached = (cached == TrueString)
|
118
|
+
status.expired = (expired == TrueString)
|
119
|
+
status.message = message
|
120
|
+
|
121
|
+
return status
|
122
|
+
end
|
123
|
+
|
124
|
+
#
|
125
|
+
#This method returns the image itself. If nothing is returned then something has gone wrong or the image is not ready yet.
|
126
|
+
#
|
127
|
+
#id - The unique identifier of the screenshot, returned by the callback handler or the TakePicture method
|
128
|
+
#
|
129
|
+
#This function returns the screenshot
|
130
|
+
#
|
131
|
+
def get_picture(id)
|
132
|
+
result = get(WebServicesBaseURL + "getpicture.ashx?id=" + id)
|
133
|
+
|
134
|
+
if result == nil
|
135
|
+
return nil
|
136
|
+
end
|
137
|
+
|
138
|
+
return result
|
139
|
+
end
|
140
|
+
|
141
|
+
#
|
142
|
+
#Get all the cookies that GrabzIt is using for a particular domain. This may include your user set cookies as well.
|
143
|
+
#
|
144
|
+
#domain - The domain to return cookies for.
|
145
|
+
#
|
146
|
+
#This function returns an array of cookies
|
147
|
+
#
|
148
|
+
def get_cookies(domain)
|
149
|
+
sig = Digest::MD5.hexdigest(@applicationSecret+"|"+domain)
|
150
|
+
|
151
|
+
qs = "key=" +URI.escape(@applicationKey)+"&domain="+URI.escape(domain)+"&sig="+sig
|
152
|
+
|
153
|
+
result = get(WebServicesBaseURL + "getcookies.ashx?" + qs)
|
154
|
+
|
155
|
+
doc = REXML::Document.new(result)
|
156
|
+
|
157
|
+
message = doc.root.elements["Message"].text()
|
158
|
+
|
159
|
+
if message != nil
|
160
|
+
raise message
|
161
|
+
end
|
162
|
+
|
163
|
+
cookies = Array.new
|
164
|
+
|
165
|
+
xml_cookies = doc.elements.to_a("//WebResult/Cookies/Cookie")
|
166
|
+
xml_cookies.each do |cookie|
|
167
|
+
grabzItCookie = GrabzItCookie.new()
|
168
|
+
grabzItCookie.name = cookie.elements["Name"].text
|
169
|
+
grabzItCookie.value = cookie.elements["Value"].text
|
170
|
+
grabzItCookie.domain = cookie.elements["Domain"].text
|
171
|
+
grabzItCookie.path = cookie.elements["Path"].text
|
172
|
+
grabzItCookie.httpOnly = (cookie.elements["HttpOnly"].text == TrueString)
|
173
|
+
if cookie.elements["Expires"] != nil
|
174
|
+
grabzItCookie.expires = cookie.elements["Expires"].text
|
175
|
+
end
|
176
|
+
grabzItCookie.type = cookie.elements["Type"].text
|
177
|
+
|
178
|
+
cookies << grabzItCookie
|
179
|
+
end
|
180
|
+
|
181
|
+
return cookies
|
182
|
+
end
|
183
|
+
|
184
|
+
#
|
185
|
+
#Sets a new custom cookie on GrabzIt, if the custom cookie has the same name and domain as a global cookie the global
|
186
|
+
#cookie is overridden.
|
187
|
+
#
|
188
|
+
#This can be useful if a websites functionality is controlled by cookies.
|
189
|
+
#
|
190
|
+
#name - The name of the cookie to set.
|
191
|
+
#domain - The domain of the website to set the cookie for.
|
192
|
+
#value - The value of the cookie.
|
193
|
+
#path - The website path the cookie relates to.
|
194
|
+
#httponly - Is the cookie only used on HTTP
|
195
|
+
#expires - When the cookie expires. Pass a null value if it does not expire.
|
196
|
+
#
|
197
|
+
#This function returns true if the cookie was successfully set.
|
198
|
+
#
|
199
|
+
def set_cookie(name, domain, value = "", path = "/", httponly = false, expires = "")
|
200
|
+
sig = Digest::MD5.hexdigest(@applicationSecret+"|"+name+"|"+domain+"|"+nil_check(value)+"|"+nil_check(path)+"|"+b_to_int(httponly).to_s+"|"+nil_check(expires)+"|0")
|
201
|
+
|
202
|
+
qs = "key=" +URI.escape(@applicationKey)+"&domain="+URI.escape(domain)+"&name="+URI.escape(name)+"&value="+URI.escape(nil_check(value))+"&path="+URI.escape(nil_check(path))+"&httponly="+b_to_int(httponly).to_s+"&expires="+nil_check(expires)+"&sig="+sig
|
203
|
+
|
204
|
+
result = get(WebServicesBaseURL + "setcookie.ashx?" + qs)
|
205
|
+
|
206
|
+
doc = REXML::Document.new(result)
|
207
|
+
|
208
|
+
message = doc.root.elements["Message"].text()
|
209
|
+
resultVal = doc.root.elements["Result"].text()
|
210
|
+
|
211
|
+
if message != nil
|
212
|
+
raise message
|
213
|
+
end
|
214
|
+
|
215
|
+
return (resultVal == TrueString)
|
216
|
+
end
|
217
|
+
|
218
|
+
#
|
219
|
+
#Delete a custom cookie or block a global cookie from being used.
|
220
|
+
#
|
221
|
+
#name - The name of the cookie to delete
|
222
|
+
#domain - The website the cookie belongs to
|
223
|
+
#
|
224
|
+
#This function returns true if the cookie was successfully set.
|
225
|
+
#
|
226
|
+
def delete_cookie(name, domain)
|
227
|
+
sig = Digest::MD5.hexdigest(@applicationSecret+"|"+name+"|"+domain+"|1")
|
228
|
+
|
229
|
+
qs = "key=" + URI.escape(@applicationKey)+"&domain="+URI.escape(domain)+"&name="+URI.escape(name)+"&delete=1&sig="+sig
|
230
|
+
|
231
|
+
result = get(WebServicesBaseURL + "setcookie.ashx?" + qs)
|
232
|
+
|
233
|
+
doc = REXML::Document.new(result)
|
234
|
+
|
235
|
+
message = doc.root.elements["Message"].text()
|
236
|
+
resultVal = doc.root.elements["Result"].text()
|
237
|
+
|
238
|
+
if message != nil
|
239
|
+
raise message
|
240
|
+
end
|
241
|
+
|
242
|
+
return (resultVal == TrueString)
|
243
|
+
end
|
244
|
+
|
245
|
+
|
246
|
+
private
|
247
|
+
def get(url)
|
248
|
+
Net::HTTP.get(URI.parse(url))
|
249
|
+
end
|
250
|
+
|
251
|
+
private
|
252
|
+
def b_to_int(bValue)
|
253
|
+
if bValue
|
254
|
+
return 1
|
255
|
+
end
|
256
|
+
return 1
|
257
|
+
end
|
258
|
+
|
259
|
+
private
|
260
|
+
def nil_check(param)
|
261
|
+
if param == nil
|
262
|
+
return ""
|
263
|
+
end
|
264
|
+
return param
|
265
|
+
end
|
266
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class GrabzItCookie
|
2
|
+
def initialize
|
3
|
+
@Name = ''
|
4
|
+
@Value = ''
|
5
|
+
@Domain = ''
|
6
|
+
@Path = ''
|
7
|
+
@HttpOnly = false
|
8
|
+
@Expires = nil
|
9
|
+
@Type = nil
|
10
|
+
end
|
11
|
+
def name
|
12
|
+
@Name
|
13
|
+
end
|
14
|
+
def name=(value)
|
15
|
+
@Name = value
|
16
|
+
end
|
17
|
+
def value
|
18
|
+
@Value
|
19
|
+
end
|
20
|
+
def value=(value)
|
21
|
+
@Value = value
|
22
|
+
end
|
23
|
+
def domain
|
24
|
+
@Domain
|
25
|
+
end
|
26
|
+
def domain=(value)
|
27
|
+
@Domain = value
|
28
|
+
end
|
29
|
+
def path
|
30
|
+
@Path
|
31
|
+
end
|
32
|
+
def path=(value)
|
33
|
+
@Path = value
|
34
|
+
end
|
35
|
+
def httpOnly
|
36
|
+
@HttpOnly
|
37
|
+
end
|
38
|
+
def httpOnly=(value)
|
39
|
+
@HttpOnly = value
|
40
|
+
end
|
41
|
+
def expires
|
42
|
+
@Expires
|
43
|
+
end
|
44
|
+
def expires=(value)
|
45
|
+
@Expires = value
|
46
|
+
end
|
47
|
+
def type
|
48
|
+
@Type
|
49
|
+
end
|
50
|
+
def type=(value)
|
51
|
+
@Type = value
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class ScreenShotStatus
|
2
|
+
def initialize
|
3
|
+
@Processing = false
|
4
|
+
@Cached = false
|
5
|
+
@Expired = false
|
6
|
+
@Message = ''
|
7
|
+
end
|
8
|
+
def processing
|
9
|
+
@Processing
|
10
|
+
end
|
11
|
+
def processing=(value)
|
12
|
+
@Processing = value
|
13
|
+
end
|
14
|
+
def cached
|
15
|
+
@Cached
|
16
|
+
end
|
17
|
+
def cached=(value)
|
18
|
+
@Cached = value
|
19
|
+
end
|
20
|
+
def expired
|
21
|
+
@Expired
|
22
|
+
end
|
23
|
+
def expired=(value)
|
24
|
+
@Expired = value
|
25
|
+
end
|
26
|
+
def message
|
27
|
+
@Message
|
28
|
+
end
|
29
|
+
def message=(value)
|
30
|
+
@Message = value
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grabzit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- GrabzIt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Automatically create and download website screenshots with GrabzIt for
|
15
|
+
free.
|
16
|
+
email: support@grabz.it
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/grabzitclient.rb
|
22
|
+
- lib/grabzitcookie.rb
|
23
|
+
- lib/screenshotstatus.rb
|
24
|
+
homepage: http://grabz.it/api/ruby
|
25
|
+
licenses: []
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 1.8.23
|
45
|
+
signing_key:
|
46
|
+
specification_version: 3
|
47
|
+
summary: GrabzIt Ruby Client
|
48
|
+
test_files: []
|