1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.extract = extract;
|
9 |
|
10 | var _vm = _interopRequireDefault(require("vm"));
|
11 |
|
12 | var _url = _interopRequireDefault(require("url"));
|
13 |
|
14 | var _request = _interopRequireDefault(require("request"));
|
15 |
|
16 | var _cheerio = _interopRequireDefault(require("cheerio"));
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | async function requestP(req, options) {
|
26 | const r = await new Promise((resolve, reject) => {
|
27 | req(options, (error, response, body) => {
|
28 | if (error) {
|
29 | reject(error);
|
30 | return;
|
31 | }
|
32 |
|
33 | resolve({
|
34 | response,
|
35 | body
|
36 | });
|
37 | });
|
38 | });
|
39 | return r;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function extractScripts(html) {
|
50 | const r = [];
|
51 |
|
52 | const $ = _cheerio.default.load(html);
|
53 |
|
54 | $('script').each((elI, el) => {
|
55 | const data = $(el).html();
|
56 |
|
57 | if (data) {
|
58 | r.push(data);
|
59 | }
|
60 | });
|
61 | return r;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function extractScript(script) {
|
72 | let result = null;
|
73 |
|
74 | if (!script.includes('dlbutton')) {
|
75 | return result;
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | const ctx = _vm.default.createContext(Object.create(null));
|
82 |
|
83 | const runOpts = {
|
84 | timeout: 1000
|
85 | };
|
86 |
|
87 | const codePre = [
|
88 |
|
89 | 'window = this;', 'document = (function(r) {', 'var elements = {', '"dlbutton": {},', '"fimage": {}', '};', 'r.getElementById = function(id) {', 'return elements[id];', '}', 'return r;', '})({});'
|
90 |
|
91 | ].join('\n');
|
92 |
|
93 | const codePost = [
|
94 |
|
95 | 'JSON.stringify({', '"dlbutton": document.getElementById("dlbutton").href', '})'
|
96 |
|
97 | ].join('\n');
|
98 |
|
99 | try {
|
100 |
|
101 | _vm.default.runInContext(codePre, ctx, runOpts);
|
102 |
|
103 |
|
104 | _vm.default.runInContext(script, ctx, runOpts);
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | result = JSON.parse('' + _vm.default.runInContext(codePost, ctx, runOpts));
|
111 | } catch (err) {
|
112 | }
|
113 |
|
114 | return result;
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | async function extract(uri, req = null) {
|
126 | const requester = req || _request.default;
|
127 | const {
|
128 | response,
|
129 | body
|
130 | } = await requestP(requester, {
|
131 | url: uri
|
132 | });
|
133 | const {
|
134 | statusCode
|
135 | } = response;
|
136 |
|
137 | if (statusCode !== 200) {
|
138 | throw new Error(`Invalid status code: ${statusCode}`);
|
139 | }
|
140 |
|
141 | const bodyType = typeof body;
|
142 |
|
143 | if (bodyType !== 'string') {
|
144 | throw new Error(`Invalid body type: ${bodyType}`);
|
145 | }
|
146 |
|
147 | const scripts = extractScripts(body);
|
148 | let result = null;
|
149 |
|
150 | for (const script of scripts) {
|
151 | result = extractScript(script);
|
152 |
|
153 | if (result) {
|
154 | break;
|
155 | }
|
156 | }
|
157 |
|
158 | if (!result || !result.dlbutton) {
|
159 | throw new Error('Failed to extract info');
|
160 | }
|
161 |
|
162 | const download = _url.default.resolve(uri, result.dlbutton);
|
163 |
|
164 | const filename = decodeURI((_url.default.parse(download).pathname || '').split('/').pop() || '') || null;
|
165 | return {
|
166 | download,
|
167 | filename
|
168 | };
|
169 | }
|
170 |
|