feat: add Facebook video extractor#38
Merged
stefanodvx merged 1 commit intogovdbot:mainfrom Mar 13, 2026
Merged
Conversation
Add a new extractor for Facebook video URLs, supporting:
- /reel/<id> (Reels)
- /videos/<id> and /video/<id>
- /watch/?v=<id> (Watch, with any query parameter order)
- /posts/<id> and /<user>/videos/<id> variants
- /share/r|v|p/<id> (Share short links, resolved via redirect)
- mobile (m.facebook.com) and mbasic variants
## Architecture
Follows the standard extractor structure (`main.go`, `models.go`,
`util.go`) consistent with existing extractors like TikTok, Twitter,
and Instagram.
### Files
- `main.go`: defines two extractors:
- `ShareExtractor` — handles /share/ URLs by following the redirect
to the canonical Facebook URL, then delegating to the main extractor.
- `Extractor` — handles all other video URL patterns. Requires auth
cookies (loaded automatically from `private/cookies/facebook.txt`
in Netscape format, same mechanism used by Twitter).
- `models.go`: minimal `VideoData` struct holding HD/SD URLs, title,
and dimensions.
- `util.go`: core scraping logic. Fetches the Facebook page HTML and
extracts video URLs from the embedded JSON data using regex patterns
matching `progressive_url` entries with HD/SD quality metadata.
### Key design decisions
**Video-ID-anchored extraction**: Facebook pages embed multiple videos
(feed recommendations, related content) in a single HTML response.
A naive global regex match on `progressive_url` returns the FIRST
occurrence, which often belongs to a different video. The
`findVideoSection()` function solves this by:
1. Locating the `dash_mpd_debug.mpd?v=<videoID>` anchor specific to
the requested video's delivery data block.
2. Bounding the search to the section ending at `"id":"<videoID>"`.
3. Applying HD/SD regex only within this scoped section.
4. Falling back to full-body search for pages with a single video.
**Watch URL normalization**: `/watch/?v=XXX` pages return inconsistent
video data when scraped. These URLs are converted to `/reel/XXX` which
yields reliable, single-video pages.
## Compliance
- Standard 3-file extractor structure (main.go, models.go, util.go)
- Cookie loading via existing `private/cookies/<id>.txt` mechanism
- Registered in `internal/extractors/main.go` (ShareExtractor first
to match /share/ URLs before the general pattern)
- Conventional Commits style
- No new dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new extractor for downloading videos from Facebook, supporting all common URL formats:
facebook.com/reel/<id>facebook.com/videos/<id>,facebook.com/<user>/videos/<id>facebook.com/watch/?v=<id>(withv=in any query parameter position)facebook.com/posts/<id>,facebook.com/<user>/posts/<id>facebook.com/share/r|v|p/<id>(resolved via HTTP redirect)m.facebook.comandmbasic.facebook.comvariantsHow it works
The extractor fetches the Facebook page HTML using auth cookies (required — loaded automatically from
private/cookies/facebook.txtin Netscape format, same mechanism already used by the Twitter extractor) and a Chrome-like User-Agent.Video URLs are extracted from the embedded JSON data in the HTML by matching
progressive_urlentries with"quality": "HD"or"quality": "SD"metadata. Both HD and SD formats are provided when available, letting the downstream format selector choose the best quality.Video-ID-anchored extraction
Facebook pages embed data for multiple videos in a single HTML response (feed recommendations, related content, etc.). A naive global regex match on
progressive_urlwould return the first occurrence, which often belongs to a different video than the one requested.To solve this, the
findVideoSection()function scopes the regex search to the correct video's data block:dash_mpd_debug.mpd?v=<videoID>anchor — unique per video within thevideoDeliveryResponseResultJSON structure"id":"<videoID>"Watch URL normalization
/watch/?v=XXXpages return HTML with inconsistent/misleading video data when scraped. These URLs are normalized to/reel/XXXbefore fetching, which yields reliable single-video pages. This is transparent to the user — they submit a watch URL and get the correct video.Share link handling
ShareExtractoris registered before the mainExtractorto match/share/r|v|p/<id>URLs first. It follows the HTTP redirect to the canonical Facebook URL, then returns it for re-matching against the main extractor pattern.Files
internal/extractors/facebook/main.goShareExtractor+Extractordefinitions,GetMedia(),buildMedia()internal/extractors/facebook/models.goVideoDatastruct (HD/SD URLs, title, dimensions)internal/extractors/facebook/util.goGetVideoData(),parseVideoFromBody(),findVideoSection(), URL/Unicode unescape helpersinternal/extractors/main.goExtractorssliceCompliance with project standards
main.go,models.go,util.go) used by all existing extractorsprivate/cookies/<id>.txtNetscape format mechanism (same as Twitter)internal/extractors/main.gowithShareExtractorbeforeExtractor(order matters for URL matching)feat: ...)bytes,regexp,strings, and existing project packages*_test.gofiles exist in the project)Configuration
Requires a
private/cookies/facebook.txtfile with valid Facebook session cookies in Netscape format. The following cookies are needed:datr,sb,c_user,fr,xs.Additionally, it is recommended to enable TLS fingerprint impersonation (
impersonate: true) in the extractor YAML config to avoid detection by Facebook's bot protection.