@unavatar/core 3.70.0 → 3.71.1

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
@@ -68,11 +68,13 @@
68
68
  - [Libravatar](#libravatar)
69
69
  - [LinkedIn](#linkedin)
70
70
  - [Mastodon](#mastodon)
71
+ - [Matrix](#matrix)
71
72
  - [Medium](#medium)
72
73
  - [Microlink](#microlink)
73
74
  - [Minecraft](#minecraft)
74
75
  - [Mixcloud](#mixcloud)
75
76
  - [NetEase Music](#netease-music)
77
+ - [note](#note)
76
78
  - [npm](#npm)
77
79
  - [OnlyFans](#onlyfans)
78
80
  - [Open Collective](#open-collective)
@@ -980,6 +982,12 @@ Get any Mastodon user's profile picture from any instance using the public accou
980
982
 
981
983
  e.g., [unavatar.io/mastodon/kpwags@hachyderm.io](https://unavatar.io/mastodon/kpwags@hachyderm.io)
982
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
+
983
991
  ### Medium
984
992
 
985
993
  Get any Medium author's profile picture by their username.
@@ -1013,6 +1021,12 @@ Get any NetEase Music user's profile picture by their numeric user ID.
1013
1021
 
1014
1022
  e.g., [unavatar.io/netease-music/32953014](https://unavatar.io/netease-music/32953014)
1015
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
+
1016
1030
  ### npm
1017
1031
 
1018
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.70.0",
5
+ "version": "3.71.1",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
@@ -46,10 +46,12 @@ const providerTiers = {
46
46
  'lemmy',
47
47
  'linkedin',
48
48
  'mastodon',
49
+ 'matrix',
49
50
  'medium',
50
51
  'minecraft',
51
52
  'mixcloud',
52
53
  'netease-music',
54
+ 'note',
53
55
  'npm',
54
56
  'onlyfans',
55
57
  'opencollective',
@@ -159,11 +161,13 @@ module.exports = ctx => {
159
161
  libravatar: require('./libravatar')(ctx),
160
162
  linkedin: require('./linkedin')(ctx),
161
163
  mastodon: require('./mastodon')(ctx),
164
+ matrix: require('./matrix')(ctx),
162
165
  medium: require('./medium')(ctx),
163
166
  microlink: require('./microlink')(ctx),
164
167
  minecraft: require('./minecraft')(ctx),
165
168
  mixcloud: require('./mixcloud')(ctx),
166
169
  'netease-music': require('./netease-music')(ctx),
170
+ note: require('./note')(ctx),
167
171
  npm: require('./npm')(ctx),
168
172
  onlyfans: require('./onlyfans')(ctx),
169
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
@@ -1,5 +1,12 @@
1
1
  'use strict'
2
2
 
3
+ const uniqueRandomArray = require('unique-random-array')
4
+
5
+ // Mobile user agents get an HTML page instead of the user_json payload.
6
+ const randomDesktopUserAgent = uniqueRandomArray(
7
+ require('top-user-agents/desktop')
8
+ )
9
+
3
10
  const PORTRAIT_URL = 'https://himg.bdimg.com/sys/portrait/item'
4
11
 
5
12
  const getAvatarUrl = input =>
@@ -17,7 +24,8 @@ module.exports = ({ got }) =>
17
24
  // Unknown users respond with an empty body, which is not valid JSON.
18
25
  const { body, statusCode } = await got(getAvatarUrl(input), {
19
26
  responseType: 'text',
20
- throwHttpErrors: false
27
+ throwHttpErrors: false,
28
+ headers: { 'user-agent': randomDesktopUserAgent() }
21
29
  })
22
30
 
23
31
  if (statusCode >= 400 || !body) return
@@ -1,5 +1,12 @@
1
1
  'use strict'
2
2
 
3
+ const uniqueRandomArray = require('unique-random-array')
4
+
5
+ // Mobile user agents get a 403 from the members API.
6
+ const randomDesktopUserAgent = uniqueRandomArray(
7
+ require('top-user-agents/desktop')
8
+ )
9
+
3
10
  const API_URL = 'https://www.zhihu.com/api/v4/members'
4
11
 
5
12
  const getAvatarUrl = input =>
@@ -16,7 +23,8 @@ module.exports = ({ got }) =>
16
23
  async function zhihu (input) {
17
24
  const { body, statusCode } = await got(getAvatarUrl(input), {
18
25
  responseType: 'json',
19
- throwHttpErrors: false
26
+ throwHttpErrors: false,
27
+ headers: { 'user-agent': randomDesktopUserAgent() }
20
28
  })
21
29
 
22
30
  if (statusCode >= 400) return