metascraper-telegram 5.29.5 → 5.29.9

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.
Files changed (3) hide show
  1. package/README.md +18 -0
  2. package/index.js +42 -31
  3. package/package.json +10 -18
package/README.md CHANGED
@@ -14,6 +14,24 @@
14
14
  $ npm install metascraper-telegram --save
15
15
  ```
16
16
 
17
+ ## API
18
+
19
+ ### metascraper-telegram([options])
20
+
21
+ #### options
22
+
23
+ ##### gotOpts
24
+
25
+ Type: `object`
26
+
27
+ Any option provided here will passed to [got#options](https://github.com/sindresorhus/got#options).
28
+
29
+ ##### keyvOpts
30
+
31
+ Type: `object`
32
+
33
+ Any option provided here will passed to [@keyvhq/memoize#options](https://github.com/microlinkhq/keyv/tree/master/packages/memoize#keyvoptions).
34
+
17
35
  ## License
18
36
 
19
37
  **metascraper-telegram** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
package/index.js CHANGED
@@ -4,16 +4,16 @@ const {
4
4
  author,
5
5
  date,
6
6
  image,
7
- loadIframe,
8
7
  memoizeOne,
8
+ sanetizeUrl,
9
9
  toRule
10
10
  } = require('@metascraper/helpers')
11
11
 
12
- const asyncMemoizeOne = require('async-memoize-one')
12
+ const memoize = require('@keyvhq/memoize')
13
13
  const { getDomain } = require('tldts')
14
+ const pReflect = require('p-reflect')
14
15
  const cssUrls = require('css-urls')
15
-
16
- const fromIframe = asyncMemoizeOne((url, $) => loadIframe(url, $.html()))
16
+ const got = require('got')
17
17
 
18
18
  const toAuthor = toRule(author)
19
19
  const toImage = toRule(image)
@@ -21,44 +21,55 @@ const toDate = toRule(date)
21
21
 
22
22
  const TELEGRAM_DOMAINS = ['telegram.me', 't.me']
23
23
 
24
- const isValidUrl = memoizeOne(url => TELEGRAM_DOMAINS.includes(getDomain(url)))
24
+ const test = memoizeOne(url => TELEGRAM_DOMAINS.includes(getDomain(url)))
25
+
26
+ const createGetIframe = gotOpts => async (url, $) => {
27
+ const iframe = $('iframe')
28
+ if (iframe.length === 0) return ''
29
+
30
+ const src = iframe.attr('src')
31
+ if (!src) return ''
32
+
33
+ const { value: response } = await pReflect(got(src, gotOpts))
34
+ if (!response) return ''
35
+
36
+ return response.body
37
+ }
38
+
39
+ module.exports = ({ gotOpts, keyvOpts } = {}) => {
40
+ const getIframe = memoize(createGetIframe(gotOpts), keyvOpts, {
41
+ key: url => sanetizeUrl(url, { removeQueryParameters: true })
42
+ })
43
+
44
+ const loadIframe = fn => async ($, url) => {
45
+ const html = await getIframe(url, $)
46
+ return html.length ? fn($.load(html), url) : undefined
47
+ }
25
48
 
26
- module.exports = () => {
27
49
  const rules = {
28
50
  author: [toAuthor($ => $('meta[property="og:title"]').attr('content'))],
29
51
  logo: [toImage($ => $('meta[property="og:image"]').attr('content'))],
30
52
  image: [
31
- toImage(async ($, url) => {
32
- const iframe = await fromIframe(url, $)
33
- if (!iframe) return
53
+ toImage(
54
+ loadIframe(($iframe, url) => {
55
+ const el = $iframe(
56
+ '.link_preview_image, .link_preview_right_image, .tgme_widget_message_photo_wrap'
57
+ )
34
58
 
35
- const el =
36
- iframe.document.querySelector('.link_preview_image') ||
37
- iframe.document.querySelector('.link_preview_right_image') ||
38
- iframe.document.querySelector('.tgme_widget_message_photo_wrap')
59
+ if (el.length === 0) return
60
+ const [{ normalizedUrl: cssUrl }] = cssUrls({
61
+ text: el.css('background-image'),
62
+ url
63
+ })
39
64
 
40
- if (!el) return
41
-
42
- const [{ normalizedUrl: cssUrl }] = cssUrls({
43
- text: el.style['background-image'],
44
- url
65
+ return cssUrl
45
66
  })
46
-
47
- return cssUrl
48
- })
67
+ )
49
68
  ],
50
- date: [
51
- toDate(async ($, url) => {
52
- const iframe = await fromIframe(url, $)
53
- if (!iframe) return
54
-
55
- const el = iframe.document.querySelector('.datetime')
56
- return el ? el.getAttribute('datetime') : undefined
57
- })
58
- ]
69
+ date: [toDate(loadIframe($iframe => $iframe('.datetime').attr('datetime')))]
59
70
  }
60
71
 
61
- rules.test = ({ url }) => isValidUrl(url)
72
+ rules.test = ({ url }) => test(url)
62
73
 
63
74
  return rules
64
75
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-telegram",
3
3
  "description": "Metascraper integration with Telegram",
4
4
  "homepage": "https://nicedoc.io/microlinkhq/metascraper/packages/metascraper-telegram",
5
- "version": "5.29.5",
5
+ "version": "5.29.9",
6
6
  "main": "index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -22,15 +22,15 @@
22
22
  "telegram"
23
23
  ],
24
24
  "dependencies": {
25
- "@metascraper/helpers": "^5.29.5",
26
- "async-memoize-one": "~1.1.0",
27
- "css-urls": "~2.0.26",
28
- "tldts": "~5.7.77"
25
+ "@keyvhq/memoize": "~1.6.13",
26
+ "@metascraper/helpers": "^5.29.7",
27
+ "css-urls": "~2.0.27",
28
+ "got": "~11.8.3",
29
+ "p-reflect": "~2.1.0",
30
+ "tldts": "~5.7.80"
29
31
  },
30
32
  "devDependencies": {
31
- "mocha": "latest",
32
- "should": "latest",
33
- "snap-shot": "latest"
33
+ "ava": "latest"
34
34
  },
35
35
  "engines": {
36
36
  "node": ">= 12"
@@ -39,16 +39,8 @@
39
39
  "index.js"
40
40
  ],
41
41
  "scripts": {
42
- "test": "NODE_PATH=.. TZ=UTC NODE_ENV=test mocha test"
42
+ "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
43
43
  },
44
44
  "license": "MIT",
45
- "mocha": {
46
- "bail": true,
47
- "exit": true,
48
- "recursive": true,
49
- "reporter": "spec",
50
- "slow": 300,
51
- "timeout": "120000"
52
- },
53
- "gitHead": "71c16e166a7775641957126288741d9051925e33"
45
+ "gitHead": "381c11e82ba75589922a0c75f7a65d49184e3619"
54
46
  }