@unavatar/core 3.7.66 → 3.7.68

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/bin/index.js CHANGED
@@ -7,11 +7,26 @@ const mri = require('mri')
7
7
 
8
8
  module.exports = ({ baseUrl }) => {
9
9
  const { _, ...flags } = mri(process.argv.slice(2), {
10
- default: {}
10
+ default: {},
11
+ alias: { H: 'header' }
11
12
  })
12
13
 
13
14
  const [input] = _
14
15
 
16
+ const parseHeaders = raw => {
17
+ const entries = raw == null ? [] : Array.isArray(raw) ? raw : [raw]
18
+ return entries.reduce((headers, entry) => {
19
+ const idx = entry.indexOf(':')
20
+ if (idx === -1) return headers
21
+ const name = entry.slice(0, idx).trim()
22
+ const value = entry.slice(idx + 1).trim()
23
+ if (name) headers[name.toLowerCase()] = value
24
+ return headers
25
+ }, {})
26
+ }
27
+
28
+ const customHeaders = parseHeaders(flags.header)
29
+
15
30
  if (!input) {
16
31
  console.error('Usage: unavatar <input> | unavatar <provider>/<key> | unavatar ping')
17
32
  console.error(
@@ -87,7 +102,10 @@ module.exports = ({ baseUrl }) => {
87
102
  }
88
103
  }
89
104
 
90
- got(apiUrl.toString(), { headers: { 'x-api-key': flags.apiKey }, responseType: 'json' })
105
+ const requestHeaders = { ...customHeaders }
106
+ if (flags.apiKey) requestHeaders['x-api-key'] = flags.apiKey
107
+
108
+ got(apiUrl.toString(), { headers: requestHeaders, responseType: 'json' })
91
109
  .then(({ body, headers, statusCode, timings }) => {
92
110
  apiHeaders = headers
93
111
  apiStatusCode = statusCode
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.7.66",
5
+ "version": "3.7.68",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
@@ -1,8 +1,11 @@
1
1
  'use strict'
2
2
 
3
+ const randomCrawlerAgent = require('../util/crawler-agent')
4
+
3
5
  module.exports = ({ createHtmlProvider, getOgImage }) =>
4
6
  createHtmlProvider({
5
7
  name: 'instagram',
6
8
  url: input => `https://www.instagram.com/${input}`,
7
- getter: getOgImage
9
+ getter: getOgImage,
10
+ htmlOpts: () => ({ headers: { 'user-agent': randomCrawlerAgent() } })
8
11
  })
@@ -2,9 +2,37 @@
2
2
 
3
3
  const { $jsonld } = require('@metascraper/helpers')
4
4
 
5
- module.exports = ({ createHtmlProvider }) =>
5
+ const unescapeUnicode = str =>
6
+ str.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) =>
7
+ String.fromCharCode(parseInt(hex, 16))
8
+ )
9
+
10
+ const getRscAvatar = $ => {
11
+ let url
12
+ $('script').each((_, el) => {
13
+ const text = $(el).html() || ''
14
+ if (!text.includes('avatarPhotoImageUrls')) return
15
+ const match = text.match(
16
+ /avatarPhotoImageUrls[\s\S]*?\\"original\\":\\"((?:[^\\"]|\\u[0-9a-fA-F]{4})+)/
17
+ )
18
+ if (match) {
19
+ url = unescapeUnicode(match[1])
20
+ return false
21
+ }
22
+ })
23
+ return url
24
+ }
25
+
26
+ const getAvatar = $ =>
27
+ $jsonld('mainEntity.image.contentUrl')($) || getRscAvatar($)
28
+
29
+ const factory = ({ createHtmlProvider }) =>
6
30
  createHtmlProvider({
7
31
  name: 'patreon',
8
32
  url: username => `https://www.patreon.com/${username}`,
9
- getter: $ => $jsonld('mainEntity.image.thumbnailUrl')($)
33
+ getter: getAvatar
10
34
  })
35
+
36
+ factory.getAvatar = getAvatar
37
+
38
+ module.exports = factory
@@ -1,11 +1,8 @@
1
1
  'use strict'
2
2
 
3
- const uniqueRandomArray = require('unique-random-array')
4
3
  const { $jsonld } = require('@metascraper/helpers')
5
4
 
6
- const randomCrawlerAgent = uniqueRandomArray(
7
- require('top-crawler-agents').filter(agent => agent.startsWith('Slackbot'))
8
- )
5
+ const randomCrawlerAgent = require('../util/crawler-agent')
9
6
 
10
7
  const toHighResolution = url => {
11
8
  if (url?.endsWith('_200x200.jpg')) {
@@ -0,0 +1,8 @@
1
+ 'use strict'
2
+
3
+ const uniqueRandomArray = require('unique-random-array')
4
+
5
+ // TODO: update top-crawler-agents to don't make necessary to filter.
6
+ module.exports = uniqueRandomArray(
7
+ require('top-crawler-agents').filter(agent => agent.startsWith('Slackbot'))
8
+ )
@@ -35,6 +35,11 @@ module.exports = ({ PROXY_TIMEOUT, getHTML, onFetchHTML }) => {
35
35
 
36
36
  const { $, statusCode } = await getHTML(providerUrl, fetchOpts)
37
37
 
38
+ attempt.lastHtml =
39
+ typeof $ === 'function' && typeof $.html === 'function'
40
+ ? $.html()
41
+ : undefined
42
+
38
43
  if (isStatusCodeMissing(statusCode)) {
39
44
  log.error({ statusCode, status: 'missing_status_code' })
40
45
  throw createEmptyProviderValueError({ provider: name, statusCode })
@@ -51,10 +56,7 @@ module.exports = ({ PROXY_TIMEOUT, getHTML, onFetchHTML }) => {
51
56
  provider: name,
52
57
  statusCode
53
58
  })
54
- error.html =
55
- typeof $ === 'function' && typeof $.html === 'function'
56
- ? $.html()
57
- : undefined
59
+ error.html = attempt.lastHtml
58
60
 
59
61
  log.error({ statusCode })
60
62