grabzit 2.2.1 → 2.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/grabzit/client.rb +583 -583
- metadata +7 -6
data/lib/grabzit/client.rb
CHANGED
@@ -1,583 +1,583 @@
|
|
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
|
-
require File.join(File.dirname(__FILE__), 'exception')
|
14
|
-
|
15
|
-
# This client provides access to the GrabzIt web services
|
16
|
-
# This API allows you to take screenshot of websites for free and convert them into images, PDF's and tables.
|
17
|
-
# @example Example usage
|
18
|
-
# require 'grabzit'
|
19
|
-
#
|
20
|
-
# grabzItClient = GrabzIt::Client.new("YOUR APPLICATION KEY", "YOUR APPLICATION SECRET")
|
21
|
-
# grabzItClient.set_image_options("http://www.google.com")
|
22
|
-
# grabzItClient.save("http://www.mysite.com/grabzit/handler")
|
23
|
-
# @version 2.1
|
24
|
-
# @author GrabzIt
|
25
|
-
# @see http://grabz.it/api/ruby/ GrabzIt Ruby API
|
26
|
-
class Client
|
27
|
-
|
28
|
-
WebServicesBaseURL = "http://api.grabz.it/services/"
|
29
|
-
private_constant :WebServicesBaseURL
|
30
|
-
TrueString = "True"
|
31
|
-
private_constant :TrueString
|
32
|
-
|
33
|
-
@@signaturePartOne;
|
34
|
-
@@signaturePartTwo;
|
35
|
-
@@startDelay;
|
36
|
-
@@request;
|
37
|
-
|
38
|
-
# Create a new instance of the Client class in order to access the GrabzIt API.
|
39
|
-
#
|
40
|
-
# @param applicationKey [String] your application key
|
41
|
-
# @param applicationSecret [String] your application secret
|
42
|
-
# @see http://grabz.it/register.aspx You can get an application key and secret by registering for free with GrabzIt
|
43
|
-
def initialize(applicationKey, applicationSecret)
|
44
|
-
@applicationKey = applicationKey
|
45
|
-
@applicationSecret = applicationSecret
|
46
|
-
end
|
47
|
-
|
48
|
-
# This method sets the parameters required to turn a online video into a animated GIF
|
49
|
-
#
|
50
|
-
# @param url [String] the URL of the online video
|
51
|
-
# @param customId [String, nil] custom identifier that you can pass through to the animated GIF web service. This will be returned with the callback URL you have specified
|
52
|
-
# @param width [Integer, 0] the width of the resulting animated GIF in pixels
|
53
|
-
# @param height [Integer, 0] the height of the resulting animated GIF in pixels
|
54
|
-
# @param start [Integer, 0] the starting position of the video that should be converted into a animated GIF
|
55
|
-
# @param duration [Integer, 0] the length in seconds of the video that should be converted into a animated GIF
|
56
|
-
# @param speed [Float, 0] the speed of the animated GIF from 0.2 to 10 times the original speed
|
57
|
-
# @param framesPerSecond [Float, 0] the number of frames per second that should be captured from the video. From a minimum of 0.2 to a maximum of 60
|
58
|
-
# @param repeat [Integer, 0] the number of times to loop the animated GIF. If 0 it will loop forever
|
59
|
-
# @param reverse [Boolean, false] if true the frames of the animated GIF are reversed
|
60
|
-
# @param customWaterMarkId [String, nil] add a custom watermark to the animated GIF
|
61
|
-
# @param quality [Integer, -1] the quality of the image where 0 is poor and 100 excellent. The default is -1 which uses the recommended quality
|
62
|
-
# @param country [String, nil] request the screenshot from different countries: Default, UK or US
|
63
|
-
# @return [void]
|
64
|
-
def set_animation_options(url, customId = nil, width = 0, height = 0, start = 0, duration = 0, speed = 0, framesPerSecond = 0, repeat = 0, reverse = false, customWaterMarkId = nil, quality = -1, country = nil)
|
65
|
-
@@startDelay = 0;
|
66
|
-
@@request = WebServicesBaseURL + "takeanimation.ashx?key=" + CGI.escape(nil_check(@applicationKey))+"&url="+CGI.escape(nil_check(url))+"&width="+nil_int_check(width)+"&height="+nil_int_check(height)+"&duration="+nil_int_check(duration)+"&speed="+nil_float_check(speed)+"&start="+nil_int_check(start)+"&customid="+CGI.escape(nil_check(customId))+"&fps="+nil_float_check(framesPerSecond)+"&repeat="+nil_int_check(repeat)+"&customwatermarkid="+CGI.escape(nil_check(customWaterMarkId))+"&reverse="+b_to_str(reverse)+"&country="+CGI.escape(nil_check(country))+"&quality="+nil_int_check(quality)+"&callback="
|
67
|
-
@@signaturePartOne = @applicationSecret+"|"+nil_check(url)+"|"
|
68
|
-
@@signaturePartTwo = "|"+nil_int_check(height)+"|"+nil_int_check(width)+"|"+nil_check(customId)+"|"+nil_float_check(framesPerSecond)+"|"+nil_float_check(speed)+"|"+nil_int_check(duration)+"|"+nil_int_check(repeat)+"|"+b_to_str(reverse)+"|"+nil_int_check(start)+"|"+nil_check(customWaterMarkId)+"|"+nil_check(country)+"|"+nil_int_check(quality)
|
69
|
-
return nil
|
70
|
-
end
|
71
|
-
|
72
|
-
|
73
|
-
# Sets the parameters required to take a screenshot of a web page.
|
74
|
-
#
|
75
|
-
# @param url [String] the URL that the screenshot should be made of
|
76
|
-
# @param customId [String, nil] custom identifier that you can pass through to the screenshot web service. This will be returned with the callback URL you have specified.
|
77
|
-
# @param browserWidth [Integer, nil] the width of the browser in pixels
|
78
|
-
# @param browserHeight [Integer, nil] the height of the browser in pixels
|
79
|
-
# @param width [Integer, nil] the width of the resulting thumbnail in pixels
|
80
|
-
# @param height [Integer, nil] the height of the resulting thumbnail in pixels
|
81
|
-
# @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
82
|
-
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
83
|
-
# @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
|
84
|
-
# @param requestAs [Integer, 0] request the screenshot in different forms: Standard Browser = 0, Mobile Browser = 1, Search Engine = 2 and Fallback Browser = 3
|
85
|
-
# @param customWaterMarkId [String, nil] add a custom watermark to the image
|
86
|
-
# @param quality [Integer, -1] the quality of the image where 0 is poor and 100 excellent
|
87
|
-
# @param country [String, nil] request the screenshot from different countries: Default = "", UK = "UK", US = "US"
|
88
|
-
# @return [void]
|
89
|
-
def set_image_options(url, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil, requestAs = 0, customWaterMarkId = nil, quality = -1, country = nil)
|
90
|
-
|
91
|
-
if delay == nil
|
92
|
-
@@startDelay = 0
|
93
|
-
else
|
94
|
-
@@startDelay = delay
|
95
|
-
end
|
96
|
-
|
97
|
-
@@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="+nil_int_check(requestAs)+"&country="+CGI.escape(nil_check(country))+"&quality="+nil_int_check(quality)+"&callback="
|
98
|
-
@@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
|
99
|
-
@@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)+"|"+nil_int_check(requestAs)+"|"+nil_check(country)+"|"+nil_int_check(quality)
|
100
|
-
|
101
|
-
return nil
|
102
|
-
end
|
103
|
-
|
104
|
-
# Sets the parameters required to extract one or more tables from a web page.
|
105
|
-
#
|
106
|
-
# @param url [String] the URL that the should be used to extract tables
|
107
|
-
# @param customId [String, nil] a custom identifier that you can pass through to the web service. This will be returned with the callback URL you have specified.
|
108
|
-
# @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
|
109
|
-
# @param format [String, 'csv'] the format the table should be in: csv, xlsx
|
110
|
-
# @param includeHeaderNames [Boolean, true] if true header names will be included in the table
|
111
|
-
# @param includeAllTables [Boolean, true] if true all table on the web page will be extracted with each table appearing in a separate spreadsheet sheet. Only available with the XLSX format.
|
112
|
-
# @param targetElement [String, nil] the id of the only HTML element in the web page that should be used to extract tables from
|
113
|
-
# @param requestAs [Integer, 0] request the screenshot in different forms: Standard Browser = 0, Mobile Browser = 1, Search Engine = 2 and Fallback Browser = 3
|
114
|
-
# @param country [String, nil] request the screenshot from different countries: Default = "", UK = "UK", US = "US"
|
115
|
-
# @return [void]
|
116
|
-
def set_table_options(url, customId = nil, tableNumberToInclude = 1, format = 'csv', includeHeaderNames = true, includeAllTables = false, targetElement = nil, requestAs = 0, country = nil)
|
117
|
-
@@startDelay = 0
|
118
|
-
|
119
|
-
@@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="+nil_int_check(requestAs)+"&country="+CGI.escape(nil_check(country))+"&callback="
|
120
|
-
@@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
|
121
|
-
@@signaturePartTwo = "|"+nil_check(customId)+"|"+nil_int_check(tableNumberToInclude)+"|"+b_to_str(includeAllTables)+"|"+b_to_str(includeHeaderNames)+"|"+nil_check(targetElement)+"|"+nil_check(format)+"|"+nil_int_check(requestAs)+"|"+nil_check(country)
|
122
|
-
|
123
|
-
return nil
|
124
|
-
end
|
125
|
-
|
126
|
-
# Sets the parameters required to convert a web page into a PDF.
|
127
|
-
#
|
128
|
-
# @param url url [String] the URL that the should be converted into a pdf
|
129
|
-
# @param customId [String, nil] a custom identifier that you can pass through to the web service. This will be returned with the callback URL you have specified.
|
130
|
-
# @param includeBackground [Boolean, true] if true the background of the web page should be included in the screenshot
|
131
|
-
# @param pagesize [String, 'A4'] the page size of the PDF to be returned: 'A3', 'A4', 'A5', 'B3', 'B4', 'B5', 'Letter'.
|
132
|
-
# @param orientation [String, 'Portrait'] the orientation of the PDF to be returned: 'Landscape' or 'Portrait'
|
133
|
-
# @param includeLinks [Boolean, true] true if links should be included in the PDF
|
134
|
-
# @param includeOutline [Boolean, false] True if the PDF outline should be included
|
135
|
-
# @param title [String, nil] provide a title to the PDF document
|
136
|
-
# @param coverURL [String, nil] the URL of a web page that should be used as a cover page for the PDF
|
137
|
-
# @param marginTop [Integer, 10] the margin that should appear at the top of the PDF document page
|
138
|
-
# @param marginLeft [Integer, 10] the margin that should appear at the left of the PDF document page
|
139
|
-
# @param marginBottom [Integer, 10] the margin that should appear at the bottom of the PDF document page
|
140
|
-
# @param marginRight [Integer, 10] the margin that should appear at the right of the PDF document
|
141
|
-
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
142
|
-
# @param requestAs [Integer, 0] request the screenshot in different forms: Standard Browser = 0, Mobile Browser = 1, Search Engine = 2 and Fallback Browser = 3
|
143
|
-
# @param customWaterMarkId [String, nil] add a custom watermark to each page of the PDF document
|
144
|
-
# @param quality [Integer, -1] the quality of the PDF where 0 is poor and 100 excellent
|
145
|
-
# @param country [String, nil] request the screenshot from different countries: Default = "", UK = "UK", US = "US"
|
146
|
-
# @return [void]
|
147
|
-
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, requestAs = 0, customWaterMarkId = nil, quality = -1, country = nil)
|
148
|
-
pagesize = nil_check(pagesize).upcase
|
149
|
-
$orientation = nil_check(orientation).capitalize
|
150
|
-
|
151
|
-
if delay == nil
|
152
|
-
@@startDelay = 0
|
153
|
-
else
|
154
|
-
@@startDelay = delay
|
155
|
-
end
|
156
|
-
|
157
|
-
@@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="+nil_int_check(requestAs)+"&country="+CGI.escape(nil_check(country))+"&quality="+nil_int_check(quality)+"&callback="
|
158
|
-
|
159
|
-
@@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
|
160
|
-
@@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)+"|"+nil_int_check(requestAs)+"|"+nil_check(country)+"|"+nil_int_check(quality)
|
161
|
-
|
162
|
-
return nil
|
163
|
-
end
|
164
|
-
|
165
|
-
# Calls the GrabzIt web service to take the screenshot
|
166
|
-
#
|
167
|
-
# The handler will be passed a URL with the following query string parameters:
|
168
|
-
# - message (is any error message associated with the screenshot)
|
169
|
-
# - customId (is a custom id you may have specified in the {#set_image_options}, {#set_table_options} or {#set_pdf_options} method)
|
170
|
-
# - id (is the unique id of the screenshot which can be used to retrieve the screenshot with the {#get_result} method)
|
171
|
-
# - filename (is the filename of the screenshot)
|
172
|
-
# - format (is the format of the screenshot)
|
173
|
-
# @note This is the recommended method of saving a screenshot
|
174
|
-
# @param callBackURL [String, nil] the handler the GrabzIt web service should call after it has completed its work
|
175
|
-
# @return [String] the unique identifier of the screenshot. This can be used to get the screenshot with the {#get_result} method
|
176
|
-
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
177
|
-
def save(callBackURL = nil)
|
178
|
-
if @@signaturePartOne == nil && @@signaturePartTwo == nil && @@request == nil
|
179
|
-
raise GrabzItException.new("No screenshot parameters have been set.", GrabzItException::PARAMETER_MISSING_PARAMETERS)
|
180
|
-
end
|
181
|
-
|
182
|
-
sig = encode(@@signaturePartOne+nil_check(callBackURL)+@@signaturePartTwo)
|
183
|
-
currentRequest = @@request
|
184
|
-
|
185
|
-
currentRequest += CGI.escape(nil_check(callBackURL))+"&sig="+sig
|
186
|
-
|
187
|
-
return get_result_value(get(currentRequest), "ID")
|
188
|
-
end
|
189
|
-
|
190
|
-
# Calls the GrabzIt web service to take the screenshot and saves it to the target path provided. if no target path is provided
|
191
|
-
# it returns the screenshot byte data.
|
192
|
-
#
|
193
|
-
# @note Warning, this is a SYNCHONOUS method and can take up to 5 minutes before a response
|
194
|
-
# @param saveToFile [String, nil] the file path that the screenshot should saved to.
|
195
|
-
# @example Synchronously save the screenshot to test.jpg
|
196
|
-
# save_to('images/test.jpg')
|
197
|
-
# @raise [RuntimeError] if the screenshot cannot be saved a RuntimeError will be raised that will contain an explanation
|
198
|
-
# @return [Boolean] returns the true if it is successfully saved to a file, otherwise if a target path is not provided it returns the screenshot's byte data.
|
199
|
-
def save_to(saveToFile = nil)
|
200
|
-
id = save()
|
201
|
-
|
202
|
-
if id == nil || id == ""
|
203
|
-
return false
|
204
|
-
end
|
205
|
-
|
206
|
-
#Wait for it to be possibly ready
|
207
|
-
sleep((@@startDelay / 1000) + 3)
|
208
|
-
|
209
|
-
#Wait for it to be ready.
|
210
|
-
while true do
|
211
|
-
status = get_status(id)
|
212
|
-
|
213
|
-
if !status.cached && !status.processing
|
214
|
-
raise GrabzItException.new("The screenshot did not complete with the error: " + status.
|
215
|
-
break
|
216
|
-
elsif status.cached
|
217
|
-
result = get_result(id)
|
218
|
-
if !result
|
219
|
-
raise GrabzItException.new("The screenshot could not be found on GrabzIt.", GrabzItException::RENDERING_MISSING_SCREENSHOT)
|
220
|
-
break
|
221
|
-
end
|
222
|
-
|
223
|
-
if saveToFile == nil || saveToFile == ""
|
224
|
-
return result
|
225
|
-
end
|
226
|
-
|
227
|
-
screenshot = File.new(saveToFile, "wb")
|
228
|
-
screenshot.write(result)
|
229
|
-
screenshot.close
|
230
|
-
|
231
|
-
break
|
232
|
-
end
|
233
|
-
|
234
|
-
sleep(3)
|
235
|
-
end
|
236
|
-
|
237
|
-
return true
|
238
|
-
end
|
239
|
-
|
240
|
-
# Get the current status of a GrabzIt screenshot
|
241
|
-
#
|
242
|
-
# @param id [String] the id of the screenshot
|
243
|
-
# @return [ScreenShotStatus] a object representing the status of the screenshot
|
244
|
-
def get_status(id)
|
245
|
-
|
246
|
-
if id == nil || id == ""
|
247
|
-
return nil
|
248
|
-
end
|
249
|
-
|
250
|
-
result = get(WebServicesBaseURL + "getstatus.ashx?id=" + nil_check(id))
|
251
|
-
|
252
|
-
doc = REXML::Document.new(result)
|
253
|
-
|
254
|
-
processing = doc.root.elements["Processing"].text()
|
255
|
-
cached = doc.root.elements["Cached"].text()
|
256
|
-
expired = doc.root.elements["Expired"].text()
|
257
|
-
message = doc.root.elements["Message"].text()
|
258
|
-
|
259
|
-
return ScreenShotStatus.new((processing == TrueString), (cached == TrueString), (expired == TrueString), message)
|
260
|
-
end
|
261
|
-
|
262
|
-
# This method returns the screenshot itself. If nothing is returned then something has gone wrong or the screenshot is not ready yet
|
263
|
-
#
|
264
|
-
# @param id [String] the id of the screenshot
|
265
|
-
# @return [Object] returns the screenshot
|
266
|
-
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
267
|
-
def get_result(id)
|
268
|
-
if id == nil || id == ""
|
269
|
-
return nil
|
270
|
-
end
|
271
|
-
|
272
|
-
return get(WebServicesBaseURL + "getfile.ashx?id=" + nil_check(id))
|
273
|
-
end
|
274
|
-
|
275
|
-
# Get all the cookies that GrabzIt is using for a particular domain. This may include your user set cookies as well
|
276
|
-
#
|
277
|
-
# @param domain [String] the domain to return cookies for
|
278
|
-
# @return [Array<Cookie>] an array of cookies
|
279
|
-
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
280
|
-
def get_cookies(domain)
|
281
|
-
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(domain))
|
282
|
-
|
283
|
-
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&sig="+sig
|
284
|
-
|
285
|
-
result = get(WebServicesBaseURL + "getcookies.ashx?" + qs)
|
286
|
-
|
287
|
-
doc = REXML::Document.new(result)
|
288
|
-
|
289
|
-
check_for_exception(doc)
|
290
|
-
|
291
|
-
cookies = Array.new
|
292
|
-
|
293
|
-
xml_cookies = doc.elements.to_a("//WebResult/Cookies/Cookie")
|
294
|
-
xml_cookies.each do |cookie|
|
295
|
-
expires = nil
|
296
|
-
if cookie.elements["Expires"] != nil
|
297
|
-
expires = cookie.elements["Expires"].text
|
298
|
-
end
|
299
|
-
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)
|
300
|
-
cookies << grabzItCookie
|
301
|
-
end
|
302
|
-
|
303
|
-
return cookies
|
304
|
-
end
|
305
|
-
|
306
|
-
# Sets a new custom cookie on GrabzIt, if the custom cookie has the same name and domain as a global cookie the global
|
307
|
-
#
|
308
|
-
# cookie is overridden
|
309
|
-
# @note This can be useful if a websites functionality is controlled by cookies
|
310
|
-
# @param name [String] the name of the cookie to set
|
311
|
-
# @param domain [String] the domain of the website to set the cookie for
|
312
|
-
# @param value [String, ''] the value of the cookie
|
313
|
-
# @param path [String, '/'] the website path the cookie relates to
|
314
|
-
# @param httponly [Boolean, false] is the cookie only used on HTTP
|
315
|
-
# @param expires [String, ''] when the cookie expires. Pass a nil value if it does not expire
|
316
|
-
# @return [Boolean] returns true if the cookie was successfully set
|
317
|
-
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
318
|
-
def set_cookie(name, domain, value = "", path = "/", httponly = false, expires = "")
|
319
|
-
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|"+nil_check(value)+"|"+nil_check(path)+"|"+b_to_str(httponly)+"|"+nil_check(expires)+"|0")
|
320
|
-
|
321
|
-
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="+CGI.escape(nil_check(expires))+"&sig="+sig
|
322
|
-
|
323
|
-
return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString)
|
324
|
-
end
|
325
|
-
|
326
|
-
# Delete a custom cookie or block a global cookie from being used
|
327
|
-
#
|
328
|
-
# @param name [String] the name of the cookie to delete
|
329
|
-
# @param domain [String] the website the cookie belongs to
|
330
|
-
# @return [Boolean] returns true if the cookie was successfully set
|
331
|
-
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
332
|
-
def delete_cookie(name, domain)
|
333
|
-
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|1")
|
334
|
-
|
335
|
-
qs = "key=" + CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&name="+CGI.escape(nil_check(name))+"&delete=1&sig="+sig
|
336
|
-
|
337
|
-
return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString)
|
338
|
-
end
|
339
|
-
|
340
|
-
# Get your uploaded custom watermark
|
341
|
-
#
|
342
|
-
# @param identifier [String, nil] the identifier of a particular custom watermark you want to view
|
343
|
-
# @return [WaterMark] the watermark with the specified identifier
|
344
|
-
def get_watermark(identifier)
|
345
|
-
watermarks = get_watermarks(identifier)
|
346
|
-
if watermarks.length == 1
|
347
|
-
return watermarks[0];
|
348
|
-
end
|
349
|
-
|
350
|
-
return nil
|
351
|
-
end
|
352
|
-
|
353
|
-
# Get your uploaded custom watermarks
|
354
|
-
#
|
355
|
-
# @return [Array<WaterMark>] an array of uploaded watermarks
|
356
|
-
def get_watermarks()
|
357
|
-
return get_watermarks(nil)
|
358
|
-
end
|
359
|
-
|
360
|
-
# Add a new custom watermark
|
361
|
-
#
|
362
|
-
# @param identifier [String] the identifier you want to give the custom watermark. It is important that this identifier is unique.
|
363
|
-
# @param path [String] the absolute path of the watermark on your server. For instance C:/watermark/1.png
|
364
|
-
# @param xpos [Integer] the horizontal position you want the screenshot to appear at: Left = 0, Center = 1, Right = 2
|
365
|
-
# @param ypos [Integer] the vertical position you want the screenshot to appear at: Top = 0, Middle = 1, Bottom = 2
|
366
|
-
# @return [Boolean] returns true if the watermark was successfully set
|
367
|
-
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
368
|
-
def add_watermark(identifier, path, xpos, ypos)
|
369
|
-
if !File.file?(path)
|
370
|
-
raise "File: " + path + " does not exist"
|
371
|
-
end
|
372
|
-
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(identifier)+"|"+nil_int_check(xpos)+"|"+nil_int_check(ypos));
|
373
|
-
|
374
|
-
boundary = '--------------------------'+Time.now.to_f.to_s
|
375
|
-
|
376
|
-
url = "http://grabz.it/services/addwatermark.ashx"
|
377
|
-
uri = URI.parse(url)
|
378
|
-
|
379
|
-
file = File.open(path, "rb")
|
380
|
-
data = file.read
|
381
|
-
|
382
|
-
post_body = Array.new
|
383
|
-
post_body << "\r\n--"+boundary+"\r\n"
|
384
|
-
post_body << "Content-Disposition: form-data; name=\"watermark\"; filename=\""+File.basename(path)+"\"\r\nContent-Type: image/jpeg\r\n\r\n"
|
385
|
-
post_body << data
|
386
|
-
post_body << "\r\n--"+boundary+"\r\n"
|
387
|
-
|
388
|
-
post_body << "Content-Disposition: form-data; name=\"key\"\r\n\r\n"
|
389
|
-
post_body << CGI.escape(nil_check(@applicationKey))
|
390
|
-
post_body << "\r\n--"+boundary+"\r\n"
|
391
|
-
|
392
|
-
post_body << "Content-Disposition: form-data; name=\"identifier\"\r\n\r\n"
|
393
|
-
post_body << CGI.escape(nil_check(identifier))
|
394
|
-
post_body << "\r\n--"+boundary+"\r\n"
|
395
|
-
|
396
|
-
post_body << "Content-Disposition: form-data; name=\"xpos\"\r\n\r\n"
|
397
|
-
post_body << nil_check(xpos)
|
398
|
-
post_body << "\r\n--"+boundary+"\r\n"
|
399
|
-
|
400
|
-
post_body << "Content-Disposition: form-data; name=\"ypos\"\r\n\r\n"
|
401
|
-
post_body << nil_check(ypos)
|
402
|
-
post_body << "\r\n--"+boundary+"\r\n"
|
403
|
-
|
404
|
-
post_body << "Content-Disposition: form-data; name=\"sig\"\r\n\r\n"
|
405
|
-
post_body << sig
|
406
|
-
post_body << "\r\n--"+boundary+"--\r\n"
|
407
|
-
|
408
|
-
request = Net::HTTP::Post.new(url)
|
409
|
-
request.content_type = "multipart/form-data, boundary="+boundary
|
410
|
-
request.body = post_body.join
|
411
|
-
|
412
|
-
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(request) }
|
413
|
-
response_check(response)
|
414
|
-
|
415
|
-
return (get_result_value(response.body(), "Result") == TrueString)
|
416
|
-
end
|
417
|
-
|
418
|
-
# Delete a custom watermark
|
419
|
-
#
|
420
|
-
# @param identifier [String] the identifier of the custom watermark you want to delete
|
421
|
-
# @return [Boolean] returns true if the watermark was successfully deleted
|
422
|
-
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
423
|
-
def delete_watermark(identifier)
|
424
|
-
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(identifier))
|
425
|
-
|
426
|
-
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig
|
427
|
-
|
428
|
-
return (get_result_value(get(WebServicesBaseURL + "deletewatermark.ashx?" + qs), "Result") == TrueString)
|
429
|
-
end
|
430
|
-
|
431
|
-
# This method calls the GrabzIt web service to take the screenshot.
|
432
|
-
#
|
433
|
-
# @param url [String] the URL that the screenshot should be made of
|
434
|
-
# @param callback [String, nil] the handler the GrabzIt web service should call after it has completed its work
|
435
|
-
# @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.
|
436
|
-
# @param browserWidth [Integer, nil] the width of the browser in pixels
|
437
|
-
# @param browserHeight [Integer, nil] the height of the browser in pixels
|
438
|
-
# @param width [Integer, nil] the width of the resulting thumbnail in pixels
|
439
|
-
# @param height [Integer, nil] the height of the resulting thumbnail in pixels
|
440
|
-
# @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
441
|
-
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
442
|
-
# @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
|
443
|
-
# @return [String] returns the unique identifier of the screenshot. This can be used to get the screenshot with the get_result method
|
444
|
-
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
445
|
-
# @deprecated Use {#set_image_options} and {#save} instead.
|
446
|
-
def take_picture(url, callback = nil, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
|
447
|
-
set_image_options(url, customId, browserWidth, browserHeight, width, height, format, delay, targetElement)
|
448
|
-
return save(callback)
|
449
|
-
end
|
450
|
-
|
451
|
-
# This method takes the screenshot and then saves the result to a file. WARNING this method is synchronous
|
452
|
-
#
|
453
|
-
# @param url [String] the URL that the screenshot should be made of
|
454
|
-
# @param saveToFile [String] the file path that the screenshot should saved to
|
455
|
-
# @param browserWidth [Integer, nil] the width of the browser in pixels
|
456
|
-
# @param browserHeight [Integer, nil] the height of the browser in pixels
|
457
|
-
# @param width [Integer, nil] the width of the resulting thumbnail in pixels
|
458
|
-
# @param height [Integer, nil] the height of the resulting thumbnail in pixels
|
459
|
-
# @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
460
|
-
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
461
|
-
# @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
|
462
|
-
# @example Synchronously save the screenshot to test.jpg
|
463
|
-
# save_picture('images/test.jpg')
|
464
|
-
# @return [Boolean] returns the true if it is successfull otherwise it throws an exception
|
465
|
-
# @raise [RuntimeError] if the screenshot cannot be saved a RuntimeError will be raised that will contain an explanation
|
466
|
-
# @deprecated Use {#set_image_options} and {#save_to} instead.
|
467
|
-
def save_picture(url, saveToFile, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
|
468
|
-
set_image_options(url, nil, browserWidth, browserHeight, width, height, format, delay, targetElement)
|
469
|
-
return save_to(saveToFile)
|
470
|
-
end
|
471
|
-
|
472
|
-
# This method returns the image itself. If nothing is returned then something has gone wrong or the image is not ready yet.
|
473
|
-
#
|
474
|
-
# @param id [String] the id of the screenshot
|
475
|
-
# @return [Object] returns the screenshot
|
476
|
-
# @deprecated Use {#get_result} instead.
|
477
|
-
def get_picture(id)
|
478
|
-
return get_result(id)
|
479
|
-
end
|
480
|
-
|
481
|
-
private
|
482
|
-
def get_watermarks(identifier = nil)
|
483
|
-
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(identifier))
|
484
|
-
|
485
|
-
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig
|
486
|
-
|
487
|
-
result = get(WebServicesBaseURL + "getwatermarks.ashx?" + qs)
|
488
|
-
|
489
|
-
doc = REXML::Document.new(result)
|
490
|
-
|
491
|
-
check_for_exception(doc)
|
492
|
-
|
493
|
-
watermarks = Array.new
|
494
|
-
|
495
|
-
xml_watemarks = doc.elements.to_a("//WebResult/WaterMarks/WaterMark")
|
496
|
-
xml_watemarks.each do |watemark|
|
497
|
-
grabzItWaterMark = GrabzIt::WaterMark.new(watemark.elements["Identifier"].text, watemark.elements["XPosition"].text.to_i, watemark.elements["YPosition"].text.to_i, watemark.elements["Format"].text)
|
498
|
-
watermarks << grabzItWaterMark
|
499
|
-
end
|
500
|
-
|
501
|
-
return watermarks
|
502
|
-
end
|
503
|
-
|
504
|
-
private
|
505
|
-
def get(url)
|
506
|
-
uri = URI.parse(url)
|
507
|
-
response = Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.request_uri) }
|
508
|
-
response_check(response)
|
509
|
-
return response.body
|
510
|
-
end
|
511
|
-
|
512
|
-
private
|
513
|
-
def response_check(response)
|
514
|
-
statusCode = response.code.to_i
|
515
|
-
if statusCode == 403
|
516
|
-
raise GrabzItException.new(response.body, GrabzItException::NETWORK_DDOS_ATTACK)
|
517
|
-
elsif statusCode >= 400
|
518
|
-
raise GrabzItException.new("A network error occured when connecting to the GrabzIt servers.", GrabzItException::NETWORK_GENERAL_ERROR)
|
519
|
-
end
|
520
|
-
end
|
521
|
-
|
522
|
-
private
|
523
|
-
def b_to_str(bValue)
|
524
|
-
if bValue
|
525
|
-
return 1.to_s
|
526
|
-
end
|
527
|
-
return 0.to_s
|
528
|
-
end
|
529
|
-
|
530
|
-
private
|
531
|
-
def nil_check(param)
|
532
|
-
if param == nil
|
533
|
-
return ""
|
534
|
-
end
|
535
|
-
return param
|
536
|
-
end
|
537
|
-
|
538
|
-
private
|
539
|
-
def nil_int_check(param)
|
540
|
-
return param.to_i.to_s
|
541
|
-
end
|
542
|
-
|
543
|
-
private
|
544
|
-
def nil_float_check(param)
|
545
|
-
val = param.to_f
|
546
|
-
if ((val % 1) == 0)
|
547
|
-
return val.to_i.to_s
|
548
|
-
end
|
549
|
-
|
550
|
-
return val.to_s
|
551
|
-
end
|
552
|
-
|
553
|
-
private
|
554
|
-
def check_for_exception(doc)
|
555
|
-
if (doc == nil)
|
556
|
-
return
|
557
|
-
end
|
558
|
-
|
559
|
-
message = doc.root.elements["Message"].text()
|
560
|
-
code = doc.root.elements["Code"].text()
|
561
|
-
|
562
|
-
if message != nil
|
563
|
-
raise GrabzItException.new(message, code)
|
564
|
-
end
|
565
|
-
end
|
566
|
-
|
567
|
-
private
|
568
|
-
def encode(text)
|
569
|
-
return Digest::MD5.hexdigest(text.encode('ascii', {:invalid => :replace, :undef => :replace, :replace => '?'}))
|
570
|
-
end
|
571
|
-
|
572
|
-
private
|
573
|
-
def get_result_value(result, field)
|
574
|
-
doc = REXML::Document.new(result)
|
575
|
-
|
576
|
-
value = doc.root.elements[field].text()
|
577
|
-
|
578
|
-
check_for_exception(doc)
|
579
|
-
|
580
|
-
return value
|
581
|
-
end
|
582
|
-
end
|
583
|
-
end
|
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
|
+
require File.join(File.dirname(__FILE__), 'exception')
|
14
|
+
|
15
|
+
# This client provides access to the GrabzIt web services
|
16
|
+
# This API allows you to take screenshot of websites for free and convert them into images, PDF's and tables.
|
17
|
+
# @example Example usage
|
18
|
+
# require 'grabzit'
|
19
|
+
#
|
20
|
+
# grabzItClient = GrabzIt::Client.new("YOUR APPLICATION KEY", "YOUR APPLICATION SECRET")
|
21
|
+
# grabzItClient.set_image_options("http://www.google.com")
|
22
|
+
# grabzItClient.save("http://www.mysite.com/grabzit/handler")
|
23
|
+
# @version 2.1
|
24
|
+
# @author GrabzIt
|
25
|
+
# @see http://grabz.it/api/ruby/ GrabzIt Ruby API
|
26
|
+
class Client
|
27
|
+
|
28
|
+
WebServicesBaseURL = "http://api.grabz.it/services/"
|
29
|
+
private_constant :WebServicesBaseURL
|
30
|
+
TrueString = "True"
|
31
|
+
private_constant :TrueString
|
32
|
+
|
33
|
+
@@signaturePartOne;
|
34
|
+
@@signaturePartTwo;
|
35
|
+
@@startDelay;
|
36
|
+
@@request;
|
37
|
+
|
38
|
+
# Create a new instance of the Client class in order to access the GrabzIt API.
|
39
|
+
#
|
40
|
+
# @param applicationKey [String] your application key
|
41
|
+
# @param applicationSecret [String] your application secret
|
42
|
+
# @see http://grabz.it/register.aspx You can get an application key and secret by registering for free with GrabzIt
|
43
|
+
def initialize(applicationKey, applicationSecret)
|
44
|
+
@applicationKey = applicationKey
|
45
|
+
@applicationSecret = applicationSecret
|
46
|
+
end
|
47
|
+
|
48
|
+
# This method sets the parameters required to turn a online video into a animated GIF
|
49
|
+
#
|
50
|
+
# @param url [String] the URL of the online video
|
51
|
+
# @param customId [String, nil] custom identifier that you can pass through to the animated GIF web service. This will be returned with the callback URL you have specified
|
52
|
+
# @param width [Integer, 0] the width of the resulting animated GIF in pixels
|
53
|
+
# @param height [Integer, 0] the height of the resulting animated GIF in pixels
|
54
|
+
# @param start [Integer, 0] the starting position of the video that should be converted into a animated GIF
|
55
|
+
# @param duration [Integer, 0] the length in seconds of the video that should be converted into a animated GIF
|
56
|
+
# @param speed [Float, 0] the speed of the animated GIF from 0.2 to 10 times the original speed
|
57
|
+
# @param framesPerSecond [Float, 0] the number of frames per second that should be captured from the video. From a minimum of 0.2 to a maximum of 60
|
58
|
+
# @param repeat [Integer, 0] the number of times to loop the animated GIF. If 0 it will loop forever
|
59
|
+
# @param reverse [Boolean, false] if true the frames of the animated GIF are reversed
|
60
|
+
# @param customWaterMarkId [String, nil] add a custom watermark to the animated GIF
|
61
|
+
# @param quality [Integer, -1] the quality of the image where 0 is poor and 100 excellent. The default is -1 which uses the recommended quality
|
62
|
+
# @param country [String, nil] request the screenshot from different countries: Default, UK or US
|
63
|
+
# @return [void]
|
64
|
+
def set_animation_options(url, customId = nil, width = 0, height = 0, start = 0, duration = 0, speed = 0, framesPerSecond = 0, repeat = 0, reverse = false, customWaterMarkId = nil, quality = -1, country = nil)
|
65
|
+
@@startDelay = 0;
|
66
|
+
@@request = WebServicesBaseURL + "takeanimation.ashx?key=" + CGI.escape(nil_check(@applicationKey))+"&url="+CGI.escape(nil_check(url))+"&width="+nil_int_check(width)+"&height="+nil_int_check(height)+"&duration="+nil_int_check(duration)+"&speed="+nil_float_check(speed)+"&start="+nil_int_check(start)+"&customid="+CGI.escape(nil_check(customId))+"&fps="+nil_float_check(framesPerSecond)+"&repeat="+nil_int_check(repeat)+"&customwatermarkid="+CGI.escape(nil_check(customWaterMarkId))+"&reverse="+b_to_str(reverse)+"&country="+CGI.escape(nil_check(country))+"&quality="+nil_int_check(quality)+"&callback="
|
67
|
+
@@signaturePartOne = @applicationSecret+"|"+nil_check(url)+"|"
|
68
|
+
@@signaturePartTwo = "|"+nil_int_check(height)+"|"+nil_int_check(width)+"|"+nil_check(customId)+"|"+nil_float_check(framesPerSecond)+"|"+nil_float_check(speed)+"|"+nil_int_check(duration)+"|"+nil_int_check(repeat)+"|"+b_to_str(reverse)+"|"+nil_int_check(start)+"|"+nil_check(customWaterMarkId)+"|"+nil_check(country)+"|"+nil_int_check(quality)
|
69
|
+
return nil
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
# Sets the parameters required to take a screenshot of a web page.
|
74
|
+
#
|
75
|
+
# @param url [String] the URL that the screenshot should be made of
|
76
|
+
# @param customId [String, nil] custom identifier that you can pass through to the screenshot web service. This will be returned with the callback URL you have specified.
|
77
|
+
# @param browserWidth [Integer, nil] the width of the browser in pixels
|
78
|
+
# @param browserHeight [Integer, nil] the height of the browser in pixels
|
79
|
+
# @param width [Integer, nil] the width of the resulting thumbnail in pixels
|
80
|
+
# @param height [Integer, nil] the height of the resulting thumbnail in pixels
|
81
|
+
# @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
82
|
+
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
83
|
+
# @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
|
84
|
+
# @param requestAs [Integer, 0] request the screenshot in different forms: Standard Browser = 0, Mobile Browser = 1, Search Engine = 2 and Fallback Browser = 3
|
85
|
+
# @param customWaterMarkId [String, nil] add a custom watermark to the image
|
86
|
+
# @param quality [Integer, -1] the quality of the image where 0 is poor and 100 excellent
|
87
|
+
# @param country [String, nil] request the screenshot from different countries: Default = "", UK = "UK", US = "US"
|
88
|
+
# @return [void]
|
89
|
+
def set_image_options(url, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil, requestAs = 0, customWaterMarkId = nil, quality = -1, country = nil)
|
90
|
+
|
91
|
+
if delay == nil
|
92
|
+
@@startDelay = 0
|
93
|
+
else
|
94
|
+
@@startDelay = delay
|
95
|
+
end
|
96
|
+
|
97
|
+
@@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="+nil_int_check(requestAs)+"&country="+CGI.escape(nil_check(country))+"&quality="+nil_int_check(quality)+"&callback="
|
98
|
+
@@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
|
99
|
+
@@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)+"|"+nil_int_check(requestAs)+"|"+nil_check(country)+"|"+nil_int_check(quality)
|
100
|
+
|
101
|
+
return nil
|
102
|
+
end
|
103
|
+
|
104
|
+
# Sets the parameters required to extract one or more tables from a web page.
|
105
|
+
#
|
106
|
+
# @param url [String] the URL that the should be used to extract tables
|
107
|
+
# @param customId [String, nil] a custom identifier that you can pass through to the web service. This will be returned with the callback URL you have specified.
|
108
|
+
# @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
|
109
|
+
# @param format [String, 'csv'] the format the table should be in: csv, xlsx
|
110
|
+
# @param includeHeaderNames [Boolean, true] if true header names will be included in the table
|
111
|
+
# @param includeAllTables [Boolean, true] if true all table on the web page will be extracted with each table appearing in a separate spreadsheet sheet. Only available with the XLSX format.
|
112
|
+
# @param targetElement [String, nil] the id of the only HTML element in the web page that should be used to extract tables from
|
113
|
+
# @param requestAs [Integer, 0] request the screenshot in different forms: Standard Browser = 0, Mobile Browser = 1, Search Engine = 2 and Fallback Browser = 3
|
114
|
+
# @param country [String, nil] request the screenshot from different countries: Default = "", UK = "UK", US = "US"
|
115
|
+
# @return [void]
|
116
|
+
def set_table_options(url, customId = nil, tableNumberToInclude = 1, format = 'csv', includeHeaderNames = true, includeAllTables = false, targetElement = nil, requestAs = 0, country = nil)
|
117
|
+
@@startDelay = 0
|
118
|
+
|
119
|
+
@@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="+nil_int_check(requestAs)+"&country="+CGI.escape(nil_check(country))+"&callback="
|
120
|
+
@@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
|
121
|
+
@@signaturePartTwo = "|"+nil_check(customId)+"|"+nil_int_check(tableNumberToInclude)+"|"+b_to_str(includeAllTables)+"|"+b_to_str(includeHeaderNames)+"|"+nil_check(targetElement)+"|"+nil_check(format)+"|"+nil_int_check(requestAs)+"|"+nil_check(country)
|
122
|
+
|
123
|
+
return nil
|
124
|
+
end
|
125
|
+
|
126
|
+
# Sets the parameters required to convert a web page into a PDF.
|
127
|
+
#
|
128
|
+
# @param url url [String] the URL that the should be converted into a pdf
|
129
|
+
# @param customId [String, nil] a custom identifier that you can pass through to the web service. This will be returned with the callback URL you have specified.
|
130
|
+
# @param includeBackground [Boolean, true] if true the background of the web page should be included in the screenshot
|
131
|
+
# @param pagesize [String, 'A4'] the page size of the PDF to be returned: 'A3', 'A4', 'A5', 'B3', 'B4', 'B5', 'Letter'.
|
132
|
+
# @param orientation [String, 'Portrait'] the orientation of the PDF to be returned: 'Landscape' or 'Portrait'
|
133
|
+
# @param includeLinks [Boolean, true] true if links should be included in the PDF
|
134
|
+
# @param includeOutline [Boolean, false] True if the PDF outline should be included
|
135
|
+
# @param title [String, nil] provide a title to the PDF document
|
136
|
+
# @param coverURL [String, nil] the URL of a web page that should be used as a cover page for the PDF
|
137
|
+
# @param marginTop [Integer, 10] the margin that should appear at the top of the PDF document page
|
138
|
+
# @param marginLeft [Integer, 10] the margin that should appear at the left of the PDF document page
|
139
|
+
# @param marginBottom [Integer, 10] the margin that should appear at the bottom of the PDF document page
|
140
|
+
# @param marginRight [Integer, 10] the margin that should appear at the right of the PDF document
|
141
|
+
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
142
|
+
# @param requestAs [Integer, 0] request the screenshot in different forms: Standard Browser = 0, Mobile Browser = 1, Search Engine = 2 and Fallback Browser = 3
|
143
|
+
# @param customWaterMarkId [String, nil] add a custom watermark to each page of the PDF document
|
144
|
+
# @param quality [Integer, -1] the quality of the PDF where 0 is poor and 100 excellent
|
145
|
+
# @param country [String, nil] request the screenshot from different countries: Default = "", UK = "UK", US = "US"
|
146
|
+
# @return [void]
|
147
|
+
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, requestAs = 0, customWaterMarkId = nil, quality = -1, country = nil)
|
148
|
+
pagesize = nil_check(pagesize).upcase
|
149
|
+
$orientation = nil_check(orientation).capitalize
|
150
|
+
|
151
|
+
if delay == nil
|
152
|
+
@@startDelay = 0
|
153
|
+
else
|
154
|
+
@@startDelay = delay
|
155
|
+
end
|
156
|
+
|
157
|
+
@@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="+nil_int_check(requestAs)+"&country="+CGI.escape(nil_check(country))+"&quality="+nil_int_check(quality)+"&callback="
|
158
|
+
|
159
|
+
@@signaturePartOne = nil_check(@applicationSecret)+"|"+nil_check(url)+"|"
|
160
|
+
@@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)+"|"+nil_int_check(requestAs)+"|"+nil_check(country)+"|"+nil_int_check(quality)
|
161
|
+
|
162
|
+
return nil
|
163
|
+
end
|
164
|
+
|
165
|
+
# Calls the GrabzIt web service to take the screenshot
|
166
|
+
#
|
167
|
+
# The handler will be passed a URL with the following query string parameters:
|
168
|
+
# - message (is any error message associated with the screenshot)
|
169
|
+
# - customId (is a custom id you may have specified in the {#set_image_options}, {#set_table_options} or {#set_pdf_options} method)
|
170
|
+
# - id (is the unique id of the screenshot which can be used to retrieve the screenshot with the {#get_result} method)
|
171
|
+
# - filename (is the filename of the screenshot)
|
172
|
+
# - format (is the format of the screenshot)
|
173
|
+
# @note This is the recommended method of saving a screenshot
|
174
|
+
# @param callBackURL [String, nil] the handler the GrabzIt web service should call after it has completed its work
|
175
|
+
# @return [String] the unique identifier of the screenshot. This can be used to get the screenshot with the {#get_result} method
|
176
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
177
|
+
def save(callBackURL = nil)
|
178
|
+
if @@signaturePartOne == nil && @@signaturePartTwo == nil && @@request == nil
|
179
|
+
raise GrabzItException.new("No screenshot parameters have been set.", GrabzItException::PARAMETER_MISSING_PARAMETERS)
|
180
|
+
end
|
181
|
+
|
182
|
+
sig = encode(@@signaturePartOne+nil_check(callBackURL)+@@signaturePartTwo)
|
183
|
+
currentRequest = @@request
|
184
|
+
|
185
|
+
currentRequest += CGI.escape(nil_check(callBackURL))+"&sig="+sig
|
186
|
+
|
187
|
+
return get_result_value(get(currentRequest), "ID")
|
188
|
+
end
|
189
|
+
|
190
|
+
# Calls the GrabzIt web service to take the screenshot and saves it to the target path provided. if no target path is provided
|
191
|
+
# it returns the screenshot byte data.
|
192
|
+
#
|
193
|
+
# @note Warning, this is a SYNCHONOUS method and can take up to 5 minutes before a response
|
194
|
+
# @param saveToFile [String, nil] the file path that the screenshot should saved to.
|
195
|
+
# @example Synchronously save the screenshot to test.jpg
|
196
|
+
# save_to('images/test.jpg')
|
197
|
+
# @raise [RuntimeError] if the screenshot cannot be saved a RuntimeError will be raised that will contain an explanation
|
198
|
+
# @return [Boolean] returns the true if it is successfully saved to a file, otherwise if a target path is not provided it returns the screenshot's byte data.
|
199
|
+
def save_to(saveToFile = nil)
|
200
|
+
id = save()
|
201
|
+
|
202
|
+
if id == nil || id == ""
|
203
|
+
return false
|
204
|
+
end
|
205
|
+
|
206
|
+
#Wait for it to be possibly ready
|
207
|
+
sleep((@@startDelay / 1000) + 3)
|
208
|
+
|
209
|
+
#Wait for it to be ready.
|
210
|
+
while true do
|
211
|
+
status = get_status(id)
|
212
|
+
|
213
|
+
if !status.cached && !status.processing
|
214
|
+
raise GrabzItException.new("The screenshot did not complete with the error: " + status.message, GrabzItException::RENDERING_ERROR)
|
215
|
+
break
|
216
|
+
elsif status.cached
|
217
|
+
result = get_result(id)
|
218
|
+
if !result
|
219
|
+
raise GrabzItException.new("The screenshot could not be found on GrabzIt.", GrabzItException::RENDERING_MISSING_SCREENSHOT)
|
220
|
+
break
|
221
|
+
end
|
222
|
+
|
223
|
+
if saveToFile == nil || saveToFile == ""
|
224
|
+
return result
|
225
|
+
end
|
226
|
+
|
227
|
+
screenshot = File.new(saveToFile, "wb")
|
228
|
+
screenshot.write(result)
|
229
|
+
screenshot.close
|
230
|
+
|
231
|
+
break
|
232
|
+
end
|
233
|
+
|
234
|
+
sleep(3)
|
235
|
+
end
|
236
|
+
|
237
|
+
return true
|
238
|
+
end
|
239
|
+
|
240
|
+
# Get the current status of a GrabzIt screenshot
|
241
|
+
#
|
242
|
+
# @param id [String] the id of the screenshot
|
243
|
+
# @return [ScreenShotStatus] a object representing the status of the screenshot
|
244
|
+
def get_status(id)
|
245
|
+
|
246
|
+
if id == nil || id == ""
|
247
|
+
return nil
|
248
|
+
end
|
249
|
+
|
250
|
+
result = get(WebServicesBaseURL + "getstatus.ashx?id=" + nil_check(id))
|
251
|
+
|
252
|
+
doc = REXML::Document.new(result)
|
253
|
+
|
254
|
+
processing = doc.root.elements["Processing"].text()
|
255
|
+
cached = doc.root.elements["Cached"].text()
|
256
|
+
expired = doc.root.elements["Expired"].text()
|
257
|
+
message = doc.root.elements["Message"].text()
|
258
|
+
|
259
|
+
return ScreenShotStatus.new((processing == TrueString), (cached == TrueString), (expired == TrueString), message)
|
260
|
+
end
|
261
|
+
|
262
|
+
# This method returns the screenshot itself. If nothing is returned then something has gone wrong or the screenshot is not ready yet
|
263
|
+
#
|
264
|
+
# @param id [String] the id of the screenshot
|
265
|
+
# @return [Object] returns the screenshot
|
266
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
267
|
+
def get_result(id)
|
268
|
+
if id == nil || id == ""
|
269
|
+
return nil
|
270
|
+
end
|
271
|
+
|
272
|
+
return get(WebServicesBaseURL + "getfile.ashx?id=" + nil_check(id))
|
273
|
+
end
|
274
|
+
|
275
|
+
# Get all the cookies that GrabzIt is using for a particular domain. This may include your user set cookies as well
|
276
|
+
#
|
277
|
+
# @param domain [String] the domain to return cookies for
|
278
|
+
# @return [Array<Cookie>] an array of cookies
|
279
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
280
|
+
def get_cookies(domain)
|
281
|
+
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(domain))
|
282
|
+
|
283
|
+
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&sig="+sig
|
284
|
+
|
285
|
+
result = get(WebServicesBaseURL + "getcookies.ashx?" + qs)
|
286
|
+
|
287
|
+
doc = REXML::Document.new(result)
|
288
|
+
|
289
|
+
check_for_exception(doc)
|
290
|
+
|
291
|
+
cookies = Array.new
|
292
|
+
|
293
|
+
xml_cookies = doc.elements.to_a("//WebResult/Cookies/Cookie")
|
294
|
+
xml_cookies.each do |cookie|
|
295
|
+
expires = nil
|
296
|
+
if cookie.elements["Expires"] != nil
|
297
|
+
expires = cookie.elements["Expires"].text
|
298
|
+
end
|
299
|
+
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)
|
300
|
+
cookies << grabzItCookie
|
301
|
+
end
|
302
|
+
|
303
|
+
return cookies
|
304
|
+
end
|
305
|
+
|
306
|
+
# Sets a new custom cookie on GrabzIt, if the custom cookie has the same name and domain as a global cookie the global
|
307
|
+
#
|
308
|
+
# cookie is overridden
|
309
|
+
# @note This can be useful if a websites functionality is controlled by cookies
|
310
|
+
# @param name [String] the name of the cookie to set
|
311
|
+
# @param domain [String] the domain of the website to set the cookie for
|
312
|
+
# @param value [String, ''] the value of the cookie
|
313
|
+
# @param path [String, '/'] the website path the cookie relates to
|
314
|
+
# @param httponly [Boolean, false] is the cookie only used on HTTP
|
315
|
+
# @param expires [String, ''] when the cookie expires. Pass a nil value if it does not expire
|
316
|
+
# @return [Boolean] returns true if the cookie was successfully set
|
317
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
318
|
+
def set_cookie(name, domain, value = "", path = "/", httponly = false, expires = "")
|
319
|
+
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|"+nil_check(value)+"|"+nil_check(path)+"|"+b_to_str(httponly)+"|"+nil_check(expires)+"|0")
|
320
|
+
|
321
|
+
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="+CGI.escape(nil_check(expires))+"&sig="+sig
|
322
|
+
|
323
|
+
return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString)
|
324
|
+
end
|
325
|
+
|
326
|
+
# Delete a custom cookie or block a global cookie from being used
|
327
|
+
#
|
328
|
+
# @param name [String] the name of the cookie to delete
|
329
|
+
# @param domain [String] the website the cookie belongs to
|
330
|
+
# @return [Boolean] returns true if the cookie was successfully set
|
331
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
332
|
+
def delete_cookie(name, domain)
|
333
|
+
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(name)+"|"+nil_check(domain)+"|1")
|
334
|
+
|
335
|
+
qs = "key=" + CGI.escape(nil_check(@applicationKey))+"&domain="+CGI.escape(nil_check(domain))+"&name="+CGI.escape(nil_check(name))+"&delete=1&sig="+sig
|
336
|
+
|
337
|
+
return (get_result_value(get(WebServicesBaseURL + "setcookie.ashx?" + qs), "Result") == TrueString)
|
338
|
+
end
|
339
|
+
|
340
|
+
# Get your uploaded custom watermark
|
341
|
+
#
|
342
|
+
# @param identifier [String, nil] the identifier of a particular custom watermark you want to view
|
343
|
+
# @return [WaterMark] the watermark with the specified identifier
|
344
|
+
def get_watermark(identifier)
|
345
|
+
watermarks = get_watermarks(identifier)
|
346
|
+
if watermarks.length == 1
|
347
|
+
return watermarks[0];
|
348
|
+
end
|
349
|
+
|
350
|
+
return nil
|
351
|
+
end
|
352
|
+
|
353
|
+
# Get your uploaded custom watermarks
|
354
|
+
#
|
355
|
+
# @return [Array<WaterMark>] an array of uploaded watermarks
|
356
|
+
def get_watermarks()
|
357
|
+
return get_watermarks(nil)
|
358
|
+
end
|
359
|
+
|
360
|
+
# Add a new custom watermark
|
361
|
+
#
|
362
|
+
# @param identifier [String] the identifier you want to give the custom watermark. It is important that this identifier is unique.
|
363
|
+
# @param path [String] the absolute path of the watermark on your server. For instance C:/watermark/1.png
|
364
|
+
# @param xpos [Integer] the horizontal position you want the screenshot to appear at: Left = 0, Center = 1, Right = 2
|
365
|
+
# @param ypos [Integer] the vertical position you want the screenshot to appear at: Top = 0, Middle = 1, Bottom = 2
|
366
|
+
# @return [Boolean] returns true if the watermark was successfully set
|
367
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
368
|
+
def add_watermark(identifier, path, xpos, ypos)
|
369
|
+
if !File.file?(path)
|
370
|
+
raise "File: " + path + " does not exist"
|
371
|
+
end
|
372
|
+
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(identifier)+"|"+nil_int_check(xpos)+"|"+nil_int_check(ypos));
|
373
|
+
|
374
|
+
boundary = '--------------------------'+Time.now.to_f.to_s
|
375
|
+
|
376
|
+
url = "http://grabz.it/services/addwatermark.ashx"
|
377
|
+
uri = URI.parse(url)
|
378
|
+
|
379
|
+
file = File.open(path, "rb")
|
380
|
+
data = file.read
|
381
|
+
|
382
|
+
post_body = Array.new
|
383
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
384
|
+
post_body << "Content-Disposition: form-data; name=\"watermark\"; filename=\""+File.basename(path)+"\"\r\nContent-Type: image/jpeg\r\n\r\n"
|
385
|
+
post_body << data
|
386
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
387
|
+
|
388
|
+
post_body << "Content-Disposition: form-data; name=\"key\"\r\n\r\n"
|
389
|
+
post_body << CGI.escape(nil_check(@applicationKey))
|
390
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
391
|
+
|
392
|
+
post_body << "Content-Disposition: form-data; name=\"identifier\"\r\n\r\n"
|
393
|
+
post_body << CGI.escape(nil_check(identifier))
|
394
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
395
|
+
|
396
|
+
post_body << "Content-Disposition: form-data; name=\"xpos\"\r\n\r\n"
|
397
|
+
post_body << nil_check(xpos)
|
398
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
399
|
+
|
400
|
+
post_body << "Content-Disposition: form-data; name=\"ypos\"\r\n\r\n"
|
401
|
+
post_body << nil_check(ypos)
|
402
|
+
post_body << "\r\n--"+boundary+"\r\n"
|
403
|
+
|
404
|
+
post_body << "Content-Disposition: form-data; name=\"sig\"\r\n\r\n"
|
405
|
+
post_body << sig
|
406
|
+
post_body << "\r\n--"+boundary+"--\r\n"
|
407
|
+
|
408
|
+
request = Net::HTTP::Post.new(url)
|
409
|
+
request.content_type = "multipart/form-data, boundary="+boundary
|
410
|
+
request.body = post_body.join
|
411
|
+
|
412
|
+
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(request) }
|
413
|
+
response_check(response)
|
414
|
+
|
415
|
+
return (get_result_value(response.body(), "Result") == TrueString)
|
416
|
+
end
|
417
|
+
|
418
|
+
# Delete a custom watermark
|
419
|
+
#
|
420
|
+
# @param identifier [String] the identifier of the custom watermark you want to delete
|
421
|
+
# @return [Boolean] returns true if the watermark was successfully deleted
|
422
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
423
|
+
def delete_watermark(identifier)
|
424
|
+
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(identifier))
|
425
|
+
|
426
|
+
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig
|
427
|
+
|
428
|
+
return (get_result_value(get(WebServicesBaseURL + "deletewatermark.ashx?" + qs), "Result") == TrueString)
|
429
|
+
end
|
430
|
+
|
431
|
+
# This method calls the GrabzIt web service to take the screenshot.
|
432
|
+
#
|
433
|
+
# @param url [String] the URL that the screenshot should be made of
|
434
|
+
# @param callback [String, nil] the handler the GrabzIt web service should call after it has completed its work
|
435
|
+
# @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.
|
436
|
+
# @param browserWidth [Integer, nil] the width of the browser in pixels
|
437
|
+
# @param browserHeight [Integer, nil] the height of the browser in pixels
|
438
|
+
# @param width [Integer, nil] the width of the resulting thumbnail in pixels
|
439
|
+
# @param height [Integer, nil] the height of the resulting thumbnail in pixels
|
440
|
+
# @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
441
|
+
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
442
|
+
# @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
|
443
|
+
# @return [String] returns the unique identifier of the screenshot. This can be used to get the screenshot with the get_result method
|
444
|
+
# @raise [RuntimeError] if the GrabzIt service reports an error with the request it will be raised as a RuntimeError
|
445
|
+
# @deprecated Use {#set_image_options} and {#save} instead.
|
446
|
+
def take_picture(url, callback = nil, customId = nil, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
|
447
|
+
set_image_options(url, customId, browserWidth, browserHeight, width, height, format, delay, targetElement)
|
448
|
+
return save(callback)
|
449
|
+
end
|
450
|
+
|
451
|
+
# This method takes the screenshot and then saves the result to a file. WARNING this method is synchronous
|
452
|
+
#
|
453
|
+
# @param url [String] the URL that the screenshot should be made of
|
454
|
+
# @param saveToFile [String] the file path that the screenshot should saved to
|
455
|
+
# @param browserWidth [Integer, nil] the width of the browser in pixels
|
456
|
+
# @param browserHeight [Integer, nil] the height of the browser in pixels
|
457
|
+
# @param width [Integer, nil] the width of the resulting thumbnail in pixels
|
458
|
+
# @param height [Integer, nil] the height of the resulting thumbnail in pixels
|
459
|
+
# @param format [String, nil] the format the screenshot should be in: bmp8, bmp16, bmp24, bmp, gif, jpg, png
|
460
|
+
# @param delay [Integer, nil] the number of milliseconds to wait before taking the screenshot
|
461
|
+
# @param targetElement [String, nil] the id of the only HTML element in the web page to turn into a screenshot
|
462
|
+
# @example Synchronously save the screenshot to test.jpg
|
463
|
+
# save_picture('images/test.jpg')
|
464
|
+
# @return [Boolean] returns the true if it is successfull otherwise it throws an exception
|
465
|
+
# @raise [RuntimeError] if the screenshot cannot be saved a RuntimeError will be raised that will contain an explanation
|
466
|
+
# @deprecated Use {#set_image_options} and {#save_to} instead.
|
467
|
+
def save_picture(url, saveToFile, browserWidth = nil, browserHeight = nil, width = nil, height = nil, format = nil, delay = nil, targetElement = nil)
|
468
|
+
set_image_options(url, nil, browserWidth, browserHeight, width, height, format, delay, targetElement)
|
469
|
+
return save_to(saveToFile)
|
470
|
+
end
|
471
|
+
|
472
|
+
# This method returns the image itself. If nothing is returned then something has gone wrong or the image is not ready yet.
|
473
|
+
#
|
474
|
+
# @param id [String] the id of the screenshot
|
475
|
+
# @return [Object] returns the screenshot
|
476
|
+
# @deprecated Use {#get_result} instead.
|
477
|
+
def get_picture(id)
|
478
|
+
return get_result(id)
|
479
|
+
end
|
480
|
+
|
481
|
+
private
|
482
|
+
def get_watermarks(identifier = nil)
|
483
|
+
sig = encode(nil_check(@applicationSecret)+"|"+nil_check(identifier))
|
484
|
+
|
485
|
+
qs = "key=" +CGI.escape(nil_check(@applicationKey))+"&identifier="+CGI.escape(nil_check(identifier))+"&sig="+sig
|
486
|
+
|
487
|
+
result = get(WebServicesBaseURL + "getwatermarks.ashx?" + qs)
|
488
|
+
|
489
|
+
doc = REXML::Document.new(result)
|
490
|
+
|
491
|
+
check_for_exception(doc)
|
492
|
+
|
493
|
+
watermarks = Array.new
|
494
|
+
|
495
|
+
xml_watemarks = doc.elements.to_a("//WebResult/WaterMarks/WaterMark")
|
496
|
+
xml_watemarks.each do |watemark|
|
497
|
+
grabzItWaterMark = GrabzIt::WaterMark.new(watemark.elements["Identifier"].text, watemark.elements["XPosition"].text.to_i, watemark.elements["YPosition"].text.to_i, watemark.elements["Format"].text)
|
498
|
+
watermarks << grabzItWaterMark
|
499
|
+
end
|
500
|
+
|
501
|
+
return watermarks
|
502
|
+
end
|
503
|
+
|
504
|
+
private
|
505
|
+
def get(url)
|
506
|
+
uri = URI.parse(url)
|
507
|
+
response = Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.request_uri) }
|
508
|
+
response_check(response)
|
509
|
+
return response.body
|
510
|
+
end
|
511
|
+
|
512
|
+
private
|
513
|
+
def response_check(response)
|
514
|
+
statusCode = response.code.to_i
|
515
|
+
if statusCode == 403
|
516
|
+
raise GrabzItException.new(response.body, GrabzItException::NETWORK_DDOS_ATTACK)
|
517
|
+
elsif statusCode >= 400
|
518
|
+
raise GrabzItException.new("A network error occured when connecting to the GrabzIt servers.", GrabzItException::NETWORK_GENERAL_ERROR)
|
519
|
+
end
|
520
|
+
end
|
521
|
+
|
522
|
+
private
|
523
|
+
def b_to_str(bValue)
|
524
|
+
if bValue
|
525
|
+
return 1.to_s
|
526
|
+
end
|
527
|
+
return 0.to_s
|
528
|
+
end
|
529
|
+
|
530
|
+
private
|
531
|
+
def nil_check(param)
|
532
|
+
if param == nil
|
533
|
+
return ""
|
534
|
+
end
|
535
|
+
return param
|
536
|
+
end
|
537
|
+
|
538
|
+
private
|
539
|
+
def nil_int_check(param)
|
540
|
+
return param.to_i.to_s
|
541
|
+
end
|
542
|
+
|
543
|
+
private
|
544
|
+
def nil_float_check(param)
|
545
|
+
val = param.to_f
|
546
|
+
if ((val % 1) == 0)
|
547
|
+
return val.to_i.to_s
|
548
|
+
end
|
549
|
+
|
550
|
+
return val.to_s
|
551
|
+
end
|
552
|
+
|
553
|
+
private
|
554
|
+
def check_for_exception(doc)
|
555
|
+
if (doc == nil)
|
556
|
+
return
|
557
|
+
end
|
558
|
+
|
559
|
+
message = doc.root.elements["Message"].text()
|
560
|
+
code = doc.root.elements["Code"].text()
|
561
|
+
|
562
|
+
if message != nil
|
563
|
+
raise GrabzItException.new(message, code)
|
564
|
+
end
|
565
|
+
end
|
566
|
+
|
567
|
+
private
|
568
|
+
def encode(text)
|
569
|
+
return Digest::MD5.hexdigest(text.encode('ascii', {:invalid => :replace, :undef => :replace, :replace => '?'}))
|
570
|
+
end
|
571
|
+
|
572
|
+
private
|
573
|
+
def get_result_value(result, field)
|
574
|
+
doc = REXML::Document.new(result)
|
575
|
+
|
576
|
+
value = doc.root.elements[field].text()
|
577
|
+
|
578
|
+
check_for_exception(doc)
|
579
|
+
|
580
|
+
return value
|
581
|
+
end
|
582
|
+
end
|
583
|
+
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: 2.2.
|
4
|
+
version: 2.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-07-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -27,10 +27,11 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
-
description:
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
description: Use GrabzIt to take PDF or image screenshots of websites. These screenshots
|
31
|
+
have highly customizable options include altering quality, delay, size, browser
|
32
|
+
type, geographic location and much more. Additionally GrabzIt can even convert HTML
|
33
|
+
tables on the web into a CSV or Excel spreadsheet. As well as enabling online video's
|
34
|
+
to be converted into animated GIF's.
|
34
35
|
email: support@grabz.it
|
35
36
|
executables: []
|
36
37
|
extensions: []
|