-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (69 loc) · 2.77 KB
/
index.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
74
75
76
77
78
79
80
81
import fetch from "node-fetch"; // Using ES module import
function extractVideoId(link) {
const match = link.match(
/(?:facebook|fb).*?(?:\/videos\/|\/reel|\/watch\?v=|\/share\/v\/)(\w+)/i
);
return match && match[1] ? match[1] : null;
}
function encodeRequestBody(requestBody) {
return Object.entries(requestBody)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join("&");
}
async function facebook(link, lq = false) {
const videoId = extractVideoId(link);
if (!videoId) {
return JSON.stringify({ error: "Invalid URL!" });
}
const requestBody = {
doc_id: "5279476072161634",
variables: JSON.stringify({
UFI2CommentsProvider_commentsKey: "CometTahoeSidePaneQuery",
caller: "CHANNEL_VIEW_FROM_PAGE_TIMELINE",
displayCommentsContextEnableComment: null,
displayCommentsContextIsAdPreview: null,
displayCommentsContextIsAggregatedShare: null,
displayCommentsContextIsStorySet: null,
displayCommentsFeedbackContext: null,
feedbackSource: 41,
feedLocation: "TAHOE",
focusCommentID: null,
privacySelectorRenderLocation: "COMET_STREAM",
renderLocation: "video_channel",
scale: 1,
streamChainingSection: false,
useDefaultActor: false,
videoChainingContext: null,
videoID: videoId,
}),
server_timestamps: true,
};
try {
const response = await fetch("https://www.facebook.com/api/graphql/", {
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
"Sec-Fetch-Site": "same-origin",
},
body: encodeRequestBody(requestBody),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const text = await response.text();
const json = JSON.parse(text.split("\n")[0]);
if (!json || !json.data || !json.data.video) {
return JSON.stringify({ error: "Video not found or invalid response!" });
}
const { playable_url, playable_url_quality_hd } = json.data.video;
if (!playable_url_quality_hd || lq === true) {
return playable_url ? `${playable_url}&sd_quality` : JSON.stringify({ error: "SD video not available!" });
}
return playable_url_quality_hd;
} catch (error) {
return JSON.stringify({ error: error.message });
}
}
export { facebook }; // Exporting the function using ES module syntax