@sockethub/platform-metadata 1.0.1-alpha.9 → 1.0.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Site-specific URL resolution for pages that don't serve useful Open Graph
3
+ * data to scrapers.
4
+ *
5
+ * Some large platforms return a generic banner image (X/Twitter), serve
6
+ * their Open Graph data only to recognized embed crawlers (Reddit), or
7
+ * hide post media behind a login (Facebook):
8
+ *
9
+ * - X/Twitter → FxTwitter's JSON API (api.fxtwitter.com), built for embed
10
+ * previews, returns the tweet text plus direct photo/video URLs.
11
+ * - Reddit → the regular scrape works, but only when presented with an
12
+ * embed-crawler user agent (see COMPAT_USER_AGENT in index.ts).
13
+ * - Facebook → same crawler-UA gating as Reddit, plus its video titles
14
+ * arrive polluted with localized engagement stats that need stripping.
15
+ *
16
+ * These are pure URL matchers/mappers — the platform decides what to do
17
+ * with the resolution (call a JSON API vs. pick a scrape user agent).
18
+ */
19
+ /**
20
+ * Match an X/Twitter status URL and return the FxTwitter API URL for it,
21
+ * or null when the URL isn't a status page (profiles, search, home feed —
22
+ * those go through the normal OG scrape).
23
+ *
24
+ * Handles `/<user>/status/<id>` and the `/i/web/status/<id>` share form.
25
+ */
26
+ export declare function resolveTwitterStatus(url: string): string | null;
27
+ /** Resolve supported YouTube video URLs to the official oEmbed endpoint. */
28
+ export declare function resolveYouTubeOEmbed(url: string): string | null;
29
+ export interface YouTubeOEmbed {
30
+ title: string;
31
+ provider_name?: string;
32
+ thumbnail_url: string;
33
+ thumbnail_width?: number;
34
+ thumbnail_height?: number;
35
+ }
36
+ /** Validate the subset of YouTube's external oEmbed payload we consume. */
37
+ export declare function parseYouTubeOEmbed(value: unknown): YouTubeOEmbed | null;
38
+ /** Map a validated YouTube oEmbed thumbnail to the platform image shape. */
39
+ export declare function youtubeOEmbedImage(embed?: YouTubeOEmbed): PageObject["image"] | undefined;
40
+ /**
41
+ * Extract YouTube's full video description from the player bootstrap JSON.
42
+ * This data is embedded in the watch HTML while the Open Graph description is
43
+ * intentionally truncated. Return undefined when the undocumented payload
44
+ * shape changes so callers can safely fall back to Open Graph metadata.
45
+ */
46
+ export declare function extractYouTubeDescription(html: string): string | undefined;
47
+ /**
48
+ * True for Facebook URLs (including fb.watch short links). Facebook serves
49
+ * an unrecognized scraper a login interstitial instead of the post, but
50
+ * serves recognized link-preview crawlers the post's Open Graph data —
51
+ * title, caption, and the video/photo thumbnail — so these URLs scrape
52
+ * with COMPAT_USER_AGENT (see index.ts), like Reddit.
53
+ */
54
+ export declare function isFacebookUrl(url: string): boolean;
55
+ /**
56
+ * Remove the engagement-stats segment Facebook prepends to video titles:
57
+ * "2.2M views · 21K reactions | <video title> | <author>". The stats are
58
+ * localized to the *scraping server's* geo-IP language, so match their
59
+ * shape — a leading pipe-delimited segment containing a digit and the "·"
60
+ * separator — rather than any particular wording.
61
+ */
62
+ export declare function stripFacebookEngagement(title: string | undefined): string | undefined;
63
+ /**
64
+ * True for Reddit URLs (including redd.it short links). Reddit serves its
65
+ * Open Graph tags — with the post's real preview image — only to
66
+ * recognized embed-crawler user agents; everything else gets a page with
67
+ * no OG data (or an outright 403 from datacenter addresses).
68
+ */
69
+ export declare function isRedditUrl(url: string): boolean;
70
+ /** Map a canonical Reddit post URL to Reddit's official media-enabled embed. */
71
+ export declare function resolveRedditEmbed(url: string): string | null;
72
+ /** Map a canonical Reddit post URL to old.reddit.com's public JSON endpoint. */
73
+ export declare function resolveRedditJson(url: string): string | null;
74
+ /**
75
+ * Unwrap Reddit's media-viewer URL (`reddit.com/media?url=<image>`) to the
76
+ * image it displays. Reddit serves neither post JSON nor oEmbed for these
77
+ * links (its oEmbed endpoint returns HTTP 400), but the target image is
78
+ * embedded in the link itself, so no network request is needed at all.
79
+ */
80
+ export declare function resolveRedditMedia(url: string): string | null;
81
+ export interface RedditPost {
82
+ title: string;
83
+ selftext?: string;
84
+ url?: string;
85
+ url_overridden_by_dest?: string;
86
+ secure_media?: {
87
+ reddit_video?: {
88
+ fallback_url?: string;
89
+ width?: number;
90
+ height?: number;
91
+ duration?: number;
92
+ };
93
+ };
94
+ preview?: {
95
+ images?: Array<{
96
+ source?: {
97
+ url?: string;
98
+ width?: number;
99
+ height?: number;
100
+ };
101
+ }>;
102
+ };
103
+ }
104
+ /** Validate and extract the post object from Reddit's listing response. */
105
+ export declare function parseRedditPost(value: unknown): RedditPost | null;
106
+ /** Select only a direct Reddit-hosted image belonging to the post. */
107
+ export declare function redditPostImage(post: RedditPost): PageObject["image"];
108
+ /** Map Reddit's hosted-video payload to the common page video shape. */
109
+ export declare function redditPostVideo(post: RedditPost): PageObject["video"];
110
+ /** Subset of Reddit's oEmbed response used for link previews. */
111
+ export interface RedditOEmbed {
112
+ title?: string;
113
+ author_name?: string;
114
+ provider_name?: string;
115
+ thumbnail_url?: string;
116
+ thumbnail_width?: number;
117
+ thumbnail_height?: number;
118
+ }
119
+ /** Validate the subset of Reddit's untrusted oEmbed JSON that we consume. */
120
+ export declare function parseRedditOEmbed(value: unknown): RedditOEmbed | null;
121
+ /**
122
+ * Convert Reddit's optional oEmbed thumbnail into the platform image shape.
123
+ * A missing or malformed thumbnail deliberately means "no image": Reddit's
124
+ * scraped Open Graph image may be a generic site hero rather than post media.
125
+ */
126
+ export declare function redditOEmbedImage(embed: RedditOEmbed): PageObject["image"] | undefined;
127
+ /** Keep only Reddit-hosted post media, excluding branding/share-card images. */
128
+ export declare function redditPostImages(images: PageObject["image"]): PageObject["image"] | undefined;
129
+ /**
130
+ * Make scraped plain text readable without flattening intentional structure.
131
+ * Preserve line breaks and paragraphs, but remove HTML indentation and cap
132
+ * excessive vertical whitespace at one blank line.
133
+ */
134
+ export declare function normalizeDescription(value: string): string;
135
+ /** Subset of the FxTwitter status API response the platform consumes. */
136
+ export interface FxTwitterStatus {
137
+ code: number;
138
+ message?: string;
139
+ tweet?: {
140
+ url?: string;
141
+ text?: string;
142
+ author?: {
143
+ name?: string;
144
+ screen_name?: string;
145
+ };
146
+ media?: {
147
+ photos?: Array<{
148
+ url?: string;
149
+ width?: number;
150
+ height?: number;
151
+ }>;
152
+ videos?: Array<{
153
+ url?: string;
154
+ thumbnail_url?: string;
155
+ width?: number;
156
+ height?: number;
157
+ duration?: number;
158
+ }>;
159
+ };
160
+ article?: {
161
+ title?: string;
162
+ preview_text?: string;
163
+ cover_media?: {
164
+ media_info?: {
165
+ original_img_url?: string;
166
+ original_img_width?: number;
167
+ original_img_height?: number;
168
+ };
169
+ };
170
+ content?: {
171
+ blocks?: Array<{
172
+ text?: string;
173
+ type?: string;
174
+ }>;
175
+ };
176
+ };
177
+ };
178
+ }
179
+ /** The `object` payload shape the metadata platform responds with. */
180
+ export interface PageObject {
181
+ type: "page";
182
+ language?: string;
183
+ title?: string;
184
+ name?: string;
185
+ description?: string;
186
+ image?: Array<{
187
+ url: string;
188
+ type?: string;
189
+ width?: string | number;
190
+ height?: string | number;
191
+ }>;
192
+ video?: {
193
+ url: string;
194
+ thumbnail?: string;
195
+ width?: number;
196
+ height?: number;
197
+ duration?: number;
198
+ };
199
+ url?: string;
200
+ favicon?: string;
201
+ charset?: string;
202
+ }
203
+ /**
204
+ * Map an FxTwitter API response onto the platform's page object. The
205
+ * preview image is the post's own media — first photo, else the video
206
+ * thumbnail — never the generic site banner. Returns null when the API
207
+ * reports anything but success (the caller falls back to the OG scrape).
208
+ */
209
+ export declare function tweetToPageObject(status: FxTwitterStatus): PageObject | null;
package/dist/schema.d.ts CHANGED
@@ -76,6 +76,28 @@ export declare const PlatformMetadataSchema: {
76
76
  image: {
77
77
  $ref: string;
78
78
  };
79
+ video: {
80
+ type: string;
81
+ required: string[];
82
+ additionalProperties: boolean;
83
+ properties: {
84
+ url: {
85
+ type: string;
86
+ };
87
+ thumbnail: {
88
+ type: string;
89
+ };
90
+ width: {
91
+ type: string;
92
+ };
93
+ height: {
94
+ type: string;
95
+ };
96
+ duration: {
97
+ type: string;
98
+ };
99
+ };
100
+ };
79
101
  url: {
80
102
  type: string;
81
103
  };
@@ -106,6 +128,9 @@ export declare const PlatformMetadataSchema: {
106
128
  height: {
107
129
  type: string[];
108
130
  };
131
+ alt: {
132
+ type: string;
133
+ };
109
134
  };
110
135
  };
