-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
651 lines (483 loc) · 18.2 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package gosdk
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
const baseURL = "https://api.screenshotone.com"
const takePath = "/take"
// Client API client for the ScreenshotOne.com API.
type Client struct {
accessKey, secretKey string
httpClient *http.Client
}
// NewClient returns new API client for the ScreenshotOne.com API.
func NewClient(accessKey, secretKey string) (*Client, error) {
client := &Client{accessKey, secretKey, &http.Client{}}
return client, nil
}
// GenerateTakeURL generates URL for taking screenshots, but does not send any request.
func (client *Client) GenerateTakeURL(options *TakeOptions) (*url.URL, error) {
// generate query
query := options.query
query.Set("access_key", client.accessKey)
queryString := query.Encode()
// sign the query string and append the signature
hash := hmac.New(sha256.New, []byte(client.secretKey))
_, err := hash.Write([]byte(queryString))
if err != nil {
return nil, fmt.Errorf("failed to sign the query string: %w", err)
}
signature := hex.EncodeToString(hash.Sum(nil))
queryString += "&signature=" + signature
u, err := url.Parse(baseURL + takePath)
if err != nil {
return nil, fmt.Errorf("failed to parse URL \"%s\": %w", baseURL+takePath, err)
}
u.RawQuery = queryString
return u, nil
}
// Take takes screenshot and returns image or error if the request failed.
func (client *Client) Take(ctx context.Context, options *TakeOptions) ([]byte, *http.Response, error) {
u, err := client.GenerateTakeURL(options)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate URL: %w", err)
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, fmt.Errorf("failed to instantiate HTTP request: %w", err)
}
response, err := client.httpClient.Do(request)
if err != nil {
return nil, nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, response, fmt.Errorf("the server returned a response: %d %s", response.StatusCode, response.Status)
}
defer response.Body.Close()
image, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, nil, fmt.Errorf("failed to read the image data from HTTP response: %w", err)
}
return image, nil, nil
}
// TakeOptions for the ScreenshotOne.com API take method.
type TakeOptions struct {
query url.Values
}
// Returns options for the ScreenshotOne.com API take method.
func NewTakeOptions(pageURL string) *TakeOptions {
query := url.Values{}
query.Add("url", pageURL)
return NewTakeWithURL(pageURL)
}
// Returns options for the ScreenshotOne.com API take method.
func NewTakeWithURL(pageURL string) *TakeOptions {
query := url.Values{}
query.Add("url", pageURL)
return &TakeOptions{query: query}
}
// Returns options for the ScreenshotOne.com API take method.
func NewTakeWithHTML(html string) *TakeOptions {
query := url.Values{}
query.Add("html", html)
return &TakeOptions{query: query}
}
// Selector is a CSS-like selector of the element to take a screenshot of.
func (o *TakeOptions) Selector(selector string) *TakeOptions {
o.query.Add("selector", selector)
return o
}
// ErrorOnSelectorNotFound determines the behavior of what to do when selector is not found.
func (o *TakeOptions) ErrorOnSelectorNotFound(errorOn bool) *TakeOptions {
o.query.Add("error_on_selector_not_found", strconv.FormatBool(errorOn))
return o
}
// FullPage renders the full page.
func (o *TakeOptions) FullPage(fullPage bool) *TakeOptions {
o.query.Add("full_page", strconv.FormatBool(fullPage))
return o
}
// Format sets response format, one of: "png", "jpeg", "webp" or "jpg".
func (o *TakeOptions) Format(format string) *TakeOptions {
o.query.Add("format", format)
return o
}
// Styles specifies custom CSS styles for the page.
func (o *TakeOptions) Styles(styles string) *TakeOptions {
o.query.Add("styles", styles)
return o
}
// Scripts specifies custom scripts for the page.
func (o *TakeOptions) Scripts(scripts string) *TakeOptions {
o.query.Add("scripts", scripts)
return o
}
// ImageQuality renders image with the specified quality. Available for the next formats: "jpeg" ("jpg"), "webp".
func (o *TakeOptions) ImageQuality(imageQuality int) *TakeOptions {
o.query.Add("image_quality", strconv.Itoa(imageQuality))
return o
}
// OmitBackground renders a transparent background for the image. Works only if the site has not defined background color.
// Available for the following response formats: "png", "webp".
func (o *TakeOptions) OmitBackground(omitBackground bool) *TakeOptions {
o.query.Add("omit_background", strconv.FormatBool(omitBackground))
return o
}
// ViewportWidth sets the width of the browser viewport (pixels).
func (o *TakeOptions) ViewportWidth(viewportWidth int) *TakeOptions {
o.query.Add("viewport_width", strconv.Itoa(viewportWidth))
return o
}
// ViewportWidth sets the height of the browser viewport (pixels).
func (o *TakeOptions) ViewportHeight(viewportHeight int) *TakeOptions {
o.query.Add("viewport_height", strconv.Itoa(viewportHeight))
return o
}
// DeviceScaleFactor sets the device scale factor. Acceptable value is one of: 1, 2 or 3.
func (o *TakeOptions) DeviceScaleFactor(deviceScaleFactor int) *TakeOptions {
o.query.Add("device_scale_factor", strconv.Itoa(deviceScaleFactor))
return o
}
// GeolocationLatitude sets geolocation latitude for the request.
// Both latitude and longitude are required if one of them is set.
func (o *TakeOptions) GeolocationLatitude(latitude float64) *TakeOptions {
o.query.Add("geolocation_latitude", strconv.FormatFloat(latitude, byte('f'), -1, 64))
return o
}
// GeolocationLatitude sets geolocation longitude for the request.
// Both latitude and longitude are required if one of them is set.
func (o *TakeOptions) GeolocationLongitude(longitude float64) *TakeOptions {
o.query.Add("geolocation_longitude", strconv.FormatFloat(longitude, byte('f'), -1, 64))
return o
}
// GeolocationAccuracy sets the geolocation accuracy in meters.
func (o *TakeOptions) GeolocationAccuracy(accuracy int) *TakeOptions {
o.query.Add("geolocation_accuracy", strconv.Itoa(accuracy))
return o
}
// BlockAds blocks ads.
func (o *TakeOptions) BlockAds(blockAds bool) *TakeOptions {
o.query.Add("block_ads", strconv.FormatBool(blockAds))
return o
}
// BlockTrackers blocks trackers.
func (o *TakeOptions) BlockTrackers(blockTrackers bool) *TakeOptions {
o.query.Add("block_trackers", strconv.FormatBool(blockTrackers))
return o
}
// BlockRequests blocks requests by specifying URL, domain, or even a simple pattern.
func (o *TakeOptions) BlockRequests(blockRequests ...string) *TakeOptions {
for _, blockRequest := range blockRequests {
o.query.Add("block_requests", blockRequest)
}
return o
}
// BlockResources blocks loading resources by type.
// Available resource types are: "document", "stylesheet", "image", "media", "font", "script", "texttrack", "xhr", "fetch", "eventsource", "websocket", "manifest", "other".
func (o *TakeOptions) BlockResources(blockResources ...string) *TakeOptions {
for _, blockResource := range blockResources {
o.query.Add("block_resources", blockResource)
}
return o
}
// Cache allows to cache the screenshot.
func (o *TakeOptions) Cache(cache bool) *TakeOptions {
o.query.Add("cache", strconv.FormatBool(cache))
return o
}
// CacheTTL sets cache TTL.
func (o *TakeOptions) CacheTTL(cacheTTL int) *TakeOptions {
o.query.Add("cache_ttl", strconv.Itoa(cacheTTL))
return o
}
// CacheTTL sets cache key.
func (o *TakeOptions) CacheKey(cacheKey string) *TakeOptions {
o.query.Add("cache_key", cacheKey)
return o
}
// UserAgent sets a user agent for the request.
func (o *TakeOptions) UserAgent(userAgent string) *TakeOptions {
o.query.Add("user_agent", userAgent)
return o
}
// Authorization sets an authorization header for the request.
func (o *TakeOptions) Authorization(authorization string) *TakeOptions {
o.query.Add("authorization", authorization)
return o
}
// Cookies set cookies for the request.
func (o *TakeOptions) Cookies(cookies ...string) *TakeOptions {
for _, cookie := range cookies {
o.query.Add("cookies", cookie)
}
return o
}
// Headers sets extra headers for the request.
func (o *TakeOptions) Headers(headers ...string) *TakeOptions {
for _, header := range headers {
o.query.Add("headers", header)
}
return o
}
// TimeZone sets time zone for the request.
// Available time zones are: "America/Santiago", "Asia/Shanghai", "Europe/Berlin", "America/Guayaquil", "Europe/Madrid", "Pacific/Majuro", "Asia/Kuala_Lumpur", "Pacific/Auckland", "Europe/Lisbon", "Europe/Kiev", "Asia/Tashkent", "Europe/London".
func (o *TakeOptions) TimeZone(timeZone string) *TakeOptions {
o.query.Add("time_zone", timeZone)
return o
}
// Delay sets delay.
func (o *TakeOptions) Delay(delay int) *TakeOptions {
o.query.Add("delay", strconv.Itoa(delay))
return o
}
// Timeout sets timeout.
func (o *TakeOptions) Timeout(timeout int) *TakeOptions {
o.query.Add("timeout", strconv.Itoa(timeout))
return o
}
// WaitUntil waits until an event occurred before taking a screenshot or rendering HTML or PDF.
func (o *TakeOptions) WaitUntil(events ...string) *TakeOptions {
for _, event := range events {
o.query.Add("wait_until", event)
}
return o
}
// WaitForSelector waits until the element appears in DOM.
func (o *TakeOptions) WaitForSelector(selector string) *TakeOptions {
o.query.Add("wait_for_selector", selector)
return o
}
// WaitForSelectorAlgorithm sets the algorithm for waiting for selectors.
func (o *TakeOptions) WaitForSelectorAlgorithm(algorithm string) *TakeOptions {
o.query.Add("wait_for_selector_algorithm", algorithm)
return o
}
// NavigationTimeout sets the timeout for navigation.
func (o *TakeOptions) NavigationTimeout(timeout int) *TakeOptions {
o.query.Add("navigation_timeout", strconv.Itoa(timeout))
return o
}
// CaptureBeyondViewport controls whether to capture beyond the viewport.
func (o *TakeOptions) CaptureBeyondViewport(capture bool) *TakeOptions {
o.query.Add("capture_beyond_viewport", strconv.FormatBool(capture))
return o
}
// FullPageScroll controls whether to scroll to the bottom of the page and back to the top.
func (o *TakeOptions) FullPageScroll(scroll bool) *TakeOptions {
o.query.Add("full_page_scroll", strconv.FormatBool(scroll))
return o
}
// FullPageScrollDelay sets the delay for full page scrolling.
func (o *TakeOptions) FullPageScrollDelay(delay int) *TakeOptions {
o.query.Add("full_page_scroll_delay", strconv.Itoa(delay))
return o
}
// FullPageScrollBy sets how much to scroll by for full page screenshots.
func (o *TakeOptions) FullPageScrollBy(pixels int) *TakeOptions {
o.query.Add("full_page_scroll_by", strconv.Itoa(pixels))
return o
}
// FullPageMaxHeight sets the maximum height for full page screenshots.
func (o *TakeOptions) FullPageMaxHeight(height int) *TakeOptions {
o.query.Add("full_page_max_height", strconv.Itoa(height))
return o
}
// HideSelectors hides elements matching the given selectors.
func (o *TakeOptions) HideSelectors(selectors ...string) *TakeOptions {
for _, selector := range selectors {
o.query.Add("hide_selectors", selector)
}
return o
}
// Click specifies a selector to click before taking the screenshot.
func (o *TakeOptions) Click(selector string) *TakeOptions {
o.query.Add("click", selector)
return o
}
// ScrollIntoView scrolls the page to ensure the given selector is in view.
func (o *TakeOptions) ScrollIntoView(selector string) *TakeOptions {
o.query.Add("scroll_into_view", selector)
return o
}
// ScrollIntoViewAdjustTop adjusts the top position after scrolling into view.
func (o *TakeOptions) ScrollIntoViewAdjustTop(pixels int) *TakeOptions {
o.query.Add("scroll_into_view_adjust_top", strconv.Itoa(pixels))
return o
}
// DarkMode sets the dark mode for the screenshot.
func (o *TakeOptions) DarkMode(enabled bool) *TakeOptions {
o.query.Add("dark_mode", strconv.FormatBool(enabled))
return o
}
// ReducedMotion sets the reduced motion mode for the screenshot.
func (o *TakeOptions) ReducedMotion(enabled bool) *TakeOptions {
o.query.Add("reduced_motion", strconv.FormatBool(enabled))
return o
}
// MediaType sets the media type for the screenshot.
func (o *TakeOptions) MediaType(mediaType string) *TakeOptions {
o.query.Add("media_type", mediaType)
return o
}
// ViewportMobile sets whether the meta viewport tag is taken into account.
func (o *TakeOptions) ViewportMobile(mobile bool) *TakeOptions {
o.query.Add("viewport_mobile", strconv.FormatBool(mobile))
return o
}
// ViewportHasTouch sets whether the viewport supports touch events.
func (o *TakeOptions) ViewportHasTouch(hasTouch bool) *TakeOptions {
o.query.Add("viewport_has_touch", strconv.FormatBool(hasTouch))
return o
}
// ViewportLandscape sets whether the viewport is in landscape mode.
func (o *TakeOptions) ViewportLandscape(landscape bool) *TakeOptions {
o.query.Add("viewport_landscape", strconv.FormatBool(landscape))
return o
}
// ViewportDevice sets the device for viewport emulation.
func (o *TakeOptions) ViewportDevice(device string) *TakeOptions {
o.query.Add("viewport_device", device)
return o
}
// BlockCookieBanners blocks cookie banners and privacy notices.
func (o *TakeOptions) BlockCookieBanners(block bool) *TakeOptions {
o.query.Add("block_cookie_banners", strconv.FormatBool(block))
return o
}
// BlockBannersByHeuristics blocks banners using heuristics.
func (o *TakeOptions) BlockBannersByHeuristics(block bool) *TakeOptions {
o.query.Add("block_banners_by_heuristics", strconv.FormatBool(block))
return o
}
// BlockChats blocks chat widgets.
func (o *TakeOptions) BlockChats(block bool) *TakeOptions {
o.query.Add("block_chats", strconv.FormatBool(block))
return o
}
// BypassCSP bypasses Content Security Policy.
func (o *TakeOptions) BypassCSP(bypass bool) *TakeOptions {
o.query.Add("bypass_csp", strconv.FormatBool(bypass))
return o
}
// Proxy sets a custom proxy for the request.
func (o *TakeOptions) Proxy(proxyURL string) *TakeOptions {
o.query.Add("proxy", proxyURL)
return o
}
// IPCountryCode sets the country for IP-based geolocation.
func (o *TakeOptions) IPCountryCode(countryCode string) *TakeOptions {
o.query.Add("ip_country_code", countryCode)
return o
}
// ResponseType sets the type of response to return.
func (o *TakeOptions) ResponseType(responseType string) *TakeOptions {
o.query.Add("response_type", responseType)
return o
}
// Store enables storing the screenshot in S3-compatible storage.
func (o *TakeOptions) Store(store bool) *TakeOptions {
o.query.Add("store", strconv.FormatBool(store))
return o
}
// StoragePath sets the storage path for the screenshot.
func (o *TakeOptions) StoragePath(path string) *TakeOptions {
o.query.Add("storage_path", path)
return o
}
// StorageACL sets the ACL for the stored screenshot.
func (o *TakeOptions) StorageACL(acl string) *TakeOptions {
o.query.Add("storage_acl", acl)
return o
}
// StorageReturnLocation enables returning the storage location.
func (o *TakeOptions) StorageReturnLocation(returnLocation bool) *TakeOptions {
o.query.Add("storage_return_location", strconv.FormatBool(returnLocation))
return o
}
// Async enables asynchronous execution of the request.
func (o *TakeOptions) Async(async bool) *TakeOptions {
o.query.Add("async", strconv.FormatBool(async))
return o
}
// WebhookURL sets the URL for the webhook.
func (o *TakeOptions) WebhookURL(url string) *TakeOptions {
o.query.Add("webhook_url", url)
return o
}
// WebhookSign controls whether to sign the webhook request body.
func (o *TakeOptions) WebhookSign(sign bool) *TakeOptions {
o.query.Add("webhook_sign", strconv.FormatBool(sign))
return o
}
// RequestGPURendering requests GPU rendering for the screenshot.
func (o *TakeOptions) RequestGPURendering(request bool) *TakeOptions {
o.query.Add("request_gpu_rendering", strconv.FormatBool(request))
return o
}
// FailIfGPURenderingFails forces the request to fail if GPU rendering fails.
func (o *TakeOptions) FailIfGPURenderingFails(fail bool) *TakeOptions {
o.query.Add("fail_if_gpu_rendering_fails", strconv.FormatBool(fail))
return o
}
// MetadataImageSize enables returning the actual image size metadata.
func (o *TakeOptions) MetadataImageSize(enable bool) *TakeOptions {
o.query.Add("metadata_image_size", strconv.FormatBool(enable))
return o
}
// MetadataFonts enables returning the fonts used by the website.
func (o *TakeOptions) MetadataFonts(enable bool) *TakeOptions {
o.query.Add("metadata_fonts", strconv.FormatBool(enable))
return o
}
// MetadataOpenGraph enables returning the Open Graph metadata.
func (o *TakeOptions) MetadataOpenGraph(enable bool) *TakeOptions {
o.query.Add("metadata_open_graph", strconv.FormatBool(enable))
return o
}
// MetadataPageTitle enables returning the page title.
func (o *TakeOptions) MetadataPageTitle(enable bool) *TakeOptions {
o.query.Add("metadata_page_title", strconv.FormatBool(enable))
return o
}
// MetadataHTTPResponseHeaders enables returning the HTTP response headers.
func (o *TakeOptions) MetadataHTTPResponseHeaders(enable bool) *TakeOptions {
o.query.Add("metadata_http_response_headers", strconv.FormatBool(enable))
return o
}
// MetadataHTTPResponseStatusCode enables returning the HTTP response status code.
func (o *TakeOptions) MetadataHTTPResponseStatusCode(enable bool) *TakeOptions {
o.query.Add("metadata_http_response_status_code", strconv.FormatBool(enable))
return o
}
// MetadataContent enables returning the content of the page.
func (o *TakeOptions) MetadataContent(enable bool) *TakeOptions {
o.query.Add("metadata_content", strconv.FormatBool(enable))
return o
}
// OpenAIAPIKey sets the OpenAI API key for vision integration.
func (o *TakeOptions) OpenAIAPIKey(key string) *TakeOptions {
o.query.Add("openai_api_key", key)
return o
}
// VisionPrompt sets the prompt for OpenAI vision integration.
func (o *TakeOptions) VisionPrompt(prompt string) *TakeOptions {
o.query.Add("vision_prompt", prompt)
return o
}
// VisionMaxTokens sets the maximum number of tokens for OpenAI vision integration.
func (o *TakeOptions) VisionMaxTokens(tokens int) *TakeOptions {
o.query.Add("vision_max_tokens", strconv.Itoa(tokens))
return o
}
// FailIfContentContains forces the request to fail if the specified text is matched on the page.
func (o *TakeOptions) FailIfContentContains(text string) *TakeOptions {
o.query.Add("fail_if_content_contains", text)
return o
}