grabzit 1.1.1 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +8 -0
- data/lib/grabzit.rb +3 -0
- data/lib/grabzit/client.rb +443 -0
- data/lib/grabzit/cookie.rb +45 -0
- data/lib/grabzit/screenshotstatus.rb +30 -0
- data/lib/grabzit/watermark.rb +30 -0
- data/test/test.png +0 -0
- data/test/test_grabzit.rb +185 -0
- metadata +32 -9
- data/lib/grabzitclient.rb +0 -268
- data/lib/grabzitcookie.rb +0 -53
- data/lib/screenshotstatus.rb +0 -32
data/Rakefile
ADDED
data/lib/grabzit.rb
ADDED
@@ -0,0 +1,443 @@
|
|
1
|
+
# The public REST API for http://grabz.it
|
2
|
+
# @example To include the GrabzIt module do the following
|
3
|
+
# require 'grabzit'
|
4
|
+
module GrabzIt
|
5
|
+
require 'digest/md5'
|
6
|
+
require 'net/http'
|
7
|
+
require 'rexml/document'
|
8
|
+
require 'cgi'
|
9
|
+
require 'uri'
|
10
|
+
require File.join(File.dirname(__FILE__), 'screenshotstatus')
|
11
|
+
require File.join(File.dirname(__FILE__), 'cookie')
|
12
|
+
require File.join(File.dirname(__FILE__), 'watermark')
|
13
|
+
|
14
|
+
# This client provides access to the GrabzIt web services
|
15
|
+
# This API allows you to take screenshot of websites for free and convert them into images, PDF's and tables.
|
16
|
+
# @example Example usage
|
17
|
+
# require 'grabzit'
|
18
|
+
#
|
19
|
+
# grabzItClient = GrabzIt::Client.new("YOUR APPLICATION KEY", "YOUR APPLICATION SECRET")
|
20
|
+
# grabzItClient.set_image_options("http://www.google.com")
|
21
|
+
# grabzItClient.save("http://www.mysite.com/grabzit/handler")
|
22
|
+
# @version 2.0
|
23
|
+
# @author GrabzIt
|
24
|
+
# @see http://grabz.it/api/ruby/ GrabzIt Ruby API
|
25
|
+
class Client
|
26
|
+
|
27
|
+
WebServicesBaseURL = "http://grabz.it/services/"
|
28
|
+
private_constant :WebServicesBaseURL
|
29
|
+
TrueString = "True"
|
30
|
+
private_constant :TrueString
|
31
|
+
|
32
|
+
@@signaturePartOne;
|
33
|
+
@@signaturePartTwo;
|
34
|
+
@@request;
|
35
|
+
|
36
|
+
# Create a new instance of the Client class in order to access the GrabzIt API.
|
37
|
+
# @param applicationKey [String] your application key
|
38
|
+
# @param applicationSecret [String] your application secret
|
39
|
+
# @see http://grabz.it/register.aspx You can get an application key and secret by registering for free with GrabzIt
|
40
|
+
def initialize(applicationKey, applicationSecret)
|
41
|
+
@applicationKey = applicationKey
|
42
|
+
@applicationSecret = applicationSecret
|
43
|
+
end
|
44
|
+
|
45
|
+
# Sets the parameters required to take a screenshot of a web page.
|
46
|
+
# @param url [String] the URL that the screenshot should be made of
|
47
|
+
# @param customId [String, nil] custom identifier that you can pass through to the screenshot webservice. This will be returned with the callback URL you have specified.
|
48
|
+
# @param browserWidth [Integer, nil] the width of the browser in pixels
|
49
|
+
# @param browserHeight [Integer, nil] the height of the browser in pixels
|
50
|
+
# @param width [Integer, nil] the width of the resulting thumbnail in pixels
|
51
|
+
# @param height [Integer, nil] the height of the resulting thumbnail in pixels
|
52
|
+
# @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
53
|
+
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
54
|
+
# @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
|
55
|
+
# @param requestMobileVersion [Boolean, false] request a mobile version of the target website if possible
|
56
|
+
# @param customWaterMarkId [String, nil] add a custom watermark to the image
|
57
|
+
# @return [void]
|
58
|
+
def set_image_options(url, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil, requestMobileVersion = false, customWaterMarkId = nil)
|
59
|
+
@@request = WebServicesBaseURL + "takepicture.ashx?key="+CGI.escape(nil_check(@applicationKey))+"&url="+CGI.escape(nil_check(url))+"&width="+nil_int_check(width)+"&height="+nil_int_check(height)+"&format="+CGI.escape(nil_check(format))+"&bwidth="+nil_int_check(browserWidth)+"&bheight="+nil_int_check(browserHeight)+"&customid="+CGI.escape(nil_check(customId))+"&delay="+nil_int_check(delay)+"&target="+CGI.escape(nil_check(targetElement))+"&customwatermarkid="+CGI.escape(nil_check(customWaterMarkId))+"&requestmobileversion="+b_to_str(requestMobileVersion)+"&callback="
|
60
|
+
@@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
|
61
|
+
@@signaturePartTwo = "|"+nil_check(format)+"|"+nil_int_check(height)+"|"+nil_int_check(width)+"|"+nil_int_check(browserHeight)+"|"+nil_int_check(browserWidth)+"|"+nil_check(customId)+"|"+nil_int_check(delay)+"|"+nil_check(targetElement)+"|"+nil_check(customWaterMarkId)+"|"+b_to_str(requestMobileVersion)
|
62
|
+
|
63
|
+
return nil
|
64
|
+
end
|
65
|
+
|
66
|
+
# Sets the parameters required to extract one or more tables from a web page.
|
67
|
+
# @param url [String] the URL that the should be used to extract tables
|
68
|
+
# @param customId [String, nil] a custom identifier that you can pass through to the webservice. This will be returned with the callback URL you have specified.
|
69
|
+
# @param tableNumberToInclude [Integer, 1] the index of the table to be converted, were all tables in a web page are ordered from the top of the web page to bottom
|
70
|
+
# @param format [String, 'csv'] the format the table should be in: csv, xlsx
|
71
|
+
# @param includeHeaderNames [Boolean, true] if true header names will be included in the table
|
72
|
+
# @param includeAllTables [Boolean, true] if true all table on the web page will be extracted with each table appearing in a seperate spreadsheet sheet. Only available with the XLSX format.
|
73
|
+
# @param targetElement [String, nil] the id of the only HTML element in the web page that should be used to extract tables from
|
74
|
+
# @param requestMobileVersion [Boolean, false] request a mobile version of the target website if possible
|
75
|
+
# @return [void]
|
76
|
+
def set_table_options(url, customId = nil, tableNumberToInclude = 1, format = 'csv', includeHeaderNames = true, includeAllTables = false, targetElement = nil, requestMobileVersion = false)
|
77
|
+
@@request = WebServicesBaseURL + "taketable.ashx?key=" + CGI.escape(nil_check(@applicationKey))+"&url="+CGI.escape(url)+"&includeAllTables="+b_to_str(includeAllTables)+"&includeHeaderNames="+b_to_str(includeHeaderNames)+"&format="+CGI.escape(nil_check(format))+"&tableToInclude="+nil_int_check(tableNumberToInclude)+"&customid="+CGI.escape(nil_check(customId))+"&target="+CGI.escape(nil_check(targetElement))+"&requestmobileversion="+b_to_str(requestMobileVersion)+"&callback="
|
78
|
+
@@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
|
79
|
+
@@signaturePartTwo = "|"+nil_check(customId)+"|"+nil_int_check(tableNumberToInclude)+"|"+b_to_str(includeAllTables)+"|"+b_to_str(includeHeaderNames)+"|"+nil_check(targetElement)+"|"+nil_check(format)+"|"+b_to_str(requestMobileVersion)
|
80
|
+
|
81
|
+
return nil
|
82
|
+
end
|
83
|
+
|
84
|
+
# Sets the parameters required to convert a web page into a PDF.
|
85
|
+
# @param url url [String] the URL that the should be converted into a pdf
|
86
|
+
# @param customId [String, nil] a custom identifier that you can pass through to the webservice. This will be returned with the callback URL you have specified.
|
87
|
+
# @param includeBackground [Boolean, true] if true the background of the web page should be included in the screenshot
|
88
|
+
# @param pagesize [String, 'A4'] the page size of the PDF to be returned: 'A3', 'A4', 'A5', 'B3', 'B4', 'B5'.
|
89
|
+
# @param orientation [String, 'Portrait'] the orientation of the PDF to be returned: 'Landscape' or 'Portrait'
|
90
|
+
# @param includeLinks [Boolean, true] true if links should be included in the PDF
|
91
|
+
# @param includeOutline [Boolean, false] True if the PDF outline should be included
|
92
|
+
# @param title [String, nil] provide a title to the PDF document
|
93
|
+
# @param coverURL [String, nil] the URL of a web page that should be used as a cover page for the PDF
|
94
|
+
# @param marginTop [Integer, 10] the margin that should appear at the top of the PDF document page
|
95
|
+
# @param marginLeft [Integer, 10] the margin that should appear at the left of the PDF document page
|
96
|
+
# @param marginBottom [Integer, 10] the margin that should appear at the bottom of the PDF document page
|
97
|
+
# @param marginRight [Integer, 10] the margin that should appear at the right of the PDF document
|
98
|
+
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
99
|
+
# @param requestMobileVersion [Boolean, false] request a mobile version of the target website if possible
|
100
|
+
# @param customWaterMarkId [String, nil] add a custom watermark to each page of the PDF document
|
101
|
+
# @return [void]
|
102
|
+
def set_pdf_options(url, customId = nil, includeBackground = true, pagesize = 'A4', orientation = 'Portrait', includeLinks = true, includeOutline = false, title = nil, coverURL = nil, marginTop = 10, marginLeft = 10, marginBottom = 10, marginRight = 10, delay = nil, requestMobileVersion = false, customWaterMarkId = nil)
|
103
|
+
pagesize = nil_check(pagesize).upcase
|
104
|
+
$orientation = nil_check(orientation).capitalize
|
105
|
+
|
106
|
+
@@request = WebServicesBaseURL + "takepdf.ashx?key=" + CGI.escape(nil_check(@applicationKey))+"&url="+CGI.escape(nil_check(url))+"&background="+b_to_str(includeBackground)+"&pagesize="+pagesize+"&orientation="+orientation+"&customid="+CGI.escape(nil_check(customId))+"&customwatermarkid="+CGI.escape(nil_check(customWaterMarkId))+"&includelinks="+b_to_str(includeLinks)+"&includeoutline="+b_to_str(includeOutline)+"&title="+CGI.escape(nil_check(title))+"&coverurl="+CGI.escape(nil_check(coverURL))+"&mleft="+nil_int_check(marginLeft)+"&mright="+nil_int_check(marginRight)+"&mtop="+nil_int_check(marginTop)+"&mbottom="+nil_int_check(marginBottom)+"&delay="+nil_int_check(delay)+"&requestmobileversion="+b_to_str(requestMobileVersion)+"&callback="
|
107
|
+
|
108
|
+
@@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
|
109
|
+
@@signaturePartTwo = "|"+nil_check(customId)+"|"+b_to_str(includeBackground)+"|"+pagesize +"|"+orientation+"|"+nil_check(customWaterMarkId)+"|"+b_to_str(includeLinks)+"|"+b_to_str(includeOutline)+"|"+nil_check(title)+"|"+nil_check(coverURL)+"|"+nil_int_check(marginTop)+"|"+nil_int_check(marginLeft)+"|"+nil_int_check(marginBottom)+"|"+nil_int_check(marginRight)+"|"+nil_int_check(delay)+"|"+b_to_str(requestMobileVersion)
|
110
|
+
|
111
|
+
return nil
|
112
|
+
end
|
113
|
+
|
114
|
+
# Calls the GrabzIt web service to take the screenshot
|
115
|
+
#
|
116
|
+
# The handler will be passed a URL with the following query string parameters:
|
117
|
+
# - message (is any error message associated with the screenshot)
|
118
|
+
# - customId (is a custom id you may have specified in the {#set_image_options}, {#set_table_options} or {#set_pdf_options} method)
|
119
|
+
# - id (is the unique id of the screenshot which can be used to retrieve the screenshot with the {#get_result} method)
|
120
|
+
# - filename (is the filename of the screenshot)
|
121
|
+
# - format (is the format of the screenshot)
|
122
|
+
# @note This is the recommended method of saving a screenshot
|
123
|
+
# @param callBackURL [String, nil] the handler the GrabzIt web service should call after it has completed its work
|
124
|
+
# @return [String] the unique identifier of the screenshot. This can be used to get the screenshot with the {#get_result} method
|
125
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
126
|
+
def save(callBackURL = nil)
|
127
|
+
if @@signaturePartOne == nil && @@signaturePartTwo == nil && @@request == nil
|
128
|
+
raise "No screenshot parameters have been set."
|
129
|
+
end
|
130
|
+
|
131
|
+
sig = Digest::MD5.hexdigest(@@signaturePartOne+nil_check(callBackURL)+@@signaturePartTwo)
|
132
|
+
@@request += CGI.escape(nil_check(callBackURL))+"&sig="+sig
|
133
|
+
|
134
|
+
return get_result_value(get(@@request), "ID")
|
135
|
+
end
|
136
|
+
|
137
|
+
# Calls the GrabzIt web service to take the screenshot and saves it to the target path provided
|
138
|
+
# @note Warning, this is a SYNCHONOUS method and can take up to 5 minutes before a response
|
139
|
+
# @param saveToFile [String] the file path that the screenshot should saved to.
|
140
|
+
# @example Synchronously save the screenshot to test.jpg
|
141
|
+
# save_to('images/test.jpg')
|
142
|
+
# @raise [RuntimeError] if the screenshot cannot be saved a RuntimeError will be raised that will contain an explanation
|
143
|
+
# @return [Boolean] returns the true if it is successful otherwise it throws an exception.
|
144
|
+
def save_to(saveToFile)
|
145
|
+
id = save()
|
146
|
+
|
147
|
+
#Wait for it to be ready.
|
148
|
+
while true do
|
149
|
+
status = get_status(id)
|
150
|
+
|
151
|
+
if !status.cached && !status.processing
|
152
|
+
raise "The screenshot did not complete with the error: " + status.Message
|
153
|
+
break;
|
154
|
+
elsif status.cached
|
155
|
+
result = get_result(id)
|
156
|
+
if !result
|
157
|
+
raise "The screenshot image could not be found on GrabzIt."
|
158
|
+
break
|
159
|
+
end
|
160
|
+
|
161
|
+
screenshot = File.new(saveToFile, "wb")
|
162
|
+
screenshot.write(result)
|
163
|
+
screenshot.close
|
164
|
+
|
165
|
+
break
|
166
|
+
end
|
167
|
+
|
168
|
+
sleep(1)
|
169
|
+
end
|
170
|
+
|
171
|
+
return true
|
172
|
+
end
|
173
|
+
|
174
|
+
|
175
|
+
# Get the current status of a GrabzIt screenshot
|
176
|
+
# @param id [String] the id of the screenshot
|
177
|
+
# @return [ScreenShotStatus] a object representing the status of the screenshot
|
178
|
+
def get_status(id)
|
179
|
+
result = get(WebServicesBaseURL + "getstatus.ashx?id=" + nil_check(id))
|
180
|
+
|
181
|
+
doc = REXML::Document.new(result)
|
182
|
+
|
183
|
+
processing = doc.root.elements["Processing"].text()
|
184
|
+
cached = doc.root.elements["Cached"].text()
|
185
|
+
expired = doc.root.elements["Expired"].text()
|
186
|
+
message = doc.root.elements["Message"].text()
|
187
|
+
|
188
|
+
return ScreenShotStatus.new((processing == TrueString), (cached == TrueString), (expired == TrueString), message)
|
189
|
+
end
|
190
|
+
|
191
|
+
# This method returns the screenshot itself. If nothing is returned then something has gone wrong or the screenshot is not ready yet
|
192
|
+
# @param id [String] the id of the screenshot
|
193
|
+
# @return [Object] returns the screenshot
|
194
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
195
|
+
def get_result(id)
|
196
|
+
return get(WebServicesBaseURL + "getfile.ashx?id=" + nil_check(id))
|
197
|
+
end
|
198
|
+
|
199
|
+
# Get all the cookies that GrabzIt is using for a particular domain. This may include your user set cookies as well
|
200
|
+
# @param domain [String] the domain to return cookies for
|
201
|
+
# @return [Array<Cookie>] an array of cookies
|
202
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
203
|
+
def get_cookies(domain)
|
204
|
+
sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(domain))
|
205
|
+
|
206
|
+
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&sig="+sig
|
207
|
+
|
208
|
+
result = get(WebServicesBaseURL + "getcookies.ashx?" + qs)
|
209
|
+
|
210
|
+
doc = REXML::Document.new(result)
|
211
|
+
|
212
|
+
message = doc.root.elements["Message"].text()
|
213
|
+
|
214
|
+
if message != nil
|
215
|
+
raise message
|
216
|
+
end
|
217
|
+
|
218
|
+
cookies = Array.new
|
219
|
+
|
220
|
+
xml_cookies = doc.elements.to_a("//WebResult/Cookies/Cookie")
|
221
|
+
xml_cookies.each do |cookie|
|
222
|
+
expires = nil
|
223
|
+
if cookie.elements["Expires"] != nil
|
224
|
+
expires = cookie.elements["Expires"].text
|
225
|
+
end
|
226
|
+
grabzItCookie = GrabzIt::Cookie.new(cookie.elements["Name"].text, cookie.elements["Domain"].text, cookie.elements["Value"].text, cookie.elements["Path"].text, (cookie.elements["HttpOnly"].text == TrueString), expires, cookie.elements["Type"].text)
|
227
|
+
cookies << grabzItCookie
|
228
|
+
end
|
229
|
+
|
230
|
+
return cookies
|
231
|
+
end
|
232
|
+
|
233
|
+
# Sets a new custom cookie on GrabzIt, if the custom cookie has the same name and domain as a global cookie the global
|
234
|
+
# cookie is overridden
|
235
|
+
# @note This can be useful if a websites functionality is controlled by cookies
|
236
|
+
# @param name [String] the name of the cookie to set
|
237
|
+
# @param domain [String] the domain of the website to set the cookie for
|
238
|
+
# @param value [String, ''] the value of the cookie
|
239
|
+
# @param path [String, '/'] the website path the cookie relates to
|
240
|
+
# @param httponly [Boolean, false] is the cookie only used on HTTP
|
241
|
+
# @param expires [String, ''] when the cookie expires. Pass a nil value if it does not expire
|
242
|
+
# @return [Boolean] returns true if the cookie was successfully set
|
243
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
244
|
+
def set_cookie(name, domain, value = "", path = "/", httponly = false, expires = "")
|
245
|
+
sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|"+nil_check(value)+"|"+nil_check(path)+"|"+b_to_str(httponly)+"|"+nil_check(expires)+"|0")
|
246
|
+
|
247
|
+
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&name="+CGI.escape(nil_check(name))+"&value="+CGI.escape(nil_check(value))+"&path="+CGI.escape(nil_check(path))+"&httponly="+b_to_str(httponly)+"&expires="+nil_check(expires)+"&sig="+sig
|
248
|
+
|
249
|
+
return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString)
|
250
|
+
end
|
251
|
+
|
252
|
+
# Delete a custom cookie or block a global cookie from being used
|
253
|
+
# @param name [String] the name of the cookie to delete
|
254
|
+
# @param domain [String] the website the cookie belongs to
|
255
|
+
# @return [Boolean] returns true if the cookie was successfully set
|
256
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
257
|
+
def delete_cookie(name, domain)
|
258
|
+
sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|1")
|
259
|
+
|
260
|
+
qs = "key=" + CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&name="+CGI.escape(nil_check(name))+"&delete=1&sig="+sig
|
261
|
+
|
262
|
+
return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString)
|
263
|
+
end
|
264
|
+
|
265
|
+
# Get your uploaded custom watermarks
|
266
|
+
# @param identifier [String, nil] the identifier of a particular custom watermark you want to view
|
267
|
+
# @return [Array<WaterMark>] an array of uploaded watermarks
|
268
|
+
def get_watermarks(identifier = nil)
|
269
|
+
sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(identifier))
|
270
|
+
|
271
|
+
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig
|
272
|
+
|
273
|
+
result = get(WebServicesBaseURL + "getwatermarks.ashx?" + qs)
|
274
|
+
|
275
|
+
doc = REXML::Document.new(result)
|
276
|
+
|
277
|
+
watermarks = Array.new
|
278
|
+
|
279
|
+
xml_watemarks = doc.elements.to_a("//WebResult/WaterMarks/WaterMark")
|
280
|
+
xml_watemarks.each do |watemark|
|
281
|
+
grabzItWaterMark = GrabzIt::WaterMark.new(watemark.elements["Identifier"].text, watemark.elements["XPosition"].text.to_i, watemark.elements["YPosition"].text.to_i, watemark.elements["Format"].text)
|
282
|
+
watermarks << grabzItWaterMark
|
283
|
+
end
|
284
|
+
|
285
|
+
return watermarks
|
286
|
+
end
|
287
|
+
|
288
|
+
# Add a new custom watermark
|
289
|
+
# @param identifier [String] the identifier you want to give the custom watermark. It is important that this identifier is unique.
|
290
|
+
# @param path [String] the absolute path of the watermark on your server. For instance C:/watermark/1.png
|
291
|
+
# @param xpos [Integer] the horizontal position you want the screenshot to appear at: Left = 0, Center = 1, Right = 2
|
292
|
+
# @param ypos [Integer] the vertical position you want the screenshot to appear at: Top = 0, Middle = 1, Bottom = 2
|
293
|
+
# @return [Boolean] returns true if the watermark was successfully set
|
294
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
295
|
+
def add_watermark(identifier, path, xpos, ypos)
|
296
|
+
if !File.file?(path)
|
297
|
+
raise "File: " + path + " does not exist"
|
298
|
+
end
|
299
|
+
sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(identifier)+"|"+nil_int_check(xpos)+"|"+nil_int_check(ypos));
|
300
|
+
|
301
|
+
boundary = '--------------------------'+Time.now.to_f.to_s
|
302
|
+
|
303
|
+
url = WebServicesBaseURL + "addwatermark.ashx"
|
304
|
+
uri = URI.parse(url)
|
305
|
+
|
306
|
+
file = File.open(path, "rb")
|
307
|
+
data = file.read
|
308
|
+
|
309
|
+
post_body = Array.new
|
310
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
311
|
+
post_body << "Content-Disposition: form-data; name=\"watermark\"; filename=\""+File.basename(path)+"\"\r\nContent-Type: image/jpeg\r\n\r\n"
|
312
|
+
post_body << data
|
313
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
314
|
+
|
315
|
+
post_body << "Content-Disposition: form-data; name=\"key\"\r\n\r\n"
|
316
|
+
post_body << CGI.escape(nil_check(@applicationKey))
|
317
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
318
|
+
|
319
|
+
post_body << "Content-Disposition: form-data; name=\"identifier\"\r\n\r\n"
|
320
|
+
post_body << CGI.escape(nil_check(identifier))
|
321
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
322
|
+
|
323
|
+
post_body << "Content-Disposition: form-data; name=\"xpos\"\r\n\r\n"
|
324
|
+
post_body << nil_check(xpos)
|
325
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
326
|
+
|
327
|
+
post_body << "Content-Disposition: form-data; name=\"ypos\"\r\n\r\n"
|
328
|
+
post_body << nil_check(ypos)
|
329
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
330
|
+
|
331
|
+
post_body << "Content-Disposition: form-data; name=\"sig\"\r\n\r\n"
|
332
|
+
post_body << sig
|
333
|
+
post_body << "\r\n--"+boundary+"--\r\n"
|
334
|
+
|
335
|
+
request = Net::HTTP::Post.new(url)
|
336
|
+
request.content_type = "multipart/form-data, boundary="+boundary
|
337
|
+
request.body = post_body.join
|
338
|
+
|
339
|
+
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(request) }
|
340
|
+
|
341
|
+
return (get_result_value(response.body(), "Result") == TrueString)
|
342
|
+
end
|
343
|
+
|
344
|
+
# Delete a custom watermark
|
345
|
+
# @param identifier [String] the identifier of the custom watermark you want to delete
|
346
|
+
# @return [Boolean] returns true if the watermark was successfully deleted
|
347
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
348
|
+
def delete_watermark(identifier)
|
349
|
+
sig = Digest::MD5.hexdigest(nil_check(@applicationSecret)+"|"+nil_check(identifier))
|
350
|
+
|
351
|
+
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig
|
352
|
+
|
353
|
+
return (get_result_value(get(WebServicesBaseURL + "deletewatermark.ashx?" + qs), "Result") == TrueString)
|
354
|
+
end
|
355
|
+
|
356
|
+
# This method calls the GrabzIt web service to take the screenshot.
|
357
|
+
# @param url [String] the URL that the screenshot should be made of
|
358
|
+
# @param callback [String, nil] the handler the GrabzIt web service should call after it has completed its work
|
359
|
+
# @param customId [String, nil] custom identifier that you can pass through to the screenshot webservice. This will be returned with the callback URL you have specified.
|
360
|
+
# @param browserWidth [Integer, nil] the width of the browser in pixels
|
361
|
+
# @param browserHeight [Integer, nil] the height of the browser in pixels
|
362
|
+
# @param width [Integer, nil] the width of the resulting thumbnail in pixels
|
363
|
+
# @param height [Integer, nil] the height of the resulting thumbnail in pixels
|
364
|
+
# @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
365
|
+
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
366
|
+
# @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
|
367
|
+
# @return [String] returns the unique identifier of the screenshot. This can be used to get the screenshot with the get_result method
|
368
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
369
|
+
# @deprecated Use {#set_image_options} and {#save} instead.
|
370
|
+
def take_picture(url, callback = nil, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
|
371
|
+
set_image_options(url, customId, browserWidth, browserHeight, width, height, format, delay, targetElement)
|
372
|
+
return save(callback)
|
373
|
+
end
|
374
|
+
|
375
|
+
# This method takes the screenshot and then saves the result to a file. WARNING this method is synchronous
|
376
|
+
# @param url [String] the URL that the screenshot should be made of
|
377
|
+
# @param saveToFile [String] the file path that the screenshot should saved to
|
378
|
+
# @param browserWidth [Integer, nil] the width of the browser in pixels
|
379
|
+
# @param browserHeight [Integer, nil] the height of the browser in pixels
|
380
|
+
# @param width [Integer, nil] the width of the resulting thumbnail in pixels
|
381
|
+
# @param height [Integer, nil] the height of the resulting thumbnail in pixels
|
382
|
+
# @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
383
|
+
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
384
|
+
# @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
|
385
|
+
# @example Synchronously save the screenshot to test.jpg
|
386
|
+
# save_picture('images/test.jpg')
|
387
|
+
# @return [Boolean] returns the true if it is successfull otherwise it throws an exception
|
388
|
+
# @raise [RuntimeError] if the screenshot cannot be saved a RuntimeError will be raised that will contain an explanation
|
389
|
+
# @deprecated Use {#set_image_options} and {#save_to} instead.
|
390
|
+
def save_picture(url, saveToFile, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
|
391
|
+
set_image_options(url, nil, browserWidth, browserHeight, width, height, format, delay, targetElement)
|
392
|
+
return save_to(saveToFile)
|
393
|
+
end
|
394
|
+
|
395
|
+
# This method returns the image itself. If nothing is returned then something has gone wrong or the image is not ready yet.
|
396
|
+
# @param id [String] the id of the screenshot
|
397
|
+
# @return [Object] returns the screenshot
|
398
|
+
# @deprecated Use {#get_result} instead.
|
399
|
+
def get_picture(id)
|
400
|
+
return get_result(id)
|
401
|
+
end
|
402
|
+
|
403
|
+
private
|
404
|
+
def get(url)
|
405
|
+
Net::HTTP.get(URI.parse(url))
|
406
|
+
end
|
407
|
+
|
408
|
+
private
|
409
|
+
def b_to_str(bValue)
|
410
|
+
if bValue
|
411
|
+
return 1.to_s
|
412
|
+
end
|
413
|
+
return 0.to_s
|
414
|
+
end
|
415
|
+
|
416
|
+
private
|
417
|
+
def nil_check(param)
|
418
|
+
if param == nil
|
419
|
+
return ""
|
420
|
+
end
|
421
|
+
return param
|
422
|
+
end
|
423
|
+
|
424
|
+
private
|
425
|
+
def nil_int_check(param)
|
426
|
+
return param.to_i.to_s
|
427
|
+
end
|
428
|
+
|
429
|
+
private
|
430
|
+
def get_result_value(result, field)
|
431
|
+
doc = REXML::Document.new(result)
|
432
|
+
|
433
|
+
message = doc.root.elements["Message"].text()
|
434
|
+
value = doc.root.elements[field].text()
|
435
|
+
|
436
|
+
if message != nil
|
437
|
+
raise message
|
438
|
+
end
|
439
|
+
|
440
|
+
return value
|
441
|
+
end
|
442
|
+
end
|
443
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module GrabzIt
|
2
|
+
# This class represents the cookies stored in GrabzIt
|
3
|
+
# @version 2.0
|
4
|
+
# @author GrabzIt
|
5
|
+
class Cookie
|
6
|
+
# @api private
|
7
|
+
def initialize(name = '', domain = '', value = '', path = '', httpOnly = false, expires = nil, type = nil)
|
8
|
+
@Name = name
|
9
|
+
@Value = value
|
10
|
+
@Domain = domain
|
11
|
+
@Path = path
|
12
|
+
@HttpOnly = httpOnly
|
13
|
+
@Expires = expires
|
14
|
+
@Type = type
|
15
|
+
end
|
16
|
+
# @return [String] the name of the cookie
|
17
|
+
def name
|
18
|
+
@Name
|
19
|
+
end
|
20
|
+
# @return [String] the value of the cookie
|
21
|
+
def value
|
22
|
+
@Value
|
23
|
+
end
|
24
|
+
# @return [String] the domain of the cookie
|
25
|
+
def domain
|
26
|
+
@Domain
|
27
|
+
end
|
28
|
+
# @return [String] the path of the cookie
|
29
|
+
def path
|
30
|
+
@Path
|
31
|
+
end
|
32
|
+
# @return [Boolean] is the cookie httponly
|
33
|
+
def httpOnly
|
34
|
+
@HttpOnly
|
35
|
+
end
|
36
|
+
# @return [String] the date and time the cookie expires
|
37
|
+
def expires
|
38
|
+
@Expires
|
39
|
+
end
|
40
|
+
# @return [String] the type of cookie
|
41
|
+
def type
|
42
|
+
@Type
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module GrabzIt
|
2
|
+
# This class represents the screenshot status
|
3
|
+
# @version 2.0
|
4
|
+
# @author GrabzIt
|
5
|
+
class ScreenShotStatus
|
6
|
+
# @api private
|
7
|
+
def initialize(processing = false, cached = false, expired = false, message = '')
|
8
|
+
@Processing = processing
|
9
|
+
@Cached = cached
|
10
|
+
@Expired = expired
|
11
|
+
@Message = message
|
12
|
+
end
|
13
|
+
# @return [Boolean] if true the screenshot is still being processed
|
14
|
+
def processing
|
15
|
+
@Processing
|
16
|
+
end
|
17
|
+
# @return [Boolean] if true the screenshot has been cached
|
18
|
+
def cached
|
19
|
+
@Cached
|
20
|
+
end
|
21
|
+
# @return [Boolean] if true the screenshot has expired
|
22
|
+
def expired
|
23
|
+
@Expired
|
24
|
+
end
|
25
|
+
# @return [String] returns any error messages associated with the screenshot
|
26
|
+
def message
|
27
|
+
@Message
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module GrabzIt
|
2
|
+
# This class represents the custom watermarks stored in GrabzIt
|
3
|
+
# @version 2.0
|
4
|
+
# @author GrabzIt
|
5
|
+
class WaterMark
|
6
|
+
# @api private
|
7
|
+
def initialize(identifier = '', xPosition = 0, yPosition = 0, format = '')
|
8
|
+
@Identifier = identifier
|
9
|
+
@XPosition = xPosition
|
10
|
+
@YPosition = yPosition
|
11
|
+
@Format = format
|
12
|
+
end
|
13
|
+
# @return [String] the identifier of the watermark
|
14
|
+
def identifier
|
15
|
+
@Identifier
|
16
|
+
end
|
17
|
+
# @return [Integer] the horizontal postion of the watermark. 0 = Left, 1 = Center, 2 = Right
|
18
|
+
def xPosition
|
19
|
+
@XPosition
|
20
|
+
end
|
21
|
+
# @return [Integer] the vertical postion of the watermark. 0 = Top, 1 = Middle, 2 = Bottom
|
22
|
+
def yPosition
|
23
|
+
@YPosition
|
24
|
+
end
|
25
|
+
# @return [String] the format of the watermark
|
26
|
+
def format
|
27
|
+
@Format
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/test/test.png
ADDED
Binary file
|
@@ -0,0 +1,185 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'grabzit'
|
3
|
+
|
4
|
+
class GrabzItTest < Test::Unit::TestCase
|
5
|
+
Cookie_Name = "test_cookie"
|
6
|
+
Cookie_Domain = ".example.com"
|
7
|
+
WaterMark_Identifier = "test_watermark"
|
8
|
+
WaterMark_Path = "test/test.png"
|
9
|
+
Screenshot_Path = "test/tmp.jpg"
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@applicationKey = "c3VwcG9ydEBncmFiei5pdA=="
|
13
|
+
@applicationSecret = "AD8/aT8/Pz8/Tz8/PwJ3Pz9sVSs/Pz8/Pz9DOzJoPwY="
|
14
|
+
#Set to true if the account is subscribed
|
15
|
+
@isSubscribedAccount = true
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_application_key
|
19
|
+
assert_not_nil(@applicationKey, "Please set your application key variable in the setup method. You can get a application key for free from: http://grabz.it")
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_application_secret
|
23
|
+
assert_not_nil(@applicationSecret, "Please set your application secret variable in the setup method. You can get a application key for free from: http://grabz.it")
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_take_picture
|
27
|
+
assert_nothing_raised "An error occured when trying to take a picture" do
|
28
|
+
grabzItClient = GrabzIt::Client.new(@applicationKey, @applicationSecret)
|
29
|
+
assert_not_nil(grabzItClient.take_picture("http://www.google.com"), "Failed to take screenshot using depreceated method")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_take_pdf
|
34
|
+
assert_nothing_raised "An error occured when trying to take a pdf screenshot" do
|
35
|
+
grabzItClient = GrabzIt::Client.new(@applicationKey, @applicationSecret)
|
36
|
+
grabzItClient.set_pdf_options("http://www.google.com")
|
37
|
+
assert_not_nil(grabzItClient.save(), "Failed to take screenshot using set_pdf_options method")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_take_image
|
42
|
+
assert_nothing_raised "An error occured when trying to take a image screenshot" do
|
43
|
+
grabzItClient = GrabzIt::Client.new(@applicationKey, @applicationSecret)
|
44
|
+
grabzItClient.set_image_options("http://www.google.com")
|
45
|
+
assert_not_nil(grabzItClient.save(), "Failed to take screenshot using set_image_options method")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_save_picture
|
50
|
+
if File.file?(Screenshot_Path)
|
51
|
+
File.delete(Screenshot_Path)
|
52
|
+
end
|
53
|
+
assert_nothing_raised "An error occured when trying to take a image screenshot" do
|
54
|
+
grabzItClient = GrabzIt::Client.new(@applicationKey, @applicationSecret)
|
55
|
+
assert_equal(true, grabzItClient.save_picture("http://www.google.com", Screenshot_Path), "Screenshot not saved")
|
56
|
+
assert_equal(true, File.file?(Screenshot_Path), "Not saved screenshot file")
|
57
|
+
end
|
58
|
+
File.delete(Screenshot_Path)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_status
|
62
|
+
assert_nothing_raised "An error occured when trying to test the status method" do
|
63
|
+
grabzItClient = GrabzIt::Client.new(@applicationKey, @applicationSecret)
|
64
|
+
grabzItClient.set_image_options("http://www.google.com")
|
65
|
+
id = grabzItClient.save()
|
66
|
+
status = grabzItClient.get_status(id)
|
67
|
+
|
68
|
+
assert_equal(true, (status.processing || status.cached), "Failed to get correct screenshot status!")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_add_cookie
|
73
|
+
assert_nothing_raised "An error occured when trying to add a cookie" do
|
74
|
+
grabzItClient = GrabzIt::Client.new(@applicationKey, @applicationSecret)
|
75
|
+
|
76
|
+
if @isSubscribedAccount
|
77
|
+
grabzItClient.set_cookie(Cookie_Name, Cookie_Domain)
|
78
|
+
elsif
|
79
|
+
assert_raise RuntimeError, "User not subscribed should throw error. If user is subscribed please set @isSubscribedAccount in the setup method" do
|
80
|
+
grabzItClient.set_cookie(Cookie_Name, Cookie_Domain)
|
81
|
+
end
|
82
|
+
return
|
83
|
+
end
|
84
|
+
|
85
|
+
assert(find_cookie(grabzItClient), "Set cookie has not been found!")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_delete_cookie
|
90
|
+
assert_nothing_raised "An error occured when trying to add a cookie" do
|
91
|
+
grabzItClient = GrabzIt::Client.new(@applicationKey, @applicationSecret)
|
92
|
+
if @isSubscribedAccount
|
93
|
+
grabzItClient.set_cookie(Cookie_Name, Cookie_Domain)
|
94
|
+
elsif
|
95
|
+
assert_raise RuntimeError, "User not subscribed should throw error. If user is subscribed please set @isSubscribedAccount in the setup method" do
|
96
|
+
grabzItClient.set_cookie(Cookie_Name, Cookie_Domain)
|
97
|
+
end
|
98
|
+
return
|
99
|
+
end
|
100
|
+
|
101
|
+
assert_equal(true, find_cookie(grabzItClient), "Test cookie not found!")
|
102
|
+
|
103
|
+
grabzItClient.delete_cookie(Cookie_Name, Cookie_Domain)
|
104
|
+
|
105
|
+
assert_equal(false, find_cookie(grabzItClient), "Failed to delete cookie!")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_delete_watermark
|
110
|
+
assert_nothing_raised "An error occured when trying to add a watermark" do
|
111
|
+
grabzItClient = GrabzIt::Client.new(@applicationKey, @applicationSecret)
|
112
|
+
begin
|
113
|
+
grabzItClient.delete_watermark(WaterMark_Identifier)
|
114
|
+
rescue
|
115
|
+
end
|
116
|
+
|
117
|
+
if @isSubscribedAccount
|
118
|
+
grabzItClient.add_watermark(WaterMark_Identifier, WaterMark_Path, 2, 2)
|
119
|
+
elsif
|
120
|
+
assert_raise RuntimeError, "User not subscribed should throw error. If user is subscribed please set @isSubscribedAccount in the setup method" do
|
121
|
+
grabzItClient.add_watermark(WaterMark_Identifier, WaterMark_Path, 2, 2)
|
122
|
+
end
|
123
|
+
return
|
124
|
+
end
|
125
|
+
|
126
|
+
assert_equal(true, find_watermark(grabzItClient), "Test watermark not found!")
|
127
|
+
|
128
|
+
grabzItClient.delete_watermark(WaterMark_Identifier)
|
129
|
+
|
130
|
+
assert_equal(false, find_watermark(grabzItClient), "Failed to delete watermark!")
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_add_watermark
|
135
|
+
assert_nothing_raised "An error occured when trying to add a watermark" do
|
136
|
+
grabzItClient = GrabzIt::Client.new(@applicationKey, @applicationSecret)
|
137
|
+
begin
|
138
|
+
grabzItClient.delete_watermark(WaterMark_Identifier)
|
139
|
+
rescue
|
140
|
+
end
|
141
|
+
if @isSubscribedAccount
|
142
|
+
grabzItClient.add_watermark(WaterMark_Identifier, WaterMark_Path, 2, 2)
|
143
|
+
elsif
|
144
|
+
assert_raise RuntimeError, "User not subscribed should throw error. If user is subscribed please set @isSubscribedAccount in the setup method" do
|
145
|
+
grabzItClient.add_watermark(WaterMark_Identifier, WaterMark_Path, 2, 2)
|
146
|
+
end
|
147
|
+
return
|
148
|
+
end
|
149
|
+
|
150
|
+
assert(find_watermark(grabzItClient), "Set watermark has not been found!")
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def find_cookie(grabzItClient)
|
155
|
+
cookies = grabzItClient.get_cookies(Cookie_Domain)
|
156
|
+
|
157
|
+
if cookies == nil
|
158
|
+
return false
|
159
|
+
end
|
160
|
+
|
161
|
+
cookies.each { |cookie|
|
162
|
+
if cookie.name == Cookie_Name
|
163
|
+
return true
|
164
|
+
end
|
165
|
+
}
|
166
|
+
|
167
|
+
return false
|
168
|
+
end
|
169
|
+
|
170
|
+
def find_watermark(grabzItClient)
|
171
|
+
watermarks = grabzItClient.get_watermarks(WaterMark_Identifier)
|
172
|
+
|
173
|
+
if watermarks == nil
|
174
|
+
return false
|
175
|
+
end
|
176
|
+
|
177
|
+
watermarks.each { |watermark|
|
178
|
+
if watermark.identifier == WaterMark_Identifier
|
179
|
+
return true
|
180
|
+
end
|
181
|
+
}
|
182
|
+
|
183
|
+
return false
|
184
|
+
end
|
185
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grabzit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,18 +9,40 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
-
dependencies:
|
14
|
-
|
15
|
-
|
12
|
+
date: 2013-03-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !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: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ! 'Automatically create screenshots of website for free, with GrabzIt.
|
31
|
+
Screenshots can be output in a variety of formats including: JPEG, PNG, TIFF, BMP,
|
32
|
+
PDF, CSV and XLSX.'
|
16
33
|
email: support@grabz.it
|
17
34
|
executables: []
|
18
35
|
extensions: []
|
19
36
|
extra_rdoc_files: []
|
20
37
|
files:
|
21
|
-
-
|
22
|
-
- lib/
|
23
|
-
- lib/
|
38
|
+
- Rakefile
|
39
|
+
- lib/grabzit.rb
|
40
|
+
- lib/grabzit/client.rb
|
41
|
+
- lib/grabzit/cookie.rb
|
42
|
+
- lib/grabzit/screenshotstatus.rb
|
43
|
+
- lib/grabzit/watermark.rb
|
44
|
+
- test/test.png
|
45
|
+
- test/test_grabzit.rb
|
24
46
|
homepage: http://grabz.it/api/ruby
|
25
47
|
licenses: []
|
26
48
|
post_install_message:
|
@@ -41,8 +63,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
63
|
version: '0'
|
42
64
|
requirements: []
|
43
65
|
rubyforge_project:
|
44
|
-
rubygems_version: 1.8.
|
66
|
+
rubygems_version: 1.8.23
|
45
67
|
signing_key:
|
46
68
|
specification_version: 3
|
47
69
|
summary: GrabzIt Ruby Client
|
48
70
|
test_files: []
|
71
|
+
has_rdoc:
|
data/lib/grabzitclient.rb
DELETED
@@ -1,268 +0,0 @@
|
|
1
|
-
require 'digest/md5'
|
2
|
-
require 'net/http'
|
3
|
-
require 'rexml/document'
|
4
|
-
require 'cgi'
|
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
|
-
#targetElement - The id of the only HTML element in the web page to turn into a screenshot
|
33
|
-
#
|
34
|
-
#This function returns the unique identifier of the screenshot. This can be used to get the screenshot with the GetPicture method.
|
35
|
-
#
|
36
|
-
def take_picture(url, callback = nil, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
|
37
|
-
qs = "key=" + CGI.escape(@applicationKey) + "&url=" + CGI.escape(url) + "&width=" + nil_check(width) + "&height=" + nil_check(height) + "&format=" + nil_check(format) + "&bwidth=" + nil_check(browserWidth) + "&bheight=" + nil_check(browserHeight) + "&callback=" + CGI.escape(nil_check(callback)) + "&customid=" + CGI.escape(nil_check(customId)) + "&delay=" + nil_check(delay) + "&target=" + CGI.escape(nil_check(targetElement))
|
38
|
-
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)+"|"+nil_check(targetElement))
|
39
|
-
qs = qs + "&sig=" + sig
|
40
|
-
|
41
|
-
result = get(WebServicesBaseURL + "takepicture.ashx?" + qs)
|
42
|
-
|
43
|
-
doc = REXML::Document.new(result)
|
44
|
-
|
45
|
-
message = doc.root.elements["Message"].text()
|
46
|
-
id = doc.root.elements["ID"].text()
|
47
|
-
|
48
|
-
if message != nil
|
49
|
-
raise message
|
50
|
-
end
|
51
|
-
|
52
|
-
return id
|
53
|
-
end
|
54
|
-
|
55
|
-
#
|
56
|
-
#This method takes the screenshot and then saves the result to a file. WARNING this method is synchronous.
|
57
|
-
#
|
58
|
-
#url - The URL that the screenshot should be made of
|
59
|
-
#saveToFile - The file path that the screenshot should saved to: e.g. images/test.jpg
|
60
|
-
#browserWidth - The width of the browser in pixels
|
61
|
-
#browserHeight - The height of the browser in pixels
|
62
|
-
#outputHeight - The height of the resulting thumbnail in pixels
|
63
|
-
#outputWidth - The width of the resulting thumbnail in pixels
|
64
|
-
#format - The format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
65
|
-
#delay - The number of milliseconds to wait before taking the screenshot
|
66
|
-
#targetElement - The id of the only HTML element in the web page to turn into a screenshot
|
67
|
-
#
|
68
|
-
#This function returns the true if it is successfull otherwise it throws an exception.
|
69
|
-
#
|
70
|
-
def save_picture(url, saveToFile, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
|
71
|
-
id = take_picture(url, nil, nil, browserWidth, browserHeight, width, height, format, delay, targetElement)
|
72
|
-
|
73
|
-
#Wait for it to be ready.
|
74
|
-
while true do
|
75
|
-
status = get_status(id)
|
76
|
-
|
77
|
-
if !status.cached && !status.processing
|
78
|
-
raise "The screenshot did not complete with the error: " + status.Message
|
79
|
-
break;
|
80
|
-
elsif status.cached
|
81
|
-
result = get_picture(id)
|
82
|
-
if !result
|
83
|
-
raise "The screenshot image could not be found on GrabzIt."
|
84
|
-
break
|
85
|
-
end
|
86
|
-
|
87
|
-
screenshot = File.new(saveToFile, "wb")
|
88
|
-
screenshot.write(result)
|
89
|
-
screenshot.close
|
90
|
-
|
91
|
-
break
|
92
|
-
end
|
93
|
-
|
94
|
-
sleep(1)
|
95
|
-
end
|
96
|
-
|
97
|
-
return true
|
98
|
-
end
|
99
|
-
|
100
|
-
#
|
101
|
-
#Get the current status of a GrabzIt screenshot
|
102
|
-
#
|
103
|
-
#id - The id of the screenshot
|
104
|
-
#
|
105
|
-
#This function returns a Status object representing the screenshot
|
106
|
-
#
|
107
|
-
def get_status(id)
|
108
|
-
result = get(WebServicesBaseURL + "getstatus.ashx?id=" + id)
|
109
|
-
|
110
|
-
doc = REXML::Document.new(result)
|
111
|
-
|
112
|
-
processing = doc.root.elements["Processing"].text()
|
113
|
-
cached = doc.root.elements["Cached"].text()
|
114
|
-
expired = doc.root.elements["Expired"].text()
|
115
|
-
message = doc.root.elements["Message"].text()
|
116
|
-
|
117
|
-
status = ScreenShotStatus.new()
|
118
|
-
status.processing = (processing == TrueString)
|
119
|
-
status.cached = (cached == TrueString)
|
120
|
-
status.expired = (expired == TrueString)
|
121
|
-
status.message = message
|
122
|
-
|
123
|
-
return status
|
124
|
-
end
|
125
|
-
|
126
|
-
#
|
127
|
-
#This method returns the image itself. If nothing is returned then something has gone wrong or the image is not ready yet.
|
128
|
-
#
|
129
|
-
#id - The unique identifier of the screenshot, returned by the callback handler or the TakePicture method
|
130
|
-
#
|
131
|
-
#This function returns the screenshot
|
132
|
-
#
|
133
|
-
def get_picture(id)
|
134
|
-
result = get(WebServicesBaseURL + "getpicture.ashx?id=" + id)
|
135
|
-
|
136
|
-
if result == nil
|
137
|
-
return nil
|
138
|
-
end
|
139
|
-
|
140
|
-
return result
|
141
|
-
end
|
142
|
-
|
143
|
-
#
|
144
|
-
#Get all the cookies that GrabzIt is using for a particular domain. This may include your user set cookies as well.
|
145
|
-
#
|
146
|
-
#domain - The domain to return cookies for.
|
147
|
-
#
|
148
|
-
#This function returns an array of cookies
|
149
|
-
#
|
150
|
-
def get_cookies(domain)
|
151
|
-
sig = Digest::MD5.hexdigest(@applicationSecret+"|"+domain)
|
152
|
-
|
153
|
-
qs = "key=" +CGI.escape(@applicationKey)+"&domain="+CGI.escape(domain)+"&sig="+sig
|
154
|
-
|
155
|
-
result = get(WebServicesBaseURL + "getcookies.ashx?" + qs)
|
156
|
-
|
157
|
-
doc = REXML::Document.new(result)
|
158
|
-
|
159
|
-
message = doc.root.elements["Message"].text()
|
160
|
-
|
161
|
-
if message != nil
|
162
|
-
raise message
|
163
|
-
end
|
164
|
-
|
165
|
-
cookies = Array.new
|
166
|
-
|
167
|
-
xml_cookies = doc.elements.to_a("//WebResult/Cookies/Cookie")
|
168
|
-
xml_cookies.each do |cookie|
|
169
|
-
grabzItCookie = GrabzItCookie.new()
|
170
|
-
grabzItCookie.name = cookie.elements["Name"].text
|
171
|
-
grabzItCookie.value = cookie.elements["Value"].text
|
172
|
-
grabzItCookie.domain = cookie.elements["Domain"].text
|
173
|
-
grabzItCookie.path = cookie.elements["Path"].text
|
174
|
-
grabzItCookie.httpOnly = (cookie.elements["HttpOnly"].text == TrueString)
|
175
|
-
if cookie.elements["Expires"] != nil
|
176
|
-
grabzItCookie.expires = cookie.elements["Expires"].text
|
177
|
-
end
|
178
|
-
grabzItCookie.type = cookie.elements["Type"].text
|
179
|
-
|
180
|
-
cookies << grabzItCookie
|
181
|
-
end
|
182
|
-
|
183
|
-
return cookies
|
184
|
-
end
|
185
|
-
|
186
|
-
#
|
187
|
-
#Sets a new custom cookie on GrabzIt, if the custom cookie has the same name and domain as a global cookie the global
|
188
|
-
#cookie is overridden.
|
189
|
-
#
|
190
|
-
#This can be useful if a websites functionality is controlled by cookies.
|
191
|
-
#
|
192
|
-
#name - The name of the cookie to set.
|
193
|
-
#domain - The domain of the website to set the cookie for.
|
194
|
-
#value - The value of the cookie.
|
195
|
-
#path - The website path the cookie relates to.
|
196
|
-
#httponly - Is the cookie only used on HTTP
|
197
|
-
#expires - When the cookie expires. Pass a null value if it does not expire.
|
198
|
-
#
|
199
|
-
#This function returns true if the cookie was successfully set.
|
200
|
-
#
|
201
|
-
def set_cookie(name, domain, value = "", path = "/", httponly = false, expires = "")
|
202
|
-
sig = Digest::MD5.hexdigest(@applicationSecret+"|"+name+"|"+domain+"|"+nil_check(value)+"|"+nil_check(path)+"|"+b_to_int(httponly).to_s+"|"+nil_check(expires)+"|0")
|
203
|
-
|
204
|
-
qs = "key=" +CGI.escape(@applicationKey)+"&domain="+CGI.escape(domain)+"&name="+CGI.escape(name)+"&value="+CGI.escape(nil_check(value))+"&path="+CGI.escape(nil_check(path))+"&httponly="+b_to_int(httponly).to_s+"&expires="+nil_check(expires)+"&sig="+sig
|
205
|
-
|
206
|
-
result = get(WebServicesBaseURL + "setcookie.ashx?" + qs)
|
207
|
-
|
208
|
-
doc = REXML::Document.new(result)
|
209
|
-
|
210
|
-
message = doc.root.elements["Message"].text()
|
211
|
-
resultVal = doc.root.elements["Result"].text()
|
212
|
-
|
213
|
-
if message != nil
|
214
|
-
raise message
|
215
|
-
end
|
216
|
-
|
217
|
-
return (resultVal == TrueString)
|
218
|
-
end
|
219
|
-
|
220
|
-
#
|
221
|
-
#Delete a custom cookie or block a global cookie from being used.
|
222
|
-
#
|
223
|
-
#name - The name of the cookie to delete
|
224
|
-
#domain - The website the cookie belongs to
|
225
|
-
#
|
226
|
-
#This function returns true if the cookie was successfully set.
|
227
|
-
#
|
228
|
-
def delete_cookie(name, domain)
|
229
|
-
sig = Digest::MD5.hexdigest(@applicationSecret+"|"+name+"|"+domain+"|1")
|
230
|
-
|
231
|
-
qs = "key=" + CGI.escape(@applicationKey)+"&domain="+CGI.escape(domain)+"&name="+CGI.escape(name)+"&delete=1&sig="+sig
|
232
|
-
|
233
|
-
result = get(WebServicesBaseURL + "setcookie.ashx?" + qs)
|
234
|
-
|
235
|
-
doc = REXML::Document.new(result)
|
236
|
-
|
237
|
-
message = doc.root.elements["Message"].text()
|
238
|
-
resultVal = doc.root.elements["Result"].text()
|
239
|
-
|
240
|
-
if message != nil
|
241
|
-
raise message
|
242
|
-
end
|
243
|
-
|
244
|
-
return (resultVal == TrueString)
|
245
|
-
end
|
246
|
-
|
247
|
-
|
248
|
-
private
|
249
|
-
def get(url)
|
250
|
-
Net::HTTP.get(URI.parse(url))
|
251
|
-
end
|
252
|
-
|
253
|
-
private
|
254
|
-
def b_to_int(bValue)
|
255
|
-
if bValue
|
256
|
-
return 1
|
257
|
-
end
|
258
|
-
return 1
|
259
|
-
end
|
260
|
-
|
261
|
-
private
|
262
|
-
def nil_check(param)
|
263
|
-
if param == nil
|
264
|
-
return ""
|
265
|
-
end
|
266
|
-
return param
|
267
|
-
end
|
268
|
-
end
|
data/lib/grabzitcookie.rb
DELETED
@@ -1,53 +0,0 @@
|
|
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
|
data/lib/screenshotstatus.rb
DELETED
@@ -1,32 +0,0 @@
|
|
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
|