@sockethub/platform-metadata 1.0.1-alpha.2 → 1.0.1-alpha.20
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.
- package/README.md +112 -11
- package/dist/index.d.ts +183 -0
- package/dist/index.js +56475 -55497
- package/dist/index.js.map +117 -114
- package/dist/resolvers.d.ts +202 -0
- package/dist/schema.d.ts +140 -0
- package/package.json +12 -9
- package/src/index.ts +0 -56
- package/src/schema.ts +0 -22
|
@@ -0,0 +1,202 @@
|
|
|
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
|
+
* True for Facebook URLs (including fb.watch short links). Facebook serves
|
|
42
|
+
* an unrecognized scraper a login interstitial instead of the post, but
|
|
43
|
+
* serves recognized link-preview crawlers the post's Open Graph data —
|
|
44
|
+
* title, caption, and the video/photo thumbnail — so these URLs scrape
|
|
45
|
+
* with COMPAT_USER_AGENT (see index.ts), like Reddit.
|
|
46
|
+
*/
|
|
47
|
+
export declare function isFacebookUrl(url: string): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Remove the engagement-stats segment Facebook prepends to video titles:
|
|
50
|
+
* "2.2M views · 21K reactions | <video title> | <author>". The stats are
|
|
51
|
+
* localized to the *scraping server's* geo-IP language, so match their
|
|
52
|
+
* shape — a leading pipe-delimited segment containing a digit and the "·"
|
|
53
|
+
* separator — rather than any particular wording.
|
|
54
|
+
*/
|
|
55
|
+
export declare function stripFacebookEngagement(title: string | undefined): string | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* True for Reddit URLs (including redd.it short links). Reddit serves its
|
|
58
|
+
* Open Graph tags — with the post's real preview image — only to
|
|
59
|
+
* recognized embed-crawler user agents; everything else gets a page with
|
|
60
|
+
* no OG data (or an outright 403 from datacenter addresses).
|
|
61
|
+
*/
|
|
62
|
+
export declare function isRedditUrl(url: string): boolean;
|
|
63
|
+
/** Map a canonical Reddit post URL to Reddit's official media-enabled embed. */
|
|
64
|
+
export declare function resolveRedditEmbed(url: string): string | null;
|
|
65
|
+
/** Map a canonical Reddit post URL to old.reddit.com's public JSON endpoint. */
|
|
66
|
+
export declare function resolveRedditJson(url: string): string | null;
|
|
67
|
+
/**
|
|
68
|
+
* Unwrap Reddit's media-viewer URL (`reddit.com/media?url=<image>`) to the
|
|
69
|
+
* image it displays. Reddit serves neither post JSON nor oEmbed for these
|
|
70
|
+
* links (its oEmbed endpoint returns HTTP 400), but the target image is
|
|
71
|
+
* embedded in the link itself, so no network request is needed at all.
|
|
72
|
+
*/
|
|
73
|
+
export declare function resolveRedditMedia(url: string): string | null;
|
|
74
|
+
export interface RedditPost {
|
|
75
|
+
title: string;
|
|
76
|
+
selftext?: string;
|
|
77
|
+
url?: string;
|
|
78
|
+
url_overridden_by_dest?: string;
|
|
79
|
+
secure_media?: {
|
|
80
|
+
reddit_video?: {
|
|
81
|
+
fallback_url?: string;
|
|
82
|
+
width?: number;
|
|
83
|
+
height?: number;
|
|
84
|
+
duration?: number;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
preview?: {
|
|
88
|
+
images?: Array<{
|
|
89
|
+
source?: {
|
|
90
|
+
url?: string;
|
|
91
|
+
width?: number;
|
|
92
|
+
height?: number;
|
|
93
|
+
};
|
|
94
|
+
}>;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** Validate and extract the post object from Reddit's listing response. */
|
|
98
|
+
export declare function parseRedditPost(value: unknown): RedditPost | null;
|
|
99
|
+
/** Select only a direct Reddit-hosted image belonging to the post. */
|
|
100
|
+
export declare function redditPostImage(post: RedditPost): PageObject["image"];
|
|
101
|
+
/** Map Reddit's hosted-video payload to the common page video shape. */
|
|
102
|
+
export declare function redditPostVideo(post: RedditPost): PageObject["video"];
|
|
103
|
+
/** Subset of Reddit's oEmbed response used for link previews. */
|
|
104
|
+
export interface RedditOEmbed {
|
|
105
|
+
title?: string;
|
|
106
|
+
author_name?: string;
|
|
107
|
+
provider_name?: string;
|
|
108
|
+
thumbnail_url?: string;
|
|
109
|
+
thumbnail_width?: number;
|
|
110
|
+
thumbnail_height?: number;
|
|
111
|
+
}
|
|
112
|
+
/** Validate the subset of Reddit's untrusted oEmbed JSON that we consume. */
|
|
113
|
+
export declare function parseRedditOEmbed(value: unknown): RedditOEmbed | null;
|
|
114
|
+
/**
|
|
115
|
+
* Convert Reddit's optional oEmbed thumbnail into the platform image shape.
|
|
116
|
+
* A missing or malformed thumbnail deliberately means "no image": Reddit's
|
|
117
|
+
* scraped Open Graph image may be a generic site hero rather than post media.
|
|
118
|
+
*/
|
|
119
|
+
export declare function redditOEmbedImage(embed: RedditOEmbed): PageObject["image"] | undefined;
|
|
120
|
+
/** Keep only Reddit-hosted post media, excluding branding/share-card images. */
|
|
121
|
+
export declare function redditPostImages(images: PageObject["image"]): PageObject["image"] | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* Make scraped plain text readable without flattening intentional structure.
|
|
124
|
+
* Preserve line breaks and paragraphs, but remove HTML indentation and cap
|
|
125
|
+
* excessive vertical whitespace at one blank line.
|
|
126
|
+
*/
|
|
127
|
+
export declare function normalizeDescription(value: string): string;
|
|
128
|
+
/** Subset of the FxTwitter status API response the platform consumes. */
|
|
129
|
+
export interface FxTwitterStatus {
|
|
130
|
+
code: number;
|
|
131
|
+
message?: string;
|
|
132
|
+
tweet?: {
|
|
133
|
+
url?: string;
|
|
134
|
+
text?: string;
|
|
135
|
+
author?: {
|
|
136
|
+
name?: string;
|
|
137
|
+
screen_name?: string;
|
|
138
|
+
};
|
|
139
|
+
media?: {
|
|
140
|
+
photos?: Array<{
|
|
141
|
+
url?: string;
|
|
142
|
+
width?: number;
|
|
143
|
+
height?: number;
|
|
144
|
+
}>;
|
|
145
|
+
videos?: Array<{
|
|
146
|
+
url?: string;
|
|
147
|
+
thumbnail_url?: string;
|
|
148
|
+
width?: number;
|
|
149
|
+
height?: number;
|
|
150
|
+
duration?: number;
|
|
151
|
+
}>;
|
|
152
|
+
};
|
|
153
|
+
article?: {
|
|
154
|
+
title?: string;
|
|
155
|
+
preview_text?: string;
|
|
156
|
+
cover_media?: {
|
|
157
|
+
media_info?: {
|
|
158
|
+
original_img_url?: string;
|
|
159
|
+
original_img_width?: number;
|
|
160
|
+
original_img_height?: number;
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
content?: {
|
|
164
|
+
blocks?: Array<{
|
|
165
|
+
text?: string;
|
|
166
|
+
type?: string;
|
|
167
|
+
}>;
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/** The `object` payload shape the metadata platform responds with. */
|
|
173
|
+
export interface PageObject {
|
|
174
|
+
type: "page";
|
|
175
|
+
language?: string;
|
|
176
|
+
title?: string;
|
|
177
|
+
name?: string;
|
|
178
|
+
description?: string;
|
|
179
|
+
image?: Array<{
|
|
180
|
+
url: string;
|
|
181
|
+
type?: string;
|
|
182
|
+
width?: string | number;
|
|
183
|
+
height?: string | number;
|
|
184
|
+
}>;
|
|
185
|
+
video?: {
|
|
186
|
+
url: string;
|
|
187
|
+
thumbnail?: string;
|
|
188
|
+
width?: number;
|
|
189
|
+
height?: number;
|
|
190
|
+
duration?: number;
|
|
191
|
+
};
|
|
192
|
+
url?: string;
|
|
193
|
+
favicon?: string;
|
|
194
|
+
charset?: string;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Map an FxTwitter API response onto the platform's page object. The
|
|
198
|
+
* preview image is the post's own media — first photo, else the video
|
|
199
|
+
* thumbnail — never the generic site banner. Returns null when the API
|
|
200
|
+
* reports anything but success (the caller falls back to the OG scrape).
|
|
201
|
+
*/
|
|
202
|
+
export declare function tweetToPageObject(status: FxTwitterStatus): PageObject | null;
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
export declare const PlatformMetadataSchema: {
|
|
2
|
+
name: string;
|
|
3
|
+
version: string;
|
|
4
|
+
contextUrl: string;
|
|
5
|
+
contextVersion: string;
|
|
6
|
+
schemaVersion: string;
|
|
7
|
+
messages: {
|
|
8
|
+
required: string[];
|
|
9
|
+
properties: {
|
|
10
|
+
type: {
|
|
11
|
+
type: string;
|
|
12
|
+
enum: string[];
|
|
13
|
+
};
|
|
14
|
+
object: boolean;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
responses: {
|
|
18
|
+
type: string;
|
|
19
|
+
required: string[];
|
|
20
|
+
additionalProperties: boolean;
|
|
21
|
+
properties: {
|
|
22
|
+
"@context": {
|
|
23
|
+
type: string;
|
|
24
|
+
items: {
|
|
25
|
+
type: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
id: {
|
|
29
|
+
type: string;
|
|
30
|
+
};
|
|
31
|
+
type: {
|
|
32
|
+
enum: string[];
|
|
33
|
+
};
|
|
34
|
+
actor: {
|
|
35
|
+
type: string;
|
|
36
|
+
required: string[];
|
|
37
|
+
additionalProperties: boolean;
|
|
38
|
+
properties: {
|
|
39
|
+
id: {
|
|
40
|
+
type: string;
|
|
41
|
+
};
|
|
42
|
+
type: {
|
|
43
|
+
enum: string[];
|
|
44
|
+
};
|
|
45
|
+
name: {
|
|
46
|
+
type: string;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
object: {
|
|
51
|
+
$ref: string;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
definitions: {
|
|
55
|
+
responses: {
|
|
56
|
+
page: {
|
|
57
|
+
type: string;
|
|
58
|
+
required: string[];
|
|
59
|
+
additionalProperties: boolean;
|
|
60
|
+
properties: {
|
|
61
|
+
type: {
|
|
62
|
+
enum: string[];
|
|
63
|
+
};
|
|
64
|
+
language: {
|
|
65
|
+
type: string;
|
|
66
|
+
};
|
|
67
|
+
title: {
|
|
68
|
+
type: string;
|
|
69
|
+
};
|
|
70
|
+
name: {
|
|
71
|
+
type: string;
|
|
72
|
+
};
|
|
73
|
+
description: {
|
|
74
|
+
type: string;
|
|
75
|
+
};
|
|
76
|
+
image: {
|
|
77
|
+
$ref: string;
|
|
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
|
+
};
|
|
101
|
+
url: {
|
|
102
|
+
type: string;
|
|
103
|
+
};
|
|
104
|
+
favicon: {
|
|
105
|
+
type: string;
|
|
106
|
+
};
|
|
107
|
+
charset: {
|
|
108
|
+
type: string;
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
ogImage: {
|
|
113
|
+
type: string;
|
|
114
|
+
items: {
|
|
115
|
+
type: string;
|
|
116
|
+
required: string[];
|
|
117
|
+
additionalProperties: boolean;
|
|
118
|
+
properties: {
|
|
119
|
+
url: {
|
|
120
|
+
type: string;
|
|
121
|
+
};
|
|
122
|
+
type: {
|
|
123
|
+
type: string;
|
|
124
|
+
};
|
|
125
|
+
width: {
|
|
126
|
+
type: string[];
|
|
127
|
+
};
|
|
128
|
+
height: {
|
|
129
|
+
type: string[];
|
|
130
|
+
};
|
|
131
|
+
alt: {
|
|
132
|
+
type: string;
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sockethub/platform-metadata",
|
|
3
|
-
"version": "1.0.1-alpha.
|
|
3
|
+
"version": "1.0.1-alpha.20",
|
|
4
4
|
"description": "Fetch metadata from a given URL",
|
|
5
5
|
"author": "Nick Jennings <nick@silverbucket.net>",
|
|
6
6
|
"license": "LGPL-3.0+",
|
|
@@ -11,17 +11,16 @@
|
|
|
11
11
|
"main": "dist/index.js",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
|
-
"
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
15
|
"import": "./dist/index.js",
|
|
16
16
|
"default": "./dist/index.js"
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"src/",
|
|
21
20
|
"dist/"
|
|
22
21
|
],
|
|
23
22
|
"engines": {
|
|
24
|
-
"
|
|
23
|
+
"node": ">=20"
|
|
25
24
|
},
|
|
26
25
|
"keywords": [
|
|
27
26
|
"sockethub",
|
|
@@ -39,16 +38,20 @@
|
|
|
39
38
|
"homepage": "https://github.com/sockethub/sockethub/tree/master/packages/platform-metadata",
|
|
40
39
|
"scripts": {
|
|
41
40
|
"build": "bun build src/index.ts --outdir dist --target node --format esm --sourcemap=external",
|
|
42
|
-
"clean": "rm -rf dist"
|
|
41
|
+
"clean": "rm -rf dist",
|
|
42
|
+
"clean:deps": "rm -rf node_modules"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"
|
|
45
|
+
"@sockethub/util": "^1.0.0-alpha.13",
|
|
46
|
+
"open-graph-scraper": "^6.11.0",
|
|
47
|
+
"undici": "^7.16.0"
|
|
46
48
|
},
|
|
47
49
|
"devDependencies": {
|
|
48
|
-
"@sockethub/schemas": "^3.0.0-alpha.
|
|
50
|
+
"@sockethub/schemas": "^3.0.0-alpha.25"
|
|
49
51
|
},
|
|
50
52
|
"peerDependencies": {
|
|
51
|
-
"@sockethub/server": "^5.0.0-alpha.
|
|
53
|
+
"@sockethub/server": "^5.0.0-alpha.25"
|
|
52
54
|
},
|
|
53
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "1a76cff52a7b6f405fc607f1402bf2346ec5c40f",
|
|
56
|
+
"types": "./dist/index.d.ts"
|
|
54
57
|
}
|
package/src/index.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ActivityStream,
|
|
3
|
-
Logger,
|
|
4
|
-
PlatformCallback,
|
|
5
|
-
PlatformConfig,
|
|
6
|
-
PlatformInterface,
|
|
7
|
-
PlatformSession,
|
|
8
|
-
} from "@sockethub/schemas";
|
|
9
|
-
import ogs from "open-graph-scraper";
|
|
10
|
-
import { PlatformMetadataSchema } from "./schema";
|
|
11
|
-
|
|
12
|
-
export default class Metadata implements PlatformInterface {
|
|
13
|
-
readonly debug: Logger;
|
|
14
|
-
config: PlatformConfig = {
|
|
15
|
-
persist: false,
|
|
16
|
-
};
|
|
17
|
-
constructor(session: PlatformSession) {
|
|
18
|
-
this.debug = session.debug;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
get schema() {
|
|
22
|
-
return PlatformMetadataSchema;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
fetch(job: ActivityStream, cb: PlatformCallback) {
|
|
26
|
-
this.debug(`fetching ${job.actor.id}`);
|
|
27
|
-
ogs({
|
|
28
|
-
url: job.actor.id,
|
|
29
|
-
})
|
|
30
|
-
.then((data) => {
|
|
31
|
-
const { result } = data;
|
|
32
|
-
job.actor.id = result.ogUrl || job.actor.id;
|
|
33
|
-
job.actor.name = result.ogSiteName || job.actor.name || "";
|
|
34
|
-
job.object = {
|
|
35
|
-
type: "page",
|
|
36
|
-
language: result.ogLocale,
|
|
37
|
-
title: result.ogTitle,
|
|
38
|
-
name: result.ogSiteName,
|
|
39
|
-
description: result.ogDescription || "",
|
|
40
|
-
image: result.ogImage,
|
|
41
|
-
url: result.ogUrl,
|
|
42
|
-
favicon: result.favicon,
|
|
43
|
-
charset: result.charset,
|
|
44
|
-
};
|
|
45
|
-
cb(null, job);
|
|
46
|
-
})
|
|
47
|
-
.catch((data) => {
|
|
48
|
-
const { result } = data;
|
|
49
|
-
cb(result.error);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
cleanup(cb: PlatformCallback) {
|
|
54
|
-
cb();
|
|
55
|
-
}
|
|
56
|
-
}
|
package/src/schema.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import packageJson from "../package.json" with { type: "json" };
|
|
2
|
-
const version = packageJson.version;
|
|
3
|
-
|
|
4
|
-
export const PlatformMetadataSchema = {
|
|
5
|
-
name: "metadata",
|
|
6
|
-
version: version,
|
|
7
|
-
messages: {
|
|
8
|
-
required: ["type"],
|
|
9
|
-
properties: {
|
|
10
|
-
type: {
|
|
11
|
-
type: "string",
|
|
12
|
-
enum: ["fetch"],
|
|
13
|
-
},
|
|
14
|
-
object: {
|
|
15
|
-
type: "object",
|
|
16
|
-
oneOf: [
|
|
17
|
-
{ $ref: "#/definitions/objectTypes/feed-parameters-url" },
|
|
18
|
-
],
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
};
|