@unavatar/core 3.68.0 → 3.70.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
@@ -44,12 +44,14 @@
44
44
  - [DuckDuckGo](#duckduckgo)
45
45
  - [Duolingo](#duolingo)
46
46
  - [Email](#email)
47
+ - [ENS](#ens)
47
48
  - [Facebook](#facebook)
48
49
  - [Farcaster](#farcaster)
49
50
  - [Flickr](#flickr)
50
51
  - [Gitee](#gitee)
51
52
  - [GitHub](#github)
52
53
  - [GitLab](#gitlab)
54
+ - [Goodreads](#goodreads)
53
55
  - [Google](#google)
54
56
  - [Google Play](#google-play)
55
57
  - [Gravatar](#gravatar)
@@ -773,6 +775,14 @@ Target a specific provider when you know where the avatar comes from.
773
775
 
774
776
  e.g., [unavatar.io/email/hello@microlink.io](https://unavatar.io/email/hello@microlink.io)
775
777
 
778
+ ### ENS
779
+
780
+ Get the avatar set on any Ethereum Name Service (ENS) name, including subnames like `jesse.base.eth`. A bare label is treated as a `.eth` name.
781
+
782
+ e.g., [unavatar.io/ens/vitalik.eth](https://unavatar.io/ens/vitalik.eth)
783
+
784
+ Names ending in `.eth` are detected automatically, so [unavatar.io/vitalik.eth](https://unavatar.io/vitalik.eth) resolves through ENS too.
785
+
776
786
  ### Facebook
777
787
 
778
788
  Get any Facebook user, page, or group profile picture by their username or ID.
@@ -829,6 +839,20 @@ Available inputs:
829
839
  - User, e.g., [unavatar.io/gitlab/sytses](https://unavatar.io/gitlab/sytses)
830
840
  - Organization, e.g., [unavatar.io/gitlab/inkscape](https://unavatar.io/gitlab/inkscape)
831
841
 
842
+ ### Goodreads
843
+
844
+ Get any Goodreads user's profile picture by their vanity username or numeric user ID, or an author's photo by their author ID.
845
+
846
+ The input supports a URI format `type:id`.
847
+
848
+ When no type is provided, the input is treated as a user (`user`).
849
+
850
+ Available inputs:
851
+
852
+ - `user` (default), by username: [unavatar.io/goodreads/otis](https://unavatar.io/goodreads/otis)
853
+ - `user` (default), by user ID: [unavatar.io/goodreads/1](https://unavatar.io/goodreads/1)
854
+ - `author`: [unavatar.io/goodreads/author:4](https://unavatar.io/goodreads/author:4)
855
+
832
856
  ### Google
833
857
 
834
858
  Get the favicon or logo for any domain using Google's favicon service.
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.68.0",
5
+ "version": "3.70.0",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
@@ -119,6 +119,7 @@
119
119
  "youtube"
120
120
  ],
121
121
  "dependencies": {
122
+ "@adraffy/ens-normalize": "~1.11.1",
122
123
  "@keyvhq/compress": "~2.2.0",
123
124
  "@keyvhq/core": "~2.2.7",
124
125
  "@keyvhq/memoize": "~2.2.1",
@@ -15,11 +15,13 @@ const ExtendableError = require('../util/error')
15
15
 
16
16
  const DATA_URI_REGEX = dataUriRegex()
17
17
  const DOMAIN_REGEX = urlRegex({ strict: false })
18
+ const ENS_REGEX = /^[^\s/.][^\s/]*\.eth$/i
18
19
 
19
20
  const isHash = require('../util/is-hash')
20
21
 
21
22
  const getInputType = input => {
22
23
  if (isEmail(input) || isHash(input)) return 'email'
24
+ if (ENS_REGEX.test(input.trim())) return 'ens'
23
25
  if (stableRegex(DOMAIN_REGEX, input)) return 'domain'
24
26
  return 'username'
25
27
  }
@@ -0,0 +1,26 @@
1
+ 'use strict'
2
+
3
+ const { ens_normalize: normalizeEnsName } = require('@adraffy/ens-normalize')
4
+
5
+ const ENS_AVATAR_URL = 'https://metadata.ens.domains/mainnet/avatar'
6
+ const ENS_TLD = '.eth'
7
+
8
+ const parseInput = input => {
9
+ const name = input.trim()
10
+ try {
11
+ return normalizeEnsName(name.includes('.') ? name : `${name}${ENS_TLD}`)
12
+ } catch {}
13
+ }
14
+
15
+ const getAvatarUrl = input => {
16
+ const name = parseInput(input)
17
+ return name && `${ENS_AVATAR_URL}/${encodeURIComponent(name)}`
18
+ }
19
+
20
+ module.exports = () =>
21
+ function ens (input) {
22
+ return getAvatarUrl(input)
23
+ }
24
+
25
+ module.exports.parseInput = parseInput
26
+ module.exports.getAvatarUrl = getAvatarUrl
@@ -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',
@@ -96,7 +97,8 @@ const providerTiers = {
96
97
  'zhihu'
97
98
  ]
98
99
  ],
99
- domain: [['bimi'], ['duckduckgo', 'google', 'microlink']]
100
+ domain: [['bimi'], ['duckduckgo', 'google', 'microlink']],
101
+ ens: [['ens']]
100
102
  }
101
103
 
102
104
  const { emailHash, ...publicTiers } = providerTiers
@@ -133,12 +135,14 @@ module.exports = ctx => {
133
135
  dribbble: require('./dribbble')(ctx),
134
136
  duckduckgo: require('./duckduckgo')(ctx),
135
137
  duolingo: require('./duolingo')(ctx),
138
+ ens: require('./ens')(ctx),
136
139
  facebook: require('./facebook')(ctx),
137
140
  farcaster: require('./farcaster')(ctx),
138
141
  flickr: require('./flickr')(ctx),
139
142
  gitee: require('./gitee')(ctx),
140
143
  github: require('./github')(ctx),
141
144
  gitlab: require('./gitlab')(ctx),
145
+ goodreads: require('./goodreads')(ctx),
142
146
  google: require('./google')(ctx),
143
147
  'google-play': require('./google-play')(ctx),
144
148
  gravatar: require('./gravatar')(ctx),