@unavatar/core 3.69.0 → 3.71.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.
package/README.md CHANGED
@@ -51,6 +51,7 @@
51
51
  - [Gitee](#gitee)
52
52
  - [GitHub](#github)
53
53
  - [GitLab](#gitlab)
54
+ - [Goodreads](#goodreads)
54
55
  - [Google](#google)
55
56
  - [Google Play](#google-play)
56
57
  - [Gravatar](#gravatar)
@@ -67,11 +68,13 @@
67
68
  - [Libravatar](#libravatar)
68
69
  - [LinkedIn](#linkedin)
69
70
  - [Mastodon](#mastodon)
71
+ - [Matrix](#matrix)
70
72
  - [Medium](#medium)
71
73
  - [Microlink](#microlink)
72
74
  - [Minecraft](#minecraft)
73
75
  - [Mixcloud](#mixcloud)
74
76
  - [NetEase Music](#netease-music)
77
+ - [note](#note)
75
78
  - [npm](#npm)
76
79
  - [OnlyFans](#onlyfans)
77
80
  - [Open Collective](#open-collective)
@@ -838,6 +841,20 @@ Available inputs:
838
841
  - User, e.g., [unavatar.io/gitlab/sytses](https://unavatar.io/gitlab/sytses)
839
842
  - Organization, e.g., [unavatar.io/gitlab/inkscape](https://unavatar.io/gitlab/inkscape)
840
843
 
844
+ ### Goodreads
845
+
846
+ Get any Goodreads user's profile picture by their vanity username or numeric user ID, or an author's photo by their author ID.
847
+
848
+ The input supports a URI format `type:id`.
849
+
850
+ When no type is provided, the input is treated as a user (`user`).
851
+
852
+ Available inputs:
853
+
854
+ - `user` (default), by username: [unavatar.io/goodreads/otis](https://unavatar.io/goodreads/otis)
855
+ - `user` (default), by user ID: [unavatar.io/goodreads/1](https://unavatar.io/goodreads/1)
856
+ - `author`: [unavatar.io/goodreads/author:4](https://unavatar.io/goodreads/author:4)
857
+
841
858
  ### Google
842
859
 
843
860
  Get the favicon or logo for any domain using Google's favicon service.
@@ -965,6 +982,12 @@ Get any Mastodon user's profile picture from any instance using the public accou
965
982
 
966
983
  e.g., [unavatar.io/mastodon/kpwags@hachyderm.io](https://unavatar.io/mastodon/kpwags@hachyderm.io)
967
984
 
985
+ ### Matrix
986
+
987
+ Get any Matrix user's profile picture from any homeserver using the public profile API. Pass the full user ID as `@user:server` (the leading `@` is optional); a bare `user` resolves on `matrix.org`. Homeservers that require authentication for profiles or media return no avatar.
988
+
989
+ e.g., [unavatar.io/matrix/@matthew:matrix.org](https://unavatar.io/matrix/@matthew:matrix.org)
990
+
968
991
  ### Medium
969
992
 
970
993
  Get any Medium author's profile picture by their username.
@@ -998,6 +1021,12 @@ Get any NetEase Music user's profile picture by their numeric user ID.
998
1021
 
999
1022
  e.g., [unavatar.io/netease-music/32953014](https://unavatar.io/netease-music/32953014)
1000
1023
 
1024
+ ### note
1025
+
1026
+ Get any note.com creator's profile picture by their username.
1027
+
1028
+ e.g., [unavatar.io/note/info](https://unavatar.io/note/info)
1029
+
1001
1030
  ### npm
1002
1031
 
1003
1032
  Get any npm user's profile picture by their username, including scoped handles.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@unavatar/core",
3
3
  "description": "Get unified user avatar from social networks, including Instagram, SoundCloud, Telegram, Twitter, YouTube & more.",
4
4
  "homepage": "https://unavatar.io",
5
- "version": "3.69.0",
5
+ "version": "3.71.0",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
@@ -0,0 +1,64 @@
1
+ 'use strict'
2
+
3
+ const GOODREADS_URL = 'https://www.goodreads.com'
4
+ const USER_ID_PATTERN = /^\d+(?:-[\w-]*)?$/
5
+ const IMAGE_SIZE_PATTERN = /p\d+(\/[^/]+)$/
6
+ const LARGEST_IMAGE_SIZE = 'p8'
7
+ const NO_PHOTO_PATH = '/nophoto/'
8
+
9
+ const MAX_INPUT_FIELDS = 2
10
+
11
+ const parseInput = input => {
12
+ const fields = input.split(':')
13
+ if (fields.length > MAX_INPUT_FIELDS || fields.some(field => !field)) return
14
+ const [first, second] = fields
15
+ return second ? { type: first, id: second } : { type: 'user', id: first }
16
+ }
17
+
18
+ const getUserProfileUrl = id =>
19
+ USER_ID_PATTERN.test(id)
20
+ ? `${GOODREADS_URL}/user/show/${id}`
21
+ : `${GOODREADS_URL}/${encodeURIComponent(id)}`
22
+
23
+ const getAuthorProfileUrl = id =>
24
+ `${GOODREADS_URL}/author/show/${encodeURIComponent(id)}`
25
+
26
+ const getAvatarUrl = input => {
27
+ const parsed = parseInput(input)
28
+ if (!parsed) throw new Error(`Invalid Goodreads input: ${input}`)
29
+ const { type, id } = parsed
30
+
31
+ switch (type) {
32
+ case 'user':
33
+ return getUserProfileUrl(id)
34
+ case 'author':
35
+ return getAuthorProfileUrl(id)
36
+ default:
37
+ throw new Error(`Unsupported Goodreads type: ${type}`)
38
+ }
39
+ }
40
+
41
+ const getAvatar = ({ $, getOgImage, NOT_FOUND }) => {
42
+ const image = getOgImage($)
43
+ if (!image) return
44
+ if (image.includes(NO_PHOTO_PATH)) return NOT_FOUND
45
+ return image.replace(IMAGE_SIZE_PATTERN, `${LARGEST_IMAGE_SIZE}$1`)
46
+ }
47
+
48
+ module.exports = ({ createHtmlProvider, getOgImage, NOT_FOUND }) => {
49
+ const htmlProvider = createHtmlProvider({
50
+ name: 'goodreads',
51
+ url: getAvatarUrl,
52
+ getter: $ => getAvatar({ $, getOgImage, NOT_FOUND })
53
+ })
54
+
55
+ const provider = (input, context) =>
56
+ parseInput(input) ? htmlProvider(input, context) : undefined
57
+
58
+ provider.getUrl = getAvatarUrl
59
+ return provider
60
+ }
61
+
62
+ module.exports.parseInput = parseInput
63
+ module.exports.getAvatarUrl = getAvatarUrl
64
+ module.exports.getAvatar = getAvatar
@@ -32,6 +32,7 @@ const providerTiers = {
32
32
  'gitee',
33
33
  'github',
34
34
  'gitlab',
35
+ 'goodreads',
35
36
  'google-play',
36
37
  'gumroad',
37
38
  'huggingface',
@@ -45,10 +46,12 @@ const providerTiers = {
45
46
  'lemmy',
46
47
  'linkedin',
47
48
  'mastodon',
49
+ 'matrix',
48
50
  'medium',
49
51
  'minecraft',
50
52
  'mixcloud',
51
53
  'netease-music',
54
+ 'note',
52
55
  'npm',
53
56
  'onlyfans',
54
57
  'opencollective',
@@ -141,6 +144,7 @@ module.exports = ctx => {
141
144
  gitee: require('./gitee')(ctx),
142
145
  github: require('./github')(ctx),
143
146
  gitlab: require('./gitlab')(ctx),
147
+ goodreads: require('./goodreads')(ctx),
144
148
  google: require('./google')(ctx),
145
149
  'google-play': require('./google-play')(ctx),
146
150
  gravatar: require('./gravatar')(ctx),
@@ -157,11 +161,13 @@ module.exports = ctx => {
157
161
  libravatar: require('./libravatar')(ctx),
158
162
  linkedin: require('./linkedin')(ctx),
159
163
  mastodon: require('./mastodon')(ctx),
164
+ matrix: require('./matrix')(ctx),
160
165
  medium: require('./medium')(ctx),
161
166
  microlink: require('./microlink')(ctx),
162
167
  minecraft: require('./minecraft')(ctx),
163
168
  mixcloud: require('./mixcloud')(ctx),
164
169
  'netease-music': require('./netease-music')(ctx),
170
+ note: require('./note')(ctx),
165
171
  npm: require('./npm')(ctx),
166
172
  onlyfans: require('./onlyfans')(ctx),
167
173
  opencollective: require('./opencollective')(ctx),
@@ -57,3 +57,4 @@ module.exports = ({ got, isReservedIp }) => {
57
57
  }
58
58
 
59
59
  module.exports.parseInput = parseInput
60
+ module.exports.isValidServer = isValidServer
@@ -0,0 +1,91 @@
1
+ 'use strict'
2
+
3
+ const { isValidServer } = require('./mastodon')
4
+
5
+ const DEFAULT_SERVER = 'matrix.org'
6
+ const WELL_KNOWN_PATH = '/.well-known/matrix/client'
7
+ const PROFILE_PATH = '/_matrix/client/v3/profile'
8
+ const MEDIA_DOWNLOAD_PATH = '/_matrix/media/v3/download'
9
+ const MXC_RE = /^mxc:\/\/([a-zA-Z0-9[][\w.:[\]-]*)\/([\w-]+)$/
10
+
11
+ const parseInput = input => {
12
+ if (typeof input !== 'string') return
13
+
14
+ const cleaned = input.startsWith('@') ? input.slice(1) : input
15
+ const separatorIndex = cleaned.indexOf(':')
16
+ const localpart =
17
+ separatorIndex === -1 ? cleaned : cleaned.slice(0, separatorIndex)
18
+ const server =
19
+ separatorIndex === -1 ? DEFAULT_SERVER : cleaned.slice(separatorIndex + 1)
20
+
21
+ if (!localpart || localpart.includes('@')) return
22
+
23
+ if (!isValidServer(server)) return
24
+
25
+ return {
26
+ userId: `@${localpart}:${server}`,
27
+ server,
28
+ hostname: new URL(`https://${server}`).hostname
29
+ }
30
+ }
31
+
32
+ const parseHomeserverUrl = baseUrl => {
33
+ try {
34
+ const url = new URL(baseUrl)
35
+ if (url.protocol !== 'https:' && url.protocol !== 'http:') return
36
+ return url.origin + url.pathname.replace(/\/+$/, '')
37
+ } catch {}
38
+ }
39
+
40
+ const getProfileUrl = (homeserverUrl, userId) =>
41
+ `${homeserverUrl}${PROFILE_PATH}/${encodeURIComponent(userId)}/avatar_url`
42
+
43
+ const getMediaUrl = (homeserverUrl, mxcUrl) => {
44
+ const match = typeof mxcUrl === 'string' && MXC_RE.exec(mxcUrl)
45
+ if (!match) return
46
+ const [, origin, mediaId] = match
47
+ return `${homeserverUrl}${MEDIA_DOWNLOAD_PATH}/${origin}/${mediaId}`
48
+ }
49
+
50
+ module.exports = ({ got, isReservedIp }) => {
51
+ const getHomeserverUrl = async server => {
52
+ const fallback = `https://${server}`
53
+ const body = await got(`${fallback}${WELL_KNOWN_PATH}`, {
54
+ responseType: 'json',
55
+ throwHttpErrors: false
56
+ })
57
+ .then(response => response.body)
58
+ .catch(() => undefined)
59
+
60
+ const homeserverUrl = parseHomeserverUrl(body?.['m.homeserver']?.base_url)
61
+ if (!homeserverUrl) return fallback
62
+ if (await isReservedIp(new URL(homeserverUrl).hostname)) return
63
+ return homeserverUrl
64
+ }
65
+
66
+ const matrix = async function (input) {
67
+ const parsed = parseInput(input)
68
+ if (!parsed) return
69
+
70
+ const { userId, server, hostname } = parsed
71
+
72
+ if (await isReservedIp(hostname)) return
73
+
74
+ const homeserverUrl = await getHomeserverUrl(server)
75
+ if (!homeserverUrl) return
76
+
77
+ const { body, statusCode } = await got(
78
+ getProfileUrl(homeserverUrl, userId),
79
+ { responseType: 'json', throwHttpErrors: false }
80
+ )
81
+
82
+ if (statusCode >= 400) return
83
+
84
+ return getMediaUrl(homeserverUrl, body?.avatar_url)
85
+ }
86
+
87
+ return matrix
88
+ }
89
+
90
+ module.exports.parseInput = parseInput
91
+ module.exports.getMediaUrl = getMediaUrl
@@ -0,0 +1,28 @@
1
+ 'use strict'
2
+
3
+ const API_URL = 'https://note.com/api/v2/creators'
4
+ const DEFAULT_AVATAR_PATH = '/assets/default/'
5
+
6
+ const getAvatarUrl = input => `${API_URL}/${encodeURIComponent(input)}`
7
+
8
+ const getAvatar = body => {
9
+ const url = body?.data?.profileImageUrl
10
+ if (typeof url !== 'string' || !url) return
11
+ if (new URL(url).pathname.startsWith(DEFAULT_AVATAR_PATH)) return
12
+ return url
13
+ }
14
+
15
+ module.exports = ({ got }) =>
16
+ async function note (input) {
17
+ const { body, statusCode } = await got(getAvatarUrl(input), {
18
+ responseType: 'json',
19
+ throwHttpErrors: false
20
+ })
21
+
22
+ if (statusCode >= 400) return
23
+
24
+ return getAvatar(body)
25
+ }
26
+
27
+ module.exports.getAvatarUrl = getAvatarUrl
28
+ module.exports.getAvatar = getAvatar