-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (54 loc) · 1.94 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
const axios = require('axios');
const async = require('async');
const shortenLink = async (token, link) => {
try {
const response = await axios.get(
'https://cutt.ly/api/api.php',
{
params: {
key: token,
short: link,
},
}
);
const result = response.data;
if (result.url.status === 7) { // Status 7 means success
return { token, link, short_url: result.url.shortLink };
} else {
return { token, link, error: result.url.title || 'Unknown error' };
}
} catch (error) {
return { token, link, error: error.response ? error.response.data : error.message };
}
};
const bulkCuttlyShortener = async (tokens, links) => {
const results = [];
const failedTokens = new Set();
const processLink = async (link) => {
let linkProcessed = false;
let retryTokens = [...tokens];
while (retryTokens.length > 0 && !linkProcessed) {
const token = retryTokens.shift();
if (failedTokens.has(token)) continue;
const result = await shortenLink(token, link);
if (result?.error?.message && (result?.error?.message == "You have reached your monthly link limit. You can upgrade your subscription plan to add more links.")) {
failedTokens.add(token);
continue;
}
if (result.error) {
continue;
}
results.push(result);
linkProcessed = true;
}
};
// Process all links concurrently with a specified limit on the number of concurrent operations
await async.eachLimit(links, 1, processLink);
return {
results: results,
failedTokens: Array.from(failedTokens),
};
};
module.exports = {
bulkCuttlyShortener,
};