@unavatar/core 3.69.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
@@ -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)
@@ -838,6 +839,20 @@ Available inputs:
838
839
  - User, e.g., [unavatar.io/gitlab/sytses](https://unavatar.io/gitlab/sytses)
839
840
  - Organization, e.g., [unavatar.io/gitlab/inkscape](https://unavatar.io/gitlab/inkscape)
840
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
+
841
856
  ### Google
842
857
 
843
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.69.0",
5
+ "version": "3.70.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',
@@ -141,6 +142,7 @@ module.exports = ctx => {
141
142
  gitee: require('./gitee')(ctx),
142
143
  github: require('./github')(ctx),
143
144
  gitlab: require('./gitlab')(ctx),
145
+ goodreads: require('./goodreads')(ctx),
144
146
  google: require('./google')(ctx),
145
147
  'google-play': require('./google-play')(ctx),
146
148
  gravatar: require('./gravatar')(ctx),