services: add facebook support (#403)

* feat: add facebook support

* chore: fix fail check

* chore: minor fix

* chore: add service in README.md

* chore: cleaning post-merge code

* facebook: add shared link pattern

* chore: clean up removing unnecessarily code

* fix: facebook shared link pattern

* matchActionDecider: redirect to facebook video instead of rendering

* facebook: pass sourceUrl in object

* url: fix botched lint

* fix: facebook shared link pattern with clean up

* test: change facebook test response to redirect

---------

Co-authored-by: dumbmoron <log@riseup.net>
This commit is contained in:
Brama Udi 2024-07-24 22:05:21 +07:00 committed by GitHub
parent 31e1fa5c5c
commit c77ee2eb44
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 159 additions and 2 deletions

View file

@ -19,6 +19,7 @@ this list is not final and keeps expanding over time. if support for a service y
| bilibili.com & bilibili.tv | ✅ | ✅ | ✅ | | | | bilibili.com & bilibili.tv | ✅ | ✅ | ✅ | | |
| dailymotion | ✅ | ✅ | ✅ | ✅ | ✅ | | dailymotion | ✅ | ✅ | ✅ | ✅ | ✅ |
| instagram posts & reels | ✅ | ✅ | ✅ | | | | instagram posts & reels | ✅ | ✅ | ✅ | | |
| facebook videos | ✅ | ❌ | ❌ | | |
| loom | ✅ | ❌ | ✅ | ✅ | | | loom | ✅ | ❌ | ✅ | ✅ | |
| ok video | ✅ | ❌ | ✅ | ✅ | ✅ | | ok video | ✅ | ❌ | ✅ | ✅ | ✅ |
| pinterest | ✅ | ✅ | ✅ | | | | pinterest | ✅ | ✅ | ✅ | | |
@ -45,6 +46,7 @@ this list is not final and keeps expanding over time. if support for a service y
| service | notes or features | | service | notes or features |
| :-------- | :----- | | :-------- | :----- |
| instagram | supports reels, photos, and videos. lets you pick what to save from multi-media posts. | | instagram | supports reels, photos, and videos. lets you pick what to save from multi-media posts. |
| facebook | supports public accessible videos content only. |
| pinterest | supports photos, gifs, videos and stories. | | pinterest | supports photos, gifs, videos and stories. |
| reddit | supports gifs and videos. | | reddit | supports gifs and videos. |
| rutube | supports yappy & private links. | | rutube | supports yappy & private links. |

View file

@ -25,6 +25,7 @@ import twitch from "./services/twitch.js";
import rutube from "./services/rutube.js"; import rutube from "./services/rutube.js";
import dailymotion from "./services/dailymotion.js"; import dailymotion from "./services/dailymotion.js";
import loom from "./services/loom.js"; import loom from "./services/loom.js";
import facebook from "./services/facebook.js";
let freebind; let freebind;
@ -192,6 +193,11 @@ export default async function(host, patternMatch, lang, obj) {
r = await loom({ r = await loom({
id: patternMatch.id id: patternMatch.id
}); });
case "facebook":
r = await facebook({
...patternMatch,
sourceUrl: url.href
});
break; break;
default: default:
return createResponse("error", { return createResponse("error", {

View file

@ -130,6 +130,7 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di
params = { type: "bridge" }; params = { type: "bridge" };
break; break;
case "facebook":
case "vine": case "vine":
case "instagram": case "instagram":
case "tumblr": case "tumblr":

View file

@ -0,0 +1,62 @@
import { genericUserAgent } from "../../config.js";
const headers = {
'User-Agent': genericUserAgent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
}
function resolveUrl(url) {
return fetch(url, { headers })
.then(r => {
if (r.headers.get('location')) {
return decodeURIComponent(r.headers.get('location'))
}
if (r.headers.get('link')) {
const linkMatch = r.headers.get('link').match(/<(.*?)\/>/)
return decodeURIComponent(linkMatch[1])
}
return false
})
.catch(() => false)
}
export default async function({ sourceUrl, shortLink, username, id }) {
const isShortLink = !!shortLink?.length
const isSharedLink = !!sourceUrl.match(/\/share\/\w\//)?.length
let url = isShortLink
? `https://fb.watch/${shortLink}`
: `https://web.facebook.com/${username}/videos/${id}`
if (isShortLink) url = await resolveUrl(url)
if (isSharedLink) url = sourceUrl
const html = await fetch(url, { headers })
.then(r => r.text())
.catch(() => false)
if (!html) return { error: 'ErrorCouldntFetch' };
const urls = []
const hd = html.match('"browser_native_hd_url":(".*?")')
const sd = html.match('"browser_native_sd_url":(".*?")')
if (hd?.[1]) urls.push(JSON.parse(hd[1]))
if (sd?.[1]) urls.push(JSON.parse(sd[1]))
if (!urls.length) {
return { error: 'ErrorEmptyDownload' };
}
let filename = `facebook_${id || shortLink}.mp4`
return {
urls: urls[0],
filename,
audioFilename: `${filename.slice(0, -4)}_audio`,
};
}

View file

@ -118,6 +118,19 @@
"alias": "loom videos", "alias": "loom videos",
"patterns": ["share/:id"], "patterns": ["share/:id"],
"enabled": true "enabled": true
},
"facebook": {
"alias": "facebook videos",
"altDomains": ["fb.watch"],
"subdomains": ["web"],
"patterns": [
"_shortLink/:shortLink",
":username/videos/:caption/:id",
":username/videos/:id",
"reel/:id",
"share/:shortLink/:id"
],
"enabled": true
} }
} }
} }

View file

@ -58,4 +58,10 @@ export const testers = {
"youtube": (patternMatch) => "youtube": (patternMatch) =>
patternMatch.id?.length <= 11, patternMatch.id?.length <= 11,
"facebook": (patternMatch) =>
patternMatch.shortLink?.length <= 11
|| patternMatch.username?.length <= 30
|| patternMatch.caption?.length <= 255
|| patternMatch.id?.length <= 20,
} }

