recycleactor commited on
Commit
00a1de0
Β·
verified Β·
1 Parent(s): 5540732

Upload server.js

Browse files
Files changed (1) hide show
  1. server.js +29 -28
server.js CHANGED
@@ -59,34 +59,35 @@ function readBody(req) {
59
  });
60
  }
61
 
62
- function fetchRaw(reqUrl, headers) {
63
- return new Promise((resolve, reject) => {
64
- let u; try { u = new URL(reqUrl); } catch(e) { return reject(e); }
65
- const mod = u.protocol === 'https:' ? https : http;
66
- const req = mod.request({
67
- hostname: u.hostname,
68
- port: u.port || (u.protocol === 'https:' ? 443 : 80),
69
- path: u.pathname + u.search,
70
- method: 'GET',
71
- headers: headers || {},
72
- }, res => {
73
- // Handle gzip/deflate encoding
74
- const encoding = res.headers['content-encoding'];
75
- let stream = res;
76
- if (encoding === 'gzip' || encoding === 'deflate') {
77
- const zlib = require('zlib');
78
- stream = encoding === 'gzip' ? res.pipe(zlib.createGunzip()) : res.pipe(zlib.createInflate());
79
- }
80
- let data = '';
81
- stream.setEncoding('utf8');
82
- stream.on('data', c => { data += c; });
83
- stream.on('end', () => resolve(data));
84
- stream.on('error', reject);
85
- });
86
- req.on('error', reject);
87
- req.setTimeout(20000, () => { req.destroy(); reject(new Error('timeout')); });
88
- req.end();
89
- });
 
90
  }
91
 
92
  // ── Cinemar parser ────────────────────────────────────────────────────────────
 
59
  });
60
  }
61
 
62
+ async function fetchRaw(url, headers = {}) {
63
+ let retries = 2;
64
+ while (retries > 0) {
65
+ try {
66
+ return await new Promise((resolve, reject) => {
67
+ const pu = new URL(url);
68
+ const mod = pu.protocol === 'https:' ? https : http;
69
+ const req = mod.get(url, {
70
+ headers: {
71
+ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
72
+ 'connection': 'close',
73
+ ...headers
74
+ },
75
+ timeout: 10000
76
+ }, (res) => {
77
+ let data = '';
78
+ res.on('data', (chunk) => data += chunk);
79
+ res.on('end', () => resolve(data));
80
+ });
81
+ req.on('error', reject);
82
+ req.on('timeout', () => { req.destroy(); reject(new Error('timeout')); });
83
+ });
84
+ } catch (e) {
85
+ console.log(`[fetchRaw] error for ${url}: ${e.message}, retries left: ${retries - 1}`);
86
+ retries--;
87
+ if (retries === 0) throw e;
88
+ await new Promise(r => setTimeout(r, 1000));
89
+ }
90
+ }
91
  }
92
 
93
  // ── Cinemar parser ────────────────────────────────────────────────────────────