anonmp4 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/index.js +108 -0
- package/package.json +25 -0
package/README.md
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ░█████╗░███╗░░██╗░█████╗░███╗░░██╗███╗░░░███╗██████╗░░░██╗██╗
|
|
3
|
+
* ██╔══██╗████╗░██║██╔══██╗████╗░██║████╗░████║██╔══██╗░██╔╝██║
|
|
4
|
+
* ███████║██╔██╗██║██║░░██║██╔██╗██║██╔████╔██║██████╔╝██╔╝░██║
|
|
5
|
+
* ██╔══██║██║╚████║██║░░██║██║╚████║██║╚██╔╝██║██╔═══╝░███████║
|
|
6
|
+
* ██║░░██║██║░╚███║╚█████╔╝██║░╚███║██║░╚═╝░██║██║░░░░░╚════██║
|
|
7
|
+
* ╚═╝░░╚═╝╚═╝░░╚══╝░╚════╝░╚═╝░░╚══╝╚═╝░░░░░╚═╝╚═╝░░░░░░░░░░╚═╝
|
|
8
|
+
*
|
|
9
|
+
* Don’t scrape AnonMP4.
|
|
10
|
+
* Servers and maintenance cost real money — keep the project alive by supporting it.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export class AnonMP4 {
|
|
14
|
+
constructor(baseUrl = 'https://anonmp4api.xyz') {
|
|
15
|
+
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async upload(input, options = {}) {
|
|
19
|
+
const formData = new FormData();
|
|
20
|
+
let fileName = options.fileName || 'video.mp4';
|
|
21
|
+
let body = input;
|
|
22
|
+
|
|
23
|
+
const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
|
|
24
|
+
|
|
25
|
+
if (isNode && typeof input === 'string') {
|
|
26
|
+
const { createReadStream, existsSync, statSync } = await import('fs');
|
|
27
|
+
const { resolve, basename } = await import('path');
|
|
28
|
+
|
|
29
|
+
const resolvedPath = resolve(input);
|
|
30
|
+
if (!existsSync(resolvedPath)) {
|
|
31
|
+
throw new Error(`File not found: ${resolvedPath}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fileName = basename(resolvedPath);
|
|
35
|
+
statSync(resolvedPath);
|
|
36
|
+
body = createReadStream(resolvedPath);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (isNode && (body && typeof body.pipe === 'function' || Buffer.isBuffer(body))) {
|
|
40
|
+
const chunks = [];
|
|
41
|
+
for await (const chunk of body) {
|
|
42
|
+
chunks.push(chunk);
|
|
43
|
+
}
|
|
44
|
+
body = new Blob([Buffer.concat(chunks)], { type: 'video/mp4' });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
formData.append('file', body, fileName);
|
|
48
|
+
|
|
49
|
+
if (!isNode && options.onProgress && (input instanceof File || input instanceof Blob)) {
|
|
50
|
+
return this._uploadXHR(`${this.baseUrl}/upload`, formData, input.size, options.onProgress);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const response = await fetch(`${this.baseUrl}/upload`, {
|
|
54
|
+
method: 'POST',
|
|
55
|
+
body: formData
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const json = await response.json();
|
|
59
|
+
if (!response.ok || json.success === false) {
|
|
60
|
+
throw json;
|
|
61
|
+
}
|
|
62
|
+
return json;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
_uploadXHR(url, formData, totalSize, onProgress) {
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const xhr = new XMLHttpRequest();
|
|
68
|
+
xhr.open('POST', url, true);
|
|
69
|
+
|
|
70
|
+
xhr.upload.onprogress = (event) => {
|
|
71
|
+
if (event.lengthComputable && onProgress) {
|
|
72
|
+
onProgress({
|
|
73
|
+
loaded: event.loaded,
|
|
74
|
+
total: event.total,
|
|
75
|
+
percentage: ((event.loaded / event.total) * 100).toFixed(2)
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
xhr.onload = () => {
|
|
81
|
+
try {
|
|
82
|
+
const json = JSON.parse(xhr.responseText);
|
|
83
|
+
if (xhr.status >= 400 || json.success === false) {
|
|
84
|
+
reject(json);
|
|
85
|
+
} else {
|
|
86
|
+
resolve(json);
|
|
87
|
+
}
|
|
88
|
+
} catch (e) {
|
|
89
|
+
reject(new Error(`Invalid JSON response: ${xhr.responseText}`));
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
xhr.onerror = () => reject(new Error('Network error during upload'));
|
|
94
|
+
xhr.send(formData);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async getInfo(videoId) {
|
|
99
|
+
const response = await fetch(`${this.baseUrl}/info/${videoId}`);
|
|
100
|
+
const json = await response.json();
|
|
101
|
+
if (!response.ok || json.success === false) {
|
|
102
|
+
throw json;
|
|
103
|
+
}
|
|
104
|
+
return json;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default AnonMP4;
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "anonmp4",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Isomorphic SDK for AnonMP4 API",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"anonmp4",
|
|
11
|
+
"video",
|
|
12
|
+
"upload",
|
|
13
|
+
"api",
|
|
14
|
+
"isomorphic"
|
|
15
|
+
],
|
|
16
|
+
"author": "",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": "./index.js",
|
|
22
|
+
"default": "./index.js"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|