-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenshoMachine.java
48 lines (39 loc) · 1.78 KB
/
ScreenshoMachine.java
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
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import javax.xml.bind.DatatypeConverter;
public class ScreenshoMachine {
private String customerKey;
private String secretPhrase;
private String apiBaseUrl = "https://api.screenshotmachine.com/?";
private String pdfApiBaseUrl = "https://pdfapi.screenshotmachine.com/?";
public ScreenshoMachine(String customerKey, String secretPhrase) {
this.customerKey = customerKey;
this.secretPhrase = secretPhrase;
}
public String generateScreenshotApiUrl(Map<String, String> options) throws NoSuchAlgorithmException, UnsupportedEncodingException {
return generateUrl(apiBaseUrl, options);
}
public String generatePdfApiUrl(Map<String, String> options) throws NoSuchAlgorithmException, UnsupportedEncodingException {
return generateUrl(pdfApiBaseUrl, options);
}
public String generateUrl(String baseUrl, Map<String, String> options) throws NoSuchAlgorithmException, UnsupportedEncodingException {
StringBuilder apiUrl = new StringBuilder(baseUrl);
apiUrl.append("key=").append(customerKey);
if (secretPhrase != null && secretPhrase.trim().length() > 0) {
apiUrl.append("&hash=").append(calculateHash(options.get("url") + secretPhrase));
}
for (String key : options.keySet()) {
apiUrl.append("&").append(key).append("=").append(URLEncoder.encode(options.get(key), StandardCharsets.UTF_8.toString()));
}
return apiUrl.toString();
}
private String calculateHash(String text) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(text.getBytes());
return DatatypeConverter.printHexBinary(md.digest()).toLowerCase();
}
}