-
Notifications
You must be signed in to change notification settings - Fork 726
/
utils.js
158 lines (139 loc) · 3.97 KB
/
utils.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
const miniget = require('miniget');
/**
* Extract string inbetween another.
*
* @param {string} haystack
* @param {string} left
* @param {string} right
* @returns {string}
*/
exports.between = (haystack, left, right) => {
let pos;
if (left instanceof RegExp) {
const match = haystack.match(left);
if (!match) { return ''; }
pos = match.index + match[0].length;
} else {
pos = haystack.indexOf(left);
if (pos === -1) { return ''; }
pos += left.length;
}
haystack = haystack.slice(pos);
pos = haystack.indexOf(right);
if (pos === -1) { return ''; }
haystack = haystack.slice(0, pos);
return haystack;
};
/**
* Get a number from an abbreviated number string.
*
* @param {string} string
* @returns {number}
*/
exports.parseAbbreviatedNumber = string => {
const match = string
.replace(',', '.')
.replace(' ', '')
.match(/([\d,.]+)([MK]?)/);
if (match) {
let [, num, multi] = match;
num = parseFloat(num);
return Math.round(multi === 'M' ? num * 1000000 :
multi === 'K' ? num * 1000 : num);
}
return null;
};
/**
* Match begin and end braces of input JSON, return only json
*
* @param {string} mixedJson
* @returns {string}
*/
exports.cutAfterJSON = mixedJson => {
let open, close;
if (mixedJson[0] === '[') {
open = '[';
close = ']';
} else if (mixedJson[0] === '{') {
open = '{';
close = '}';
}
if (!open) {
throw new Error(`Can't cut unsupported JSON (need to begin with [ or { ) but got: ${mixedJson[0]}`);
}
// States if the loop is currently in a string
let isString = false;
// Current open brackets to be closed
let counter = 0;
let i;
for (i = 0; i < mixedJson.length; i++) {
// Toggle the isString boolean when leaving/entering string
if (mixedJson[i] === '"' && mixedJson[i - 1] !== '\\') {
isString = !isString;
continue;
}
if (isString) continue;
if (mixedJson[i] === open) {
counter++;
} else if (mixedJson[i] === close) {
counter--;
}
// All brackets have been closed, thus end of JSON is reached
if (counter === 0) {
// Return the cut JSON
return mixedJson.substr(0, i + 1);
}
}
// We ran through the whole string and ended up with an unclosed bracket
throw Error("Can't cut unsupported JSON (no matching closing bracket found)");
};
/**
* Checks if there is a playability error.
*
* @param {Object} player_response
* @param {Array.<string>} statuses
* @param {Error} ErrorType
* @returns {!Error}
*/
exports.playError = (player_response, statuses, ErrorType = Error) => {
let playability = player_response && player_response.playabilityStatus;
if (playability && statuses.includes(playability.status)) {
return new ErrorType(playability.reason || (playability.messages && playability.messages[0]));
}
return null;
};
/**
* Temporary helper to help deprecating a few properties.
*
* @param {Object} obj
* @param {string} prop
* @param {Object} value
* @param {string} oldPath
* @param {string} newPath
*/
exports.deprecate = (obj, prop, value, oldPath, newPath) => {
Object.defineProperty(obj, prop, {
get: () => {
console.warn(`\`${oldPath}\` will be removed in a near future release, ` +
`use \`${newPath}\` instead.`);
return value;
},
});
};
// Check for updates.
const package = require('../package.json');
exports.lastUpdateCheck = 0;
exports.checkForUpdates = () => {
if (!process.env.YTDL_NO_UPDATE && !package.version.startsWith('0.0.0-') &&
Date.now() - exports.lastUpdateCheck >= 1000 * 60 * 60 * 12) {
exports.lastUpdateCheck = Date.now();
return miniget('https://api.github.com/repos/fent/node-ytdl-core/releases/latest', {
headers: { 'User-Agent': 'ytdl-core' },
}).text().then(response => {
if (JSON.parse(response).tag_name !== `v${package.version}`) {
console.warn('\x1b[33mWARNING:\x1B[0m ytdl-core is out of date! Update with "npm install ytdl-core@latest".');
}
});
}
return null;
};