-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
290 lines (234 loc) · 8.73 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
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
const http = require('http');
const {
Copyleaks,
CopyleaksConfig,
CopyleaksURLSubmissionModel,
CopyleaksFileSubmissionModel,
CopyleaksFileOcrSubmissionModel,
CopyleaksDeleteRequestModel,
CopyleaksExportModel,
CopyleaksSourceCodeSubmissionModel,
CopyleaksNaturalLanguageSubmissionModel,
CopyleaksWritingAssistantSubmissionModel
} = require('../dist');
const base64Img = require('./base64.img');
const hostname = '127.0.0.1';
const port = 3000;
const DEMO_EMAIL = '<EMAIL>'; // change this with your own copyleaks email.
const DEMO_KEY = '<API_KEY>'; // change this with your own copyleaks API key.
const WEBHOOK_URL = '<WEBHOOK_URL>'; //exe https://glacial-refuge-96501.herokuapp.com/10b0z2w1
const copyleaks = new Copyleaks();
let testingInProgress = false;
const server = http.createServer((req, res) => {
res.statusCode = 200;
TEST_copyleaks();
res.setHeader('Content-Type', 'text/plain');
res.end('started testing - check logs');
});
TEST_copyleaks = () => {
if (testingInProgress) {
return;
}
testingInProgress = true;
// Login
copyleaks.loginAsync(DEMO_EMAIL, DEMO_KEY)
.then(
loginResult => {
logSuccess('loginAsync', loginResult);
// TEST_MISC();
TEST_CreditsBalance(loginResult);
// TEST_UsageHistory(loginResult);
// TEST_submitUrlAsync(loginResult);
// TEST_submitFileAsync(loginResult);
// TEST_submitOcrFileAsync(loginResult);
// TEST_deleteScanAsync(["1653575562405", "1653575774429"],loginResult);
// TEST_exportAsync(loginResult);
// TEST_submitAIDetectionNaturalLanguage(loginResult);
// TEST_submitAIDetectionSourceCode(loginResult);
// TEST_submitWritingAssistText(loginResult);
// TEST_getCorrectionTypes(loginResult);
},
err => logError('loginAsync', err)
)
}
function TEST_MISC() {
// OCR Supported Languages
copyleaks.getOCRSupportedLanguagesAsync()
.then(
loginResult => {
logSuccess('getOCRSupportedLanguagesAsync', loginResult);
},
err => logError('getOCRSupportedLanguagesAsync', err)
)
// Supported File Types
copyleaks.getSupportedFileTypesAsync()
.then(
loginResult => {
logSuccess('getSupportedFileTypesAsync', loginResult);
},
err => logError('getSupportedFileTypesAsync', err)
)
// Release Notes
copyleaks.getReleaseNotesAsync()
.then(
loginResult => {
logSuccess('getReleaseNotesAsync', loginResult);
},
err => logError('getReleaseNotesAsync', err)
)
}
function TEST_CreditsBalance(loginResult) {
copyleaks.getCreditsBalanceAsync(loginResult).then(res => logSuccess('getCreditsBalanceAsync', res), err => logError('getCreditsBalanceAsync', err));
}
function TEST_UsageHistory(loginResult) {
copyleaks.getUsagesHistoryCsvAsync(loginResult, '01-01-2020', '02-02-2020').then(res => logSuccess('getUsagesHistoryCsvAsync', res), err => logError('getUsagesHistoryCsvAsync', err));
}
function TEST_submitUrlAsync(loginResult) {
var submission = new CopyleaksURLSubmissionModel(
'https://copyleaks.com',
{
sandbox: true,
webhooks: {
status: `${WEBHOOK_URL}/submit-url-webhook/{STATUS}`
}
}
);
copyleaks.submitUrlAsync(loginResult, Date.now() + 1, submission).then(res => logSuccess('submitUrlAsync', res), err => { logError('submitUrlAsync', err) });
}
function TEST_submitFileAsync(loginResult) {
var submission = new CopyleaksFileSubmissionModel(
'aGVsbG8gd29ybGQ=',
'nodejs-sdk-demo.txt',
{
sandbox: true,
webhooks: {
status: `${WEBHOOK_URL}/submit-file-webhook/{STATUS}`
}
}
);
copyleaks.submitFileAsync(loginResult, Date.now() + 1, submission).then(res => logSuccess('submitFileAsync', res), err => { logError('submitFileAsync', err) });
}
function TEST_submitOcrFileAsync(loginResult) {
var submission = new CopyleaksFileOcrSubmissionModel(
'en',
base64Img,
'nodejs-sdk-demo.txt',
{
sandbox: true,
webhooks: {
status: `${WEBHOOK_URL}/submit-file-ocr-webhook/{STATUS}`
}
}
);
copyleaks.submitFileOcrAsync(loginResult, Date.now() + 1, submission).then(res => logSuccess('submitFileOcrAsync', res), err => { logError('submitFileOcrAsync', err) });
}
function TEST_deleteScanAsync(scansId, loginResult) {
if (scansId.length) {
const model = new CopyleaksDeleteRequestModel(
// add your own scan ids to remove
scansId.map(id => ({ id })),
false,
`${WEBHOOK_URL}/delete`
)
copyleaks.deleteAsync(loginResult, model).then(res => logSuccess('deleteAsync', res), err => { logError('deleteAsync', err) });;
}
}
function TEST_exportAsync(loginResult) {
var scanId = "1610625417127"; // change this with your own scanId
const model = new CopyleaksExportModel(
`${WEBHOOK_URL}/export/scanId/${scanId}/completion`,
[
// results
{
id: '2a1b402420', // change this with your own result Id
endpoint: `${WEBHOOK_URL}/export/${scanId}/result/2a1b402420`,
verb: 'POST',
headers: [['key', 'value'], ['key2', 'value2']]
}
],
{
// crawled version
endpoint: `${WEBHOOK_URL}/export/${scanId}/crawled-version`,
verb: 'POST',
headers: [['key', 'value'], ['key2', 'value2']]
}
);
copyleaks.exportAsync(loginResult, scanId, scanId, model).then(res => logSuccess('exportAsync', res), err => { logError('exportAsync', err) });
}
function TEST_submitAIDetectionNaturalLanguage(loginResult) {
const sampleText = "Lions are social animals, living in groups called prides, typically consisting of several females, their offspring, and a few males. Female lions are the primary hunters, working together to catch prey. Lions are known for their strength, teamwork, and complex social structures.";
const submission = new CopyleaksNaturalLanguageSubmissionModel(sampleText);
submission.sandbox = true;
copyleaks.aiDetectionClient.submitNaturalTextAsync(loginResult, Date.now() + 1, submission)
.then(response => {
logSuccess('TEST_submitAIDetectionNaturalLanguage', response);
})
.catch(error => {
logError('TEST_submitAIDetectionNaturalLanguage', error);
});
}
function TEST_submitAIDetectionSourceCode(loginResult) {
const sampleCode = `
def add(a, b):
return a + b
def multiply(a, b):
return a * b
def main():
x = 5
y = 10
sum_result = add(x, y)
product_result = multiply(x, y)
print(f'Sum: {sum_result}')
print(f'Product: {product_result}')
if __name__ == '__main__':
main()
`;
const submission = new CopyleaksSourceCodeSubmissionModel(sampleCode, 'example.py');
submission.sandbox = true;
copyleaks.aiDetectionClient.submitNaturalTextAsync(loginResult, Date.now() + 1, submission)
.then(response => {
logSuccess('TEST_submitAIDetectionSourceCode', response);
})
.catch(error => {
logError('TEST_submitAIDetectionSourceCode', error);
});
}
function TEST_submitWritingAssistText(loginResult) {
const sampleText = "Lions are the only cat that live in groups, called pride. A prides typically consists of a few adult males, several feales, and their offspring. This social structure is essential for hunting and raising young cubs. Female lions, or lionesses are the primary hunters of the prid. They work together in cordinated groups to take down prey usually targeting large herbiores like zbras, wildebeest and buffalo. Their teamwork and strategy during hunts highlight the intelligence and coperation that are key to their survival.";
const submission = new CopyleaksWritingAssistantSubmissionModel(sampleText);
submission.sandbox = true;
copyleaks.writingAssistantClient.submitTextAsync(loginResult, Date.now() + 1, submission)
.then(response => {
logSuccess('TEST_submitWritingAssistText', response);
})
.catch(error => {
logError('TEST_submitWritingAssistText', error);
});
}
function TEST_getCorrectionTypes(loginResult) {
copyleaks.writingAssistantClient.getCorrectionTypesAsync(loginResult, "en")
.then(response => {
logSuccess('TEST_submitAIDetectionNaturalLanguage', response);
})
.catch(error => {
logError('TEST_submitAIDetectionNaturalLanguage', error);
});
}
function logError(title, err) {
console.error('----------ERROR----------');
console.error(`${title}:`);
console.error(err);
console.error('-------------------------');
}
function logSuccess(title, result) {
console.log('----------SUCCESS----------');
console.log(`${title}`);
if (result) {
console.log(`result:`);
console.log(result);
}
console.log('-------------------------');
}
server.listen(port, hostname, () => {
console.log(`please visit http://${hostname}:${port}/ to start the test`);
});