Create facebook.js
Browse files- lib/facebook.js +114 -0
lib/facebook.js
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import got from 'got'
|
2 |
+
import * as cheerio from 'cheerio'
|
3 |
+
import { FacebookDlArgsSchema, FacebookDlMediaSchema, FacebookDlSchema } from '../types/facebook-v1.js'
|
4 |
+
import { DEFAULT_HEADERS } from './constant.js'
|
5 |
+
|
6 |
+
export async function facebookdl(url) {
|
7 |
+
FacebookDlArgsSchema.parse(arguments)
|
8 |
+
const html = await got('https://fdownloader.net/en', {
|
9 |
+
headers: {
|
10 |
+
...DEFAULT_HEADERS
|
11 |
+
}
|
12 |
+
}).text()
|
13 |
+
const k_url_search = /k_url_search="(.*?)"/.exec(html)![1]
|
14 |
+
const k_exp = /k_exp="(.*?)"/.exec(html)![1]
|
15 |
+
const k_token = /k_token="(.*?)"/.exec(html)![1]
|
16 |
+
const k_prefix_name = /k_prefix_name="(.*?)"/.exec(html)![1]
|
17 |
+
|
18 |
+
const form = {
|
19 |
+
k_exp,
|
20 |
+
k_token,
|
21 |
+
q: url,
|
22 |
+
lang: 'en',
|
23 |
+
web: 'fdownloader.net',
|
24 |
+
v: 'v2',
|
25 |
+
w: ''
|
26 |
+
}
|
27 |
+
const data = await got.post(k_url_search, {
|
28 |
+
headers: {
|
29 |
+
...DEFAULT_HEADERS,
|
30 |
+
referer: 'https://fdownloader.net/'
|
31 |
+
},
|
32 |
+
form
|
33 |
+
}).json<{ data: string }>()
|
34 |
+
|
35 |
+
const $ = cheerio.load(data.data)
|
36 |
+
|
37 |
+
const k_url_convert = /k_url_convert = "(.*?)"/.exec($.html())![1]
|
38 |
+
const c_exp = /k_exp = "(.*?)"/.exec($.html())![1]
|
39 |
+
const c_token = /c_token = "(.*?)"/.exec($.html())![1]
|
40 |
+
|
41 |
+
const thumbnail = $('.thumbnail > .image-fb > img').attr('src')
|
42 |
+
const duration = $('.content > .clearfix > p').text() || undefined
|
43 |
+
const video = $('table.table').eq(0).find('tbody > tr').map((_, el) => {
|
44 |
+
const $el = $(el)
|
45 |
+
const $td = $el.find('td')
|
46 |
+
const quality = $td.eq(0).text()
|
47 |
+
const url = $td.eq(2).find('a').attr('href')
|
48 |
+
if (url) {
|
49 |
+
return {
|
50 |
+
quality,
|
51 |
+
download: () => Promise.resolve(url)
|
52 |
+
}
|
53 |
+
}
|
54 |
+
// TODO:
|
55 |
+
return false
|
56 |
+
const $button = $td.eq(2).find('button')
|
57 |
+
const ftype = 'mp4'
|
58 |
+
const v_id = $('#FbId').attr('value')
|
59 |
+
const videoUrl = $button.attr('data-videourl')
|
60 |
+
const videoType = $button.attr('data-videotype')
|
61 |
+
const videoCodec = $button.attr('data-videocodec')
|
62 |
+
const fquality = $button.attr('data-fquality')
|
63 |
+
const audioUrl = $('#audioUrl').attr('value')
|
64 |
+
const audioType = $('#audioType').attr('value')
|
65 |
+
}).toArray().filter(Boolean)
|
66 |
+
|
67 |
+
const audio: FacebookDlMediaSchema = []
|
68 |
+
const audioUrl = $('#audioUrl').attr('value')!
|
69 |
+
audio.push({
|
70 |
+
quality: '7kbps',
|
71 |
+
download: () => Promise.resolve(audioUrl)
|
72 |
+
})
|
73 |
+
|
74 |
+
const result = {
|
75 |
+
thumbnail,
|
76 |
+
duration,
|
77 |
+
video,
|
78 |
+
audio
|
79 |
+
}
|
80 |
+
console.log(result)
|
81 |
+
return FacebookDlSchema.parse(result)
|
82 |
+
}
|
83 |
+
|
84 |
+
export async function convert(
|
85 |
+
url: string,
|
86 |
+
v_id: string,
|
87 |
+
ftype: string,
|
88 |
+
videoUrl: string,
|
89 |
+
videoType: string,
|
90 |
+
videoCodec: string,
|
91 |
+
audioUrl: string,
|
92 |
+
audioType: string,
|
93 |
+
fquality: string,
|
94 |
+
fname: string,
|
95 |
+
exp: string,
|
96 |
+
token: string,
|
97 |
+
) {
|
98 |
+
const data = await got.post(url, {
|
99 |
+
form: {
|
100 |
+
ftype,
|
101 |
+
v_id,
|
102 |
+
videoUrl,
|
103 |
+
videoType,
|
104 |
+
videoCodec,
|
105 |
+
audioUrl,
|
106 |
+
audioType,
|
107 |
+
fquality,
|
108 |
+
fname,
|
109 |
+
exp,
|
110 |
+
token,
|
111 |
+
cv: 'v2'
|
112 |
+
}
|
113 |
+
})
|
114 |
+
}
|