metascraper-telegram 5.29.6 → 5.29.7

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 +39 -29
  3. package/package.json +6 -4
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,15 @@ const {
4
4
  author,
5
5
  date,
6
6
  image,
7
- loadIframe,
8
7
  memoizeOne,
9
8
  toRule
10
9
  } = require('@metascraper/helpers')
11
10
 
12
- const asyncMemoizeOne = require('async-memoize-one')
11
+ const memoize = require('@keyvhq/memoize')
13
12
  const { getDomain } = require('tldts')
13
+ const pReflect = require('p-reflect')
14
14
  const cssUrls = require('css-urls')
15
-
16
- const fromIframe = asyncMemoizeOne((url, $) => loadIframe(url, $.html()))
15
+ const got = require('got')
17
16
 
18
17
  const toAuthor = toRule(author)
19
18
  const toImage = toRule(image)
@@ -23,39 +22,50 @@ const TELEGRAM_DOMAINS = ['telegram.me', 't.me']
23
22
 
24
23
  const test = memoizeOne(url => TELEGRAM_DOMAINS.includes(getDomain(url)))
25
24
 
26
- module.exports = () => {
25
+ const createGetIframe = gotOpts => async (url, $) => {
26
+ const iframe = $('iframe')
27
+ if (iframe.length === 0) return ''
28
+
29
+ const src = iframe.attr('src')
30
+ if (!src) return ''
31
+
32
+ const { value: response } = await pReflect(got(src, gotOpts))
33
+ if (!response) return ''
34
+
35
+ return response.body
36
+ }
37
+
38
+ module.exports = ({ gotOpts, keyvOpts } = {}) => {
39
+ const getIframe = memoize(createGetIframe(gotOpts), keyvOpts, {
40
+ key: url => url
41
+ })
42
+
43
+ const loadIframe = fn => async ($, url) => {
44
+ const html = await getIframe(url, $)
45
+ return html.length ? fn($.load(html), url) : undefined
46
+ }
47
+
27
48
  const rules = {
28
49
  author: [toAuthor($ => $('meta[property="og:title"]').attr('content'))],
29
50
  logo: [toImage($ => $('meta[property="og:image"]').attr('content'))],
30
51
  image: [
31
- toImage(async ($, url) => {
32
- const iframe = await fromIframe(url, $)
33
- if (!iframe) return
52
+ toImage(
53
+ loadIframe(($iframe, url) => {
54
+ const el = $iframe(
55
+ '.link_preview_image, .link_preview_right_image, .tgme_widget_message_photo_wrap'
56
+ )
34
57
 
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')
58
+ if (el.length === 0) return
59
+ const [{ normalizedUrl: cssUrl }] = cssUrls({
60
+ text: el.css('background-image'),
61
+ url
62
+ })
39
63
 
40
- if (!el) return
41
-
42
- const [{ normalizedUrl: cssUrl }] = cssUrls({
43
- text: el.style['background-image'],
44
- url
64
+ return cssUrl
45
65
  })
46
-
47
- return cssUrl
48
- })
66
+ )
49
67
  ],
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
- ]
68
+ date: [toDate(loadIframe($iframe => $iframe('.datetime').attr('datetime')))]
59
69
  }
60
70
 
61
71
  rules.test = ({ url }) => test(url)
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.6",
5
+ "version": "5.29.7",
6
6
  "main": "index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -22,9 +22,11 @@
22
22
  "telegram"
23
23
  ],
24
24
  "dependencies": {
25
- "@metascraper/helpers": "^5.29.6",
26
- "async-memoize-one": "~1.1.1",
25
+ "@keyvhq/memoize": "~1.6.13",
26
+ "@metascraper/helpers": "^5.29.7",
27
27
  "css-urls": "~2.0.27",
28
+ "got": "~11.8.3",
29
+ "p-reflect": "~2.1.0",
28
30
  "tldts": "~5.7.80"
29
31
  },
30
32
  "devDependencies": {
@@ -40,5 +42,5 @@
40
42
  "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
41
43
  },
42
44
  "license": "MIT",
43
- "gitHead": "b05f3948ac8878aedee0cb21f3024779c78b6bf4"
45
+ "gitHead": "78434709c910cca5d8df3871c0b06131cce5df45"
44
46
  }