-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScreenshot.cs
More file actions
178 lines (127 loc) · 4.07 KB
/
Screenshot.cs
File metadata and controls
178 lines (127 loc) · 4.07 KB
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
using System;
using RestSharp;
using Restpack;
using Newtonsoft.Json;
namespace Restpack.Screenshot
{
public class Screenshot
{
private RestClient client;
public struct RequestOptions
{
[JsonProperty("mode")]
public string Mode;
[JsonProperty("format")]
public string Format;
[JsonProperty("json")]
public bool Json;
[JsonProperty("url")]
public string Url;
[JsonProperty("html")]
public string HTML;
[JsonProperty("height")]
public int Height;
[JsonProperty("width")]
public int Width;
[JsonProperty("thumbnail_height")]
public int ThumbnailHeight;
[JsonProperty("thumbnail_width")]
public int ThumbnailWidth;
[JsonProperty("css")]
public string CSS;
[JsonProperty("js")]
public string JS;
[JsonProperty("delay")]
public int Delay;
[JsonProperty("cache_ttl")]
public int CacheTTL;
[JsonProperty("user_agent")]
public string UserAgent;
[JsonProperty("accept_language")]
public string AcceptLanguage;
[JsonProperty("headers")]
public string Headers;
[JsonProperty("element_selector")]
public string ElementSelector;
[JsonProperty("retina")]
public bool Retina;
[JsonProperty("emulate_media")]
public string EmulateMedia;
[JsonProperty("allow_failed")]
public bool AllowFailed;
[JsonProperty("wait")]
public string Wait;
[JsonProperty("shutter")]
public string Shutter;
[JsonProperty("privacy")]
public bool Privacy;
[JsonProperty("filename")]
public string Filename;
[JsonProperty("block_ads")]
public bool BlockAds;
[JsonProperty("block_cookie_warnings")]
public bool BlockCookieWarnings;
[JsonProperty("omit_background")]
public bool OmitBackground;
}
public Screenshot(string accessToken)
{
client = new Client("https://restpack.io/api/screenshot/v7/capture", accessToken).GetClient();
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
};
}
private ResultJson Execute(RequestOptions options)
{
RestRequest request = new RestRequest();
var jsonBody = JsonConvert.SerializeObject(options, Formatting.Indented);
request.AddJsonBody(jsonBody);
var response = client.Post(request);
if (!response.IsSuccessful)
{
var resultError = Newtonsoft.Json.JsonConvert.DeserializeObject<ResultError>(response.Content);
throw new Exception(resultError.Error);
}
return Newtonsoft.Json.JsonConvert.DeserializeObject<ResultJson>(response.Content);
}
private byte[] ExecuteBytes(RequestOptions options)
{
RestRequest request = new RestRequest();
var jsonBody = JsonConvert.SerializeObject(options, Formatting.Indented);
request.AddJsonBody(jsonBody);
var response = client.Post(request);
if (!response.IsSuccessful)
{
var resultError = Newtonsoft.Json.JsonConvert.DeserializeObject<ResultError>(response.Content);
throw new Exception(resultError.Error);
}
return response.RawBytes;
}
public ResultJson Capture(string url, RequestOptions options = new RequestOptions())
{
options.Json = true;
options.Url = url;
return this.Execute(options);
}
public byte[] CaptureBytes(string url, RequestOptions options = new RequestOptions())
{
options.Json = false;
options.Url = url;
return this.ExecuteBytes(options);
}
public ResultJson CaptureHTML(string html, RequestOptions options = new RequestOptions())
{
options.Json = true;
options.HTML = html;
return this.Execute(options);
}
public byte[] CaptureHTMLBytes(string html, RequestOptions options = new RequestOptions())
{
options.Json = false;
options.HTML = html;
return this.ExecuteBytes(options);
}
}
}