111
136
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sockethub/platform-metadata",
3
- "version": "1.0.1-alpha.9",
3
+ "version": "1.0.1-beta.0",
4
4
  "description": "Fetch metadata from a given URL",
5
5
  "author": "Nick Jennings <nick@silverbucket.net>",
6
6
  "license": "LGPL-3.0+",
@@ -42,15 +42,16 @@
42
42
  "clean:deps": "rm -rf node_modules"
43
43
  },
44
44
  "dependencies": {
45
- "@sockethub/util": "^1.0.0-alpha.2",
46
- "open-graph-scraper": "^6.11.0"
45
+ "@sockethub/util": "^1.0.0-beta.0",
46
+ "open-graph-scraper": "^6.11.0",
47
+ "undici": "^7.30.0"
47
48
  },
48
49
  "devDependencies": {
49
- "@sockethub/schemas": "^3.0.0-alpha.14"
50
+ "@sockethub/schemas": "^3.0.0-beta.0"
50
51
  },
51
52
  "peerDependencies": {
52
- "@sockethub/server": "^5.0.0-alpha.14"
53
+ "@sockethub/server": "^5.0.0-beta.0"
53
54
  },
54
- "gitHead": "4f99b07fc19c9606875621b3f96323fa6e62ae3f",
55
+ "gitHead": "a17e337ee7d2dd496d225c0d6e8ae48f4f4ce73c",
55
56
  "types": "./dist/index.d.ts"
56
57
  }