-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add Facebook Marketplace embeds #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a4e8dd
feat(platforms): parse marketplace listings
rosethornbush f073957
fix(platforms): match marketplace error shape
rosethornbush 3326d31
feat(bot): gate marketplace with PostHog
rosethornbush a00e55a
feat(bot): render marketplace listings
rosethornbush ac66d7e
fix(platforms): show marketplace location circle
rosethornbush 0e0b477
fix(bot): preserve marketplace field narrowing
rosethornbush File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
30 changes: 30 additions & 0 deletions
30
packages/platforms/src/platforms/facebook-marketplace.d.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| export interface MarketplaceListing { | ||
| id: string; | ||
| marketplace_listing_title: string; | ||
| redacted_description?: { text: string } | null; | ||
| creation_time: number; | ||
| formatted_price?: { text: string } | null; | ||
| listing_price?: { formatted_amount_zeros_stripped?: string } | null; | ||
| location_text?: { text: string } | null; | ||
| location?: { latitude: number; longitude: number } | null; | ||
| } | ||
|
|
||
| export interface MarketplacePhotos { | ||
| id: string; | ||
| listing_photos: Array<{ | ||
| image: { uri: string }; | ||
| accessibility_caption?: string; | ||
| }>; | ||
| } | ||
|
|
||
| export interface MarketplaceData { | ||
| listing: MarketplaceListing; | ||
| photos: MarketplacePhotos; | ||
| mapTemplate?: string; | ||
| } | ||
|
|
||
| export interface MarketplaceMeta { | ||
| price?: string; | ||
| location?: string; | ||
| map?: string; | ||
| } |
126 changes: 126 additions & 0 deletions
126
packages/platforms/src/platforms/facebook-marketplace.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import * as cheerio from "cheerio"; | ||
|
|
||
| import type { Platform } from "../types"; | ||
| import type { | ||
| MarketplaceData, | ||
| MarketplaceListing, | ||
| MarketplaceMeta, | ||
| MarketplacePhotos, | ||
| } from "./facebook-marketplace.d"; | ||
|
|
||
| const MATCH_RE = | ||
| /^(?:https?:\/\/)?(?:www\.|m\.)?facebook\.com\/marketplace\/item\/(\d+)\/?(?:[?#].*)?$/; | ||
| const LISTING_PREFIX = "adp_MarketplacePDPContainerQueryRelayPreloader_"; | ||
| const PHOTOS_PREFIX = "adp_MarketplacePDPC2CMediaViewerWithImagesQueryRelayPreloader_"; | ||
|
|
||
| export const FacebookMarketplace: Platform< | ||
| "FacebookMarketplace", | ||
| MarketplaceData, | ||
| MarketplaceMeta | ||
| > = { | ||
| type: "FacebookMarketplace", | ||
| async match(url) { | ||
| return url.match(MATCH_RE)?.[1] ?? null; | ||
| }, | ||
| async fetch(id, env) { | ||
| if (!/^\d+$/.test(id)) { | ||
| throw { code: 400, message: "Invalid Marketplace listing ID" }; | ||
| } | ||
| const response = await fetch(`https://www.facebook.com/marketplace/item/${id}/`, { | ||
| headers: { | ||
| "User-Agent": env?.EMBED_USER_AGENT ?? "", | ||
| Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", | ||
| "Accept-Language": "en-US,en;q=0.9", | ||
| "Sec-Fetch-Dest": "document", | ||
| "Sec-Fetch-Mode": "navigate", | ||
| "Sec-Fetch-Site": "none", | ||
| "Sec-Fetch-User": "?1", | ||
| }, | ||
| }); | ||
| if (!response.ok) throw { code: response.status, message: response.statusText }; | ||
|
|
||
| const $ = cheerio.load(await response.text()); | ||
| let listing: MarketplaceListing | undefined; | ||
| let photos: MarketplacePhotos | undefined; | ||
| let mapTemplate: string | undefined; | ||
|
|
||
| for (const element of $('script[type="application/json"][data-sjs]').toArray()) { | ||
| const text = $(element).text(); | ||
| if ( | ||
| !text.includes(LISTING_PREFIX) && | ||
| !text.includes(PHOTOS_PREFIX) && | ||
| !text.includes('"TilesMapConfig"') | ||
| ) | ||
| continue; | ||
| let data; | ||
| try { | ||
| data = JSON.parse(text); | ||
| } catch { | ||
| throw { code: 500, message: "Failed to parse Marketplace data" }; | ||
| } | ||
| for (const requireItem of data.require ?? []) { | ||
| if (requireItem[0] !== "ScheduledServerJS") continue; | ||
| for (const payload of requireItem[3] ?? []) { | ||
| const box = payload.__bbox; | ||
| for (const definition of box?.define ?? []) { | ||
| if (definition[0] === "TilesMapConfig") { | ||
| mapTemplate = definition[2]?.STATIC_MAP_URL_TEMPLATE; | ||
| } | ||
| } | ||
| for (const item of box?.require ?? []) { | ||
| if (item[0] !== "RelayPrefetchedStreamCache") continue; | ||
| const [key, value] = item[3]; | ||
| const target = | ||
| value?.__bbox?.result?.data?.viewer?.marketplace_product_details_page?.target; | ||
| if (target?.id !== id) continue; | ||
| if (key.startsWith(LISTING_PREFIX)) { | ||
| // SAFETY: this preloader's target matches the requested listing ID and observed listing shape. | ||
| listing = target as MarketplaceListing; | ||
| } | ||
| if (key.startsWith(PHOTOS_PREFIX)) { | ||
| // SAFETY: this preloader's target matches the requested listing ID and observed photo shape. | ||
| photos = target as MarketplacePhotos; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (!listing || !photos) { | ||
| throw { code: 500, message: "Marketplace listing data is unavailable" }; | ||
| } | ||
| return { listing, photos, mapTemplate }; | ||
| }, | ||
| async transform({ listing, photos, mapTemplate }) { | ||
| const media = photos.listing_photos.map((photo) => ({ | ||
| url: photo.image.uri, | ||
| type: "photo", | ||
| description: photo.accessibility_caption, | ||
| })); | ||
| let map: string | undefined; | ||
| if (mapTemplate && listing.location) { | ||
| const url = new URL(mapTemplate); | ||
| url.searchParams.set("size", "600x180"); | ||
| url.searchParams.set("scale", "2"); | ||
| url.searchParams.set("zoom", "11"); | ||
| url.searchParams.set("language", "en_US"); | ||
| url.searchParams.set("center", `${listing.location.latitude},${listing.location.longitude}`); | ||
| url.searchParams.set( | ||
| "circle", | ||
| `weight:2|color:0x4D6AA47f|fillcolor:0x4D6AA41c|${listing.location.latitude},${listing.location.longitude}|2k`, | ||
| ); | ||
| map = url.href; | ||
| } | ||
| return { | ||
| platform: this.type, | ||
| author: { name: listing.marketplace_listing_title, avatar: media[0]?.url ?? "" }, | ||
| url: `https://www.facebook.com/marketplace/item/${listing.id}/`, | ||
| text: listing.redacted_description?.text, | ||
| timestamp: listing.creation_time, | ||
| price: | ||
| listing.formatted_price?.text ?? listing.listing_price?.formatted_amount_zeros_stripped, | ||
| location: listing.location_text?.text, | ||
| map, | ||
| media, | ||
| }; | ||
| }, | ||
| }; |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a user lacks Marketplace access, this filter removes the valid match without recording why. Message requests then receive no response, interactions incorrectly report that no platform matched, and message updates can leave the existing bot embed stale because its update target is skipped. Handle denied matches explicitly so users receive an access-related result and updates are not silently abandoned.
Prompt To Fix With AI