-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtools.js
73 lines (63 loc) · 2.13 KB
/
tools.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
const PDFDocument = require('pdfkit');
const getStream = require('get-stream');
const cheerio = require('cheerio');
const axios = require('axios');
const got = require('got');
module.exports = {
get: async (url, option = {}) => {
try {
const response = await axios.get(url, option);
return response;
} catch (err) {
//get code status
const code = err.response.status;
if (code !== 200) {
const res = await axios.get(`https://bypass.kato-rest.us/url/${btoa(url)}`);
return res;
}
}
},
post: async (url, data, option) => {
const response = await axios.post(url, data, option);
return response;
},
generatePDF: async (images) => {
try {
const doc = new PDFDocument({ autoFirstPage: false });
for (const image of images) {
if (image.endsWith(".gif")) continue;
const base64 = btoa(image);
const buffer = await require('got')(`https://bypass.kato-rest.us/url/${base64}`).buffer();
const img = doc.openImage(buffer);
doc.addPage({ size: [img.width, img.height] });
doc.image(img, 0, 0);
};
doc.end();
const pdfStream = await getStream.buffer(doc);
return pdfStream;
} catch (error) {
console.log(error);
return null;
}
},
getVideoSrc: async (url) => {
try {
const response = await axios.get(url);
const data = response.data;
const $ = cheerio.load(data);
let src = null;
let src1 = $("source");
let src2 = data.split("sources: [");
let src3 = $("iframe");
if (src1.length > 0)
src = src1.attr("src");
else if (src2.length > 1)
src = src2[1].split("]")[0].split("'file':")[1].split("'")[1];
else
src = src3.attr("src");
return src;
} catch (error) {
return "-";
}
}
};