Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
700 lines (620 loc) · 23.1 KB
/
Copy pathindex.js
File metadata and controls
700 lines (620 loc) · 23.1 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization, Range",
"Access-Control-Expose-Headers": "Content-Length, Content-Range, Content-Disposition"
};
const MAX_DOWNLOAD_BYTES = 650 * 1024 * 1024;
const ENGINES = [
{ name: "engine-1", host: "facebook.com/plugins/video.php", status: "active" },
{ name: "engine-2", host: "prexzyapis.com", status: "active" },
{ name: "engine-3", host: "cobaltapi.cjs.nz", status: "active" },
{ name: "fallback", host: "facebook.com", status: "standby" }
];
function json(data, status = 200) {
return new Response(JSON.stringify(data, null, 2), {
status,
headers: {
"Content-Type": "application/json; charset=utf-8",
...CORS_HEADERS
}
});
}
function sanitizeFilename(name) {
if (!name || typeof name !== "string") return "facebook_media";
return name.replace(/[\\/:*?"<>|\r\n]+/g, "_").trim().slice(0, 100);
}
function formatBytesNumber(bytes) {
if (!bytes || bytes <= 0) return null;
const units = ["Bytes", "KB", "MB", "GB", "TB"];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return `${parseFloat((bytes / Math.pow(1024, i)).toFixed(2))} ${units[i]}`;
}
function cleanFbUrl(u) {
if (!u || typeof u !== "string") return null;
return u
.replace(/\\u0025/g, "%")
.replace(/\\u0026/g, "&")
.replace(/\\\//g, "/")
.replace(/&/g, "&")
.trim();
}
function extractUrl(input) {
if (!input || typeof input !== "string") return null;
const trimmed = input.trim();
if (/^\d{12,22}$/.test(trimmed)) {
return `https://www.facebook.com/watch/?v=${trimmed}`;
}
const match = trimmed.match(/https?:\/\/(?:[a-zA-Z0-9_-]+\.)?(?:facebook\.com|fb\.watch)\/[^\s]+/i);
return match ? match[0] : null;
}
function extractVideoId(targetUrl) {
if (!targetUrl || typeof targetUrl !== "string") return null;
const watchMatch = targetUrl.match(/[?&]v=(\d+)/i);
if (watchMatch) return watchMatch[1];
const pathMatch = targetUrl.match(/\/(?:videos|reel)\/(\d+)/i);
if (pathMatch) return pathMatch[1];
const digitsMatch = targetUrl.match(/\/(\d{12,22})\/?/);
return digitsMatch ? digitsMatch[1] : null;
}
async function resolveRedirects(targetUrl) {
let current = targetUrl;
if (!/fb\.watch|facebook\.com\/share/i.test(current)) {
return current;
}
const testUas = [
"facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)",
"curl/8.5.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
];
for (const ua of testUas) {
try {
let temp = current;
for (let i = 0; i < 5; i++) {
const res = await fetch(temp, {
method: "GET",
redirect: "manual",
headers: {
"User-Agent": ua,
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}
});
if ([301, 302, 303, 307, 308].includes(res.status)) {
const loc = res.headers.get("location");
if (loc) {
temp = new URL(loc, temp).toString();
const directMatch = temp.match(/(https?:\/\/(?:www\.)?facebook\.com\/(?:reel|videos|watch\/\?v=)\/[0-9]+)/i);
if (directMatch) return directMatch[1] + "/";
continue;
}
}
if (res.status === 200) {
const html = await res.text();
const ogUrl = html.match(/property="og:url"\s+content="([^"]+)"/i)
|| html.match(/rel="canonical"\s+href="([^"]+)"/i)
|| html.match(/"video_id":"(\d+)"/i);
if (ogUrl) {
const matchedVal = ogUrl[1];
if (/^\d+$/.test(matchedVal)) return `https://www.facebook.com/watch/?v=${matchedVal}`;
return matchedVal.split("?")[0];
}
}
break;
}
const idMatch = temp.match(/\/(?:reel|videos)\/(\d+)/i) || temp.match(/[?&]v=(\d+)/i);
if (idMatch) {
return `https://www.facebook.com/reel/${idMatch[1]}/`;
}
} catch {}
}
return current;
}
async function probeContentLength(url) {
if (!url || typeof url !== "string") return null;
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 4000);
const res = await fetch(url, {
method: "HEAD",
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Referer": "https://www.facebook.com/"
},
signal: controller.signal
});
clearTimeout(timer);
const cl = res.headers.get("content-length");
if (cl && Number(cl) > 0) return Number(cl);
} catch {}
return null;
}
async function fetchPluginUrl(candidateUrl) {
try {
const pluginUrl = `https://www.facebook.com/plugins/video.php?href=${encodeURIComponent(candidateUrl)}`;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 12000);
const res = await fetch(pluginUrl, {
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
},
signal: controller.signal
});
clearTimeout(timer);
if (!res.ok) return null;
const html = await res.text();
const hdMatch = html.match(/"browser_native_hd_url":"([^"]+)"/) || html.match(/"hd_src":"([^"]+)"/);
const sdMatch = html.match(/"browser_native_sd_url":"([^"]+)"/) || html.match(/"sd_src":"([^"]+)"/);
const thumbMatch = html.match(/"thumb_url":"([^"]+)"/) || html.match(/class="[^"]*videoThumb[^"]*"[^>]+src="([^"]+)"/);
const durMatch = html.match(/"duration_s":([0-9.]+)/);
const hd = cleanFbUrl(hdMatch?.[1]);
const sd = cleanFbUrl(sdMatch?.[1]);
if (!hd && !sd) return null;
return {
status: "active",
host: "facebook.com/plugins/video.php",
hd_url: hd,
sd_url: sd,
thumbnail: cleanFbUrl(thumbMatch?.[1]),
duration: durMatch ? Math.round(Number(durMatch[1])) : 0
};
} catch {
return null;
}
}
async function fetchPluginEngine(videoUrl) {
const primary = await fetchPluginUrl(videoUrl);
if (primary) return primary;
const videoId = extractVideoId(videoUrl);
if (videoId) {
const altWatch = await fetchPluginUrl(`https://www.facebook.com/watch/?v=${videoId}`);
if (altWatch) return altWatch;
const altReel = await fetchPluginUrl(`https://www.facebook.com/reel/${videoId}/`);
if (altReel) return altReel;
}
return null;
}
async function fetchPrexzyEngine(videoUrl) {
const videoId = extractVideoId(videoUrl);
const candidates = [videoUrl];
if (videoId) {
candidates.push(`https://www.facebook.com/watch/?v=${videoId}`);
candidates.push(`https://www.facebook.com/reel/${videoId}/`);
}
for (const candidate of candidates) {
try {
const target = `https://prexzyapis.com/download/aio?url=${encodeURIComponent(candidate)}`;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 12000);
const res = await fetch(target, {
headers: { "User-Agent": "Mozilla/5.0" },
signal: controller.signal
});
clearTimeout(timer);
if (!res.ok) continue;
const d = await res.json();
if (!d || !d.data) continue;
const data = d.data;
const medias = data.medias || [];
const hdMedia = medias.find(m => String(m.quality).toUpperCase() === "HD");
const sdMedia = medias.find(m => String(m.quality).toUpperCase() === "SD") || medias[0];
const hd = cleanFbUrl(hdMedia?.url);
const sd = cleanFbUrl(sdMedia?.url);
if (!hd && !sd) continue;
return {
status: "active",
host: "prexzyapis.com",
title: data.title || "",
duration: data.duration ? Math.round(data.duration / 1000) : 0,
hd_url: hd,
sd_url: sd
};
} catch {}
}
return null;
}
async function fetchCobaltEngine(videoUrl) {
const instances = [
"https://cobaltapi.cjs.nz/",
"https://cobalt.kwiatekm.tokyo/"
];
const videoId = extractVideoId(videoUrl);
const candidates = [videoUrl];
if (videoId) {
candidates.push(`https://www.facebook.com/watch/?v=${videoId}`);
candidates.push(`https://www.facebook.com/reel/${videoId}/`);
}
for (const inst of instances) {
for (const cand of candidates) {
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 9000);
const res = await fetch(inst, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"User-Agent": "Mozilla/5.0"
},
body: JSON.stringify({ url: cand }),
signal: controller.signal
});
clearTimeout(timer);
if (!res.ok) continue;
const d = await res.json();
const streamUrl = cleanFbUrl(d.url);
if (streamUrl) {
return {
status: "active",
host: new URL(inst).hostname,
url: streamUrl,
filename: d.filename || null
};
}
} catch {}
}
}
return null;
}
async function fetchWebFallback(videoUrl) {
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 10000);
const res = await fetch(videoUrl, {
headers: {
"User-Agent": "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
},
signal: controller.signal
});
clearTimeout(timer);
if (!res.ok) return null;
const html = await res.text();
const hdMatch = html.match(/"browser_native_hd_url":"([^"]+)"/)
|| html.match(/"hd_src":"([^"]+)"/)
|| html.match(/"playable_url_quality_hd":"([^"]+)"/);
const sdMatch = html.match(/"browser_native_sd_url":"([^"]+)"/)
|| html.match(/"sd_src":"([^"]+)"/)
|| html.match(/"playable_url":"([^"]+)"/);
const ogTitle = html.match(/<meta\s+property="og:title"\s+content="([^"]*)"/i)
|| html.match(/<title>([^<]*)<\/title>/i);
const ogImage = html.match(/<meta\s+property="og:image"\s+content="([^"]*)"/i);
const hd = cleanFbUrl(hdMatch?.[1]);
const sd = cleanFbUrl(sdMatch?.[1]);
if (!hd && !sd) return null;
return {
status: "active",
host: "facebook.com",
title: ogTitle?.[1] || "",
thumbnail: cleanFbUrl(ogImage?.[1]),
hd_url: hd,
sd_url: sd
};
} catch {
return null;
}
}
async function scrapeFacebook(canonicalUrl, origin) {
const [pluginRes, prexzyRes, cobaltRes, webRes] = await Promise.allSettled([
fetchPluginEngine(canonicalUrl),
fetchPrexzyEngine(canonicalUrl),
fetchCobaltEngine(canonicalUrl),
fetchWebFallback(canonicalUrl)
]);
const plugin = pluginRes.status === "fulfilled" ? pluginRes.value : null;
const prexzy = prexzyRes.status === "fulfilled" ? prexzyRes.value : null;
const cobalt = cobaltRes.status === "fulfilled" ? cobaltRes.value : null;
const web = webRes.status === "fulfilled" ? webRes.value : null;
if (!plugin && !prexzy && !cobalt && !web) {
throw new Error("Unable to retrieve Facebook video streams from available engines");
}
const primaryHd = plugin?.hd_url || prexzy?.hd_url || cobalt?.url || web?.hd_url || null;
const primarySd = plugin?.sd_url || prexzy?.sd_url || web?.sd_url || null;
const rawTitle = prexzy?.title || web?.title || cobalt?.filename || "Facebook Video";
const title = sanitizeFilename(rawTitle.replace(/\s*\|\s*Facebook/gi, "").trim());
const videoId = extractVideoId(canonicalUrl) || "fb_media";
const duration = plugin?.duration || prexzy?.duration || 0;
const thumbnail = plugin?.thumbnail || web?.thumbnail || `https://graph.facebook.com/${videoId}/picture?type=large`;
const [hdBytes, sdBytes] = await Promise.all([
probeContentLength(primaryHd),
probeContentLength(primarySd)
]);
const qualities = [];
const qualitiesMap = {};
if (primaryHd) {
qualitiesMap["HD"] = primaryHd;
qualities.push({
quality: "HD",
resolution: "720p / 1080p",
size: hdBytes,
size_formatted: formatBytesNumber(hdBytes),
download_allowed: hdBytes ? hdBytes <= MAX_DOWNLOAD_BYTES : true,
url: primaryHd,
download_url: `${origin}/api/download?url=${encodeURIComponent(primaryHd)}&filename=${encodeURIComponent(title + "_HD.mp4")}`,
stream_url: `${origin}/api/stream?url=${encodeURIComponent(primaryHd)}`
});
}
if (primarySd) {
qualitiesMap["SD"] = primarySd;
qualities.push({
quality: "SD",
resolution: "360p / 480p",
size: sdBytes,
size_formatted: formatBytesNumber(sdBytes),
download_allowed: sdBytes ? sdBytes <= MAX_DOWNLOAD_BYTES : true,
url: primarySd,
download_url: `${origin}/api/download?url=${encodeURIComponent(primarySd)}&filename=${encodeURIComponent(title + "_SD.mp4")}`,
stream_url: `${origin}/api/stream?url=${encodeURIComponent(primarySd)}`
});
}
const bestMedia = qualities[0] || null;
const primaryVideoUrl = bestMedia?.url || null;
const videoDownloadUrl = primaryVideoUrl
? `${origin}/api/download?url=${encodeURIComponent(primaryVideoUrl)}&filename=${encodeURIComponent(title + ".mp4")}`
: null;
const videoStreamUrl = primaryVideoUrl
? `${origin}/api/stream?url=${encodeURIComponent(primaryVideoUrl)}`
: null;
return {
engine: plugin ? "engine-1 (facebook-plugin)" : (prexzy ? "engine-2 (prexzy-aio)" : "engine-3 (cobalt-edge)"),
id: videoId,
title,
duration,
thumbnail,
max_download_size: MAX_DOWNLOAD_BYTES,
max_download_size_formatted: "650 MB",
engines: {
active: plugin ? "engine-1" : (prexzy ? "engine-2" : "engine-3"),
engine_1: "facebook.com/plugins/video.php",
engine_2: "prexzyapis.com",
engine_3: "cobaltapi.cjs.nz",
fallback: "facebook.com"
},
sources: {
facebook_plugin: plugin ? {
status: "active",
host: "facebook.com/plugins/video.php",
hd_url: plugin.hd_url,
sd_url: plugin.sd_url,
duration: plugin.duration
} : { status: "offline", host: "facebook.com/plugins/video.php" },
prexzy_aio: prexzy ? {
status: "active",
host: "prexzyapis.com",
title: prexzy.title,
duration: prexzy.duration,
hd_url: prexzy.hd_url,
sd_url: prexzy.sd_url
} : { status: "offline", host: "prexzyapis.com" },
cobalt_edge: cobalt ? {
status: "active",
host: cobalt.host,
url: cobalt.url,
filename: cobalt.filename
} : { status: "offline", host: "cobaltapi.cjs.nz" }
},
media: {
video: {
quality: bestMedia?.quality || "HD",
size: bestMedia?.size || null,
size_formatted: bestMedia?.size_formatted || null,
download_allowed: bestMedia?.download_allowed ?? true,
url: primaryVideoUrl,
qualities,
qualities_map: qualitiesMap,
download_url: videoDownloadUrl,
stream_url: videoStreamUrl
}
}
};
}
async function pipeStream(targetUrl, request, filename, asAttachment) {
const upstreamHeaders = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
"Referer": "https://www.facebook.com/"
};
const range = request.headers.get("range");
if (range) {
upstreamHeaders["Range"] = range;
}
const upstream = await fetch(targetUrl, {
method: "GET",
headers: upstreamHeaders
});
const contentLength = upstream.headers.get("content-length");
if (asAttachment && contentLength && Number(contentLength) > MAX_DOWNLOAD_BYTES) {
return json({
status: "error",
code: 413,
message: "File size exceeds the 650 MB download limit",
file_size: Number(contentLength),
file_size_formatted: formatBytesNumber(Number(contentLength)),
max_download_size: MAX_DOWNLOAD_BYTES,
max_download_size_formatted: "650 MB"
}, 413);
}
const responseHeaders = new Headers(CORS_HEADERS);
const isAudioFile = filename.endsWith(".mp3") || filename.endsWith(".m4a");
const fallbackType = isAudioFile ? (filename.endsWith(".mp3") ? "audio/mpeg" : "audio/mp4") : "video/mp4";
const contentType = upstream.headers.get("content-type") || fallbackType;
responseHeaders.set("Content-Type", contentType);
if (contentLength) responseHeaders.set("Content-Length", contentLength);
const contentRange = upstream.headers.get("content-range");
if (contentRange) responseHeaders.set("Content-Range", contentRange);
const acceptRanges = upstream.headers.get("accept-ranges");
if (acceptRanges) responseHeaders.set("Accept-Ranges", acceptRanges);
responseHeaders.set(
"Content-Disposition",
asAttachment ? `attachment; filename="${filename}"` : "inline"
);
responseHeaders.set("Cache-Control", "public, max-age=86400");
let body = upstream.body;
if (asAttachment) {
let bytesTransferred = 0;
const limiter = new TransformStream({
transform(chunk, controller) {
bytesTransferred += chunk.length;
if (bytesTransferred > MAX_DOWNLOAD_BYTES) {
controller.error(new Error("File size exceeds 650 MB limit"));
} else {
controller.enqueue(chunk);
}
}
});
body = body.pipeThrough(limiter);
}
return new Response(body, {
status: upstream.status,
headers: responseHeaders
});
}
async function handleProxy(request, asAttachment) {
const reqUrl = new URL(request.url);
const targetUrl = reqUrl.searchParams.get("url");
const rawId = reqUrl.searchParams.get("id");
const quality = (reqUrl.searchParams.get("quality") || reqUrl.searchParams.get("q") || "HD").toUpperCase();
const customFilename = reqUrl.searchParams.get("filename");
if (targetUrl && /^https?:\/\//i.test(targetUrl)) {
try {
const parsed = new URL(targetUrl);
const isDirectMedia = !parsed.hostname.includes("facebook.com") && !parsed.hostname.includes("fb.watch");
if (isDirectMedia) {
const filename = sanitizeFilename(customFilename || "facebook_video.mp4");
return await pipeStream(parsed.toString(), request, filename, asAttachment);
}
} catch {
return json({ status: "error", message: "Invalid media URL" }, 400);
}
}
const lookupUrl = targetUrl || (rawId ? `https://www.facebook.com/watch/?v=${rawId}` : null);
if (!lookupUrl) {
return json({ status: "error", message: "Missing valid 'url' or 'id' parameter" }, 400);
}
try {
const canonical = await resolveRedirects(lookupUrl);
const data = await scrapeFacebook(canonical, reqUrl.origin);
const qualitiesMap = data.media?.video?.qualities_map || {};
const streamUrl = qualitiesMap[quality] || qualitiesMap["HD"] || qualitiesMap["SD"] || Object.values(qualitiesMap)[0];
if (!streamUrl) {
return json({ status: "error", message: "Could not resolve stream URL for requested video" }, 404);
}
const filename = sanitizeFilename(customFilename || `${data.title || "facebook"}_${quality}.mp4`);
return await pipeStream(streamUrl, request, filename, asAttachment);
} catch (err) {
return json({ status: "error", message: `Proxy error: ${err.message}` }, 500);
}
}
export default {
async fetch(request, env, ctx) {
if (request.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: CORS_HEADERS
});
}
const url = new URL(request.url);
const origin = url.origin;
const path = url.pathname;
if (path === "/" || path === "/api") {
return json({
status: "online",
service: "Facebook Video & Reels Scraper API",
version: "1.1.0",
limits: {
max_download_size: MAX_DOWNLOAD_BYTES,
max_download_size_formatted: "650 MB"
},
engines: ENGINES,
endpoints: {
scrape: {
method: ["GET", "POST"],
path: "/api/scrape",
params: {
url: "Facebook video / reels / watch / share URL, or numeric video ID"
},
example: `${origin}/api/scrape?url=https://www.facebook.com/watch/?v=10153231379946729`
},
download: {
method: "GET",
path: "/api/download",
params: {
url: "Direct stream URL or Facebook post URL",
id: "Optional Facebook video ID",
quality: "HD or SD (default: HD)",
filename: "Optional custom output filename"
},
notes: "Files larger than 650 MB cannot be downloaded via proxy",
example: `${origin}/api/download?id=10153231379946729&quality=HD`
},
stream: {
method: "GET",
path: "/api/stream",
params: {
url: "Direct stream URL or Facebook post URL",
id: "Optional Facebook video ID",
quality: "HD or SD (default: HD)"
},
example: `${origin}/api/stream?id=10153231379946729&quality=HD`
},
health: {
method: "GET",
path: "/health"
}
}
});
}
if (path === "/health") {
return json({ status: "ok", timestamp: new Date().toISOString() });
}
if (path === "/api/download" || path === "/download") {
return handleProxy(request, true);
}
if (path === "/api/stream" || path === "/stream") {
return handleProxy(request, false);
}
if (["/api/scrape", "/scrape", "/api/video", "/api/info"].includes(path)) {
let rawInput = null;
if (request.method === "GET") {
rawInput = url.searchParams.get("url") || url.searchParams.get("id");
} else if (request.method === "POST") {
const contentType = request.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
try {
const body = await request.json();
rawInput = body.url || body.id || body.link;
} catch {
return json({ status: "error", message: "Invalid JSON body" }, 400);
}
} else if (contentType.includes("application/x-www-form-urlencoded")) {
const form = await request.formData();
rawInput = form.get("url") || form.get("id") || form.get("link");
}
} else {
return json({ status: "error", message: "Method not allowed" }, 405);
}
const extracted = extractUrl(rawInput);
if (!extracted) {
return json({
status: "error",
message: "A valid Facebook video URL, reels link, fb.watch shortlink, or video ID is required"
}, 400);
}
try {
const canonical = await resolveRedirects(extracted);
const data = await scrapeFacebook(canonical, origin);
return json({
status: "success",
code: 200,
data
});
} catch (err) {
return json({
status: "error",
code: 500,
message: err.message
}, 500);
}
}
return json({ status: "error", message: "Not found" }, 404);
}
};