View file

@ -64,7 +64,17 @@ function aliasURL(url) {
if (url.hostname === 'dai.ly' && parts.length === 2) { if (url.hostname === 'dai.ly' && parts.length === 2) {
url = new URL(`https://dailymotion.com/video/${parts[1]}`) url = new URL(`https://dailymotion.com/video/${parts[1]}`)
} }
case "facebook":
case "fb":
if (url.searchParams.get('v')) {
url = new URL(`https://web.facebook.com/user/videos/${url.searchParams.get('v')}`)
}
if (url.hostname === 'fb.watch') {
url = new URL(`https://web.facebook.com/_shortLink/${parts[1]}`)
}
break; break;
case "ddinstagram": case "ddinstagram":
if (services.instagram.altDomains.includes(host.domain) && [null, 'd', 'g'].includes(host.subdomain)) { if (services.instagram.altDomains.includes(host.domain) && [null, 'd', 'g'].includes(host.subdomain)) {
url.hostname = 'instagram.com'; url.hostname = 'instagram.com';

View file

@ -1160,5 +1160,62 @@
"code": 400, "code": 400,
"status": "error" "status": "error"
} }
}],
"facebook": [{
"name": "direct video with username and id",
"url": "https://web.facebook.com/100048111287134/videos/1157798148685638/",
"params": {},
"expected": {
"code": 200,
"status": "redirect"
}
}, {
"name": "direct video with id as query param",
"url": "https://web.facebook.com/watch/?v=883839773514682&ref=sharing",
"params": {},
"expected": {
"code": 200,
"status": "redirect"
}
}, {
"name": "direct video with caption",
"url": "https://web.facebook.com/wood57/videos/𝐒𝐞𝐛𝐚𝐬𝐤𝐨𝐦-𝐟𝐮𝐥𝐥/883839773514682",
"params": {},
"expected": {
"code": 200,
"status": "redirect"
}
}, {
"name": "shortlink video",
"url": "https://fb.watch/r1K6XHMfGT/",
"params": {},
"expected": {
"code": 200,
"status": "redirect"
}
}, {
"name": "reel video",
"url": "https://web.facebook.com/reel/730293269054758",
"params": {},
"expected": {
"code": 200,
"status": "redirect"
}
}, {
"name": "shared video link",
"url": "https://www.facebook.com/share/v/NEf87jbPTvFE8LsL/",
"params": {},
"expected": {
"code": 200,
"status": "redirect"
}
}, {
"name": "shared video link v2",
"url": "https://web.facebook.com/share/r/JFZfPVgLkiJQmWrr/",
"params": {},
"expected": {
"code": 200,
"status": "redirect"
}
}] }]
} }