@unavatar/core 3.56.0 → 3.58.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
@@ -34,6 +34,7 @@
34
34
  - [Cursor](#cursor)
35
35
  - [Dailymotion](#dailymotion)
36
36
  - [Deezer](#deezer)
37
+ - [DEV](#dev)
37
38
  - [DeviantArt](#deviantart)
38
39
  - [Discord](#discord)
39
40
  - [Docker Hub](#docker-hub)
@@ -66,6 +67,7 @@
66
67
  - [npm](#npm)
67
68
  - [OnlyFans](#onlyfans)
68
69
  - [OpenStreetMap](#openstreetmap)
70
+ - [osu!](#osu)
69
71
  - [Patreon](#patreon)
70
72
  - [PayPal](#paypal)
71
73
  - [Pinterest](#pinterest)
@@ -679,6 +681,12 @@ Available inputs:
679
681
  - `playlist`: [unavatar.io/deezer/playlist:908622995](https://unavatar.io/deezer/playlist:908622995)
680
682
  - `track`: [unavatar.io/deezer/track:3135556](https://unavatar.io/deezer/track:3135556)
681
683
 
684
+ ### DEV
685
+
686
+ Get any [DEV](https://dev.to) user's profile picture by username, or an organization's logo by its slug.
687
+
688
+ e.g., [unavatar.io/devto/ben](https://unavatar.io/devto/ben)
689
+
682
690
  ### DeviantArt
683
691
 
684
692
  Get any DeviantArt user's profile picture by their username.
@@ -937,6 +945,15 @@ Available inputs:
937
945
  - Numeric user ID, e.g., [unavatar.io/openstreetmap/98672](https://unavatar.io/openstreetmap/98672)
938
946
  - Username, e.g., [unavatar.io/openstreetmap/Terence%20Eden](https://unavatar.io/openstreetmap/Terence%20Eden)
939
947
 
948
+ ### osu!
949
+
950
+ Get any osu! player's profile picture by their username or numeric user ID.
951
+
952
+ Available inputs:
953
+
954
+ - Username, e.g., [unavatar.io/osu/peppy](https://unavatar.io/osu/peppy)
955
+ - Numeric user ID, e.g., [unavatar.io/osu/7562902](https://unavatar.io/osu/7562902)
956
+
940
957
  ### Patreon
941
958
 
942
959
  Get any Patreon creator's profile picture by their username.
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.56.0",
5
+ "version": "3.58.0",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
@@ -0,0 +1,67 @@
1
+ 'use strict'
2
+
3
+ const API_URL = 'https://dev.to/api'
4
+
5
+ const PLACEHOLDER_URL =
6
+ 'https://thepracticaldev.s3.amazonaws.com/i/99mvlsfu5tfj9m7ku25d.png'
7
+
8
+ const RESIZED_IMAGE_RE = /^(.+\/dynamic\/image\/)([^/]+)\/(https?%3A%2F%2F.+)$/i
9
+
10
+ const DIMENSION_RE = /\b(width|height)=\d+/g
11
+
12
+ const parseInput = input => input.trim().replace(/^@/, '').toLowerCase()
13
+
14
+ const getUserAvatarUrl = input =>
15
+ `${API_URL}/users/by_username?url=${encodeURIComponent(input)}`
16
+
17
+ const getOrganizationAvatarUrl = input =>
18
+ `${API_URL}/organizations/${encodeURIComponent(input)}`
19
+
20
+ const getAvatar = ({ body, size }) => {
21
+ const image = body?.profile_image
22
+ if (typeof image !== 'string' || !image) return
23
+
24
+ const match = RESIZED_IMAGE_RE.exec(image)
25
+ const originalUrl = match ? decodeURIComponent(match[3]) : image
26
+ if (originalUrl === PLACEHOLDER_URL) return
27
+ if (!match) return image
28
+
29
+ const [, prefix, options, encodedOriginalUrl] = match
30
+ const resizedOptions = options.replace(DIMENSION_RE, `$1=${size}`)
31
+ return `${prefix}${resizedOptions}/${encodedOriginalUrl}`
32
+ }
33
+
34
+ const fetchAvatarUrl = async ({ got, url, size }) => {
35
+ const { statusCode, body } = await got(url, {
36
+ responseType: 'json',
37
+ throwHttpErrors: false
38
+ })
39
+
40
+ if (statusCode >= 400) return
41
+
42
+ return getAvatar({ body, size })
43
+ }
44
+
45
+ module.exports = ({ constants, got }) =>
46
+ async function devto (input) {
47
+ const username = parseInput(input)
48
+ const size = constants.AVATAR_SIZE
49
+
50
+ const userAvatarUrl = await fetchAvatarUrl({
51
+ got,
52
+ size,
53
+ url: getUserAvatarUrl(username)
54
+ })
55
+ if (userAvatarUrl) return userAvatarUrl
56
+
57
+ return fetchAvatarUrl({
58
+ got,
59
+ size,
60
+ url: getOrganizationAvatarUrl(username)
61
+ })
62
+ }
63
+
64
+ module.exports.parseInput = parseInput
65
+ module.exports.getUserAvatarUrl = getUserAvatarUrl
66
+ module.exports.getOrganizationAvatarUrl = getOrganizationAvatarUrl
67
+ module.exports.getAvatar = getAvatar
@@ -20,6 +20,7 @@ const providerTiers = {
20
20
  'dailymotion',
21
21
  'deezer',
22
22
  'deviantart',
23
+ 'devto',
23
24
  'discord',
24
25
  'dockerhub',
25
26
  'dribbble',
@@ -45,6 +46,7 @@ const providerTiers = {
45
46
  'npm',
46
47
  'onlyfans',
47
48
  'openstreetmap',
49
+ 'osu',
48
50
  'patreon',
49
51
  'paypal',
50
52
  'pinterest',
@@ -114,6 +116,7 @@ module.exports = ctx => {
114
116
  dailymotion: require('./dailymotion')(ctx),
115
117
  deezer: require('./deezer')(ctx),
116
118
  deviantart: require('./deviantart')(ctx),
119
+ devto: require('./devto')(ctx),
117
120
  discord: require('./discord')(ctx),
118
121
  dockerhub: require('./dockerhub')(ctx),
119
122
  dribbble: require('./dribbble')(ctx),
@@ -143,6 +146,7 @@ module.exports = ctx => {
143
146
  npm: require('./npm')(ctx),
144
147
  onlyfans: require('./onlyfans')(ctx),
145
148
  openstreetmap: require('./openstreetmap')(ctx),
149
+ osu: require('./osu')(ctx),
146
150
  patreon: require('./patreon')(ctx),
147
151
  paypal: require('./paypal')(ctx),
148
152
  pinterest: require('./pinterest')(ctx),
@@ -0,0 +1,23 @@
1
+ 'use strict'
2
+
3
+ const PROFILE_URL = 'https://osu.ppy.sh/users'
4
+ const UPLOADED_AVATAR_ORIGIN = 'https://a.ppy.sh/'
5
+
6
+ const isUploadedAvatar = url => url.startsWith(UPLOADED_AVATAR_ORIGIN)
7
+
8
+ const getAvatarUrl = input => `${PROFILE_URL}/${encodeURIComponent(input)}`
9
+
10
+ const getAvatar = ({ $, getOgImage, NOT_FOUND }) => {
11
+ const image = getOgImage($)
12
+ return image && isUploadedAvatar(image) ? image : NOT_FOUND
13
+ }
14
+
15
+ module.exports = ({ createHtmlProvider, getOgImage, NOT_FOUND }) =>
16
+ createHtmlProvider({
17
+ name: 'osu',
18
+ url: getAvatarUrl,
19
+ getter: $ => getAvatar({ $, getOgImage, NOT_FOUND })
20
+ })
21
+
22
+ module.exports.getAvatarUrl = getAvatarUrl
23
+ module.exports.getAvatar = getAvatar