/
index.js
71 lines (63 loc) · 1.93 KB
/
index.js
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
const { default: axios } = require("axios");
const FormData = require("form-data");
const DEFAULT_API_KEY = "c98dc592f9msh2004666c82a805dp1266d4jsn8ce72d731ad1"; // For Eveyone :)
class safeImages {
constructor(apiKey) {
this.apiKey = apiKey || DEFAULT_API_KEY;
}
setApiKey(apiKey) {
this.apiKey = apiKey || DEFAULT_API_KEY;
}
async detectFromURL(imageURL) {
try {
const response = await this.makeRequest({
method: "POST",
url: "https://nsfw-images-detection-and-classification.p.rapidapi.com/adult-content",
data: {
url: imageURL,
},
});
const objects = response.data.objects || [];
return objects.length >= 1;
} catch (error) {
console.error(error);
throw new Error("Error while detecting NSFW content.");
}
}
async detectFromFile(imageBuffer) {
try {
const response = await this.makeRequest({
method: "POST",
url: "https://nsfw-images-detection-and-classification.p.rapidapi.com/adult-content-file",
data: this.createFormData(imageBuffer),
});
const objects = response.data.objects || [];
return objects.length >= 1;
} catch (error) {
console.error(error);
throw new Error("Error while detecting NSFW content.");
}
}
async makeRequest(options) {
try {
const response = await axios.request({
...options,
headers: {
...options.headers,
"X-RapidAPI-Key": this.apiKey,
"X-RapidAPI-Host":
"nsfw-images-detection-and-classification.p.rapidapi.com",
},
});
return response;
} catch (error) {
throw new Error("Error while making the API request.");
}
}
createFormData(imageBuffer) {
const data = new FormData();
data.append("image", imageBuffer);
return data;
}
}
module.exports = safeImages;