metascraper-tiktok 5.49.12

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 ADDED
@@ -0,0 +1,27 @@
1
+ <div align="center">
2
+ <br>
3
+ <img style="width: 500px; margin:3rem 0 1.5rem;" src="https://metascraper.js.org/static/logo-banner.png" alt="metascraper">
4
+ <br>
5
+ <br>
6
+ <p align="center"><strong>metascraper-tiktok</strong>: Metascraper integration with tiktok.com.</p>
7
+ <p align="center">See our <a href="https://metascraper.js.org" target='_blank' rel='noopener noreferrer'>website</a> for more information.</p>
8
+ <br>
9
+ </div>
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ $ npm install metascraper-tiktok --save
15
+ ```
16
+
17
+ ## API
18
+
19
+ ### metascraper-tiktok([options])
20
+
21
+ ## License
22
+
23
+ **metascraper-tiktok** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
24
+ Authored and maintained by [Microlink](https://microlink.io) with help from [contributors](https://github.com/microlinkhq/metascraper/contributors).
25
+
26
+ > [microlink.io](https://microlink.io) · GitHub [microlinkhq](https://github.com/microlinkhq) · X [@microlinkhq](https://x.com/microlinkhq)
27
+
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "metascraper-tiktok",
3
+ "description": "Metascraper integration with tiktok.com",
4
+ "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-tiktok",
5
+ "version": "5.49.12",
6
+ "types": "src/index.d.ts",
7
+ "main": "src/index.js",
8
+ "author": {
9
+ "email": "hello@microlink.io",
10
+ "name": "microlink.io",
11
+ "url": "https://microlink.io"
12
+ },
13
+ "repository": {
14
+ "directory": "packages/metascraper-tiktok",
15
+ "type": "git",
16
+ "url": "git+https://github.com/microlinkhq/metascraper.git"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/microlinkhq/metascraper/issues"
20
+ },
21
+ "keywords": [
22
+ "metascraper",
23
+ "tiktok",
24
+ "video"
25
+ ],
26
+ "dependencies": {
27
+ "@metascraper/helpers": "workspace:*"
28
+ },
29
+ "devDependencies": {
30
+ "ava": "5"
31
+ },
32
+ "engines": {
33
+ "node": ">= 16"
34
+ },
35
+ "files": [
36
+ "src"
37
+ ],
38
+ "scripts": {
39
+ "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
40
+ },
41
+ "license": "MIT"
42
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ declare function rules(): import('metascraper').Rules;
2
+ export = rules;
package/src/index.js ADDED
@@ -0,0 +1,80 @@
1
+ 'use strict'
2
+
3
+ const {
4
+ author,
5
+ date,
6
+ memoizeOne,
7
+ parseUrl,
8
+ title,
9
+ toRule
10
+ } = require('@metascraper/helpers')
11
+
12
+ const toAuthor = toRule(author)
13
+ const toDate = toRule(date)
14
+ const toTitle = toRule(title)
15
+
16
+ const test = memoizeOne(url => parseUrl(url).domain === 'tiktok.com')
17
+
18
+ // Assumes a valid TikTok Snowflake-style video ID
19
+ const getTimestampFromId = id => {
20
+ const timestamp = Number(BigInt(id) >> 32n)
21
+ return new Date(timestamp * 1000).toISOString()
22
+ }
23
+
24
+ const getAuthorAndUsername = memoizeOne((url, $) => {
25
+ const ogTitle = $('meta[property="og:title"]').attr('content')
26
+ const authorName = ogTitle
27
+ ?.split(' on TikTok')[0]
28
+ ?.split(' (@')[0]
29
+ ?.split(' | TikTok')[0]
30
+ ?.trim()
31
+ const username = url.split('@')[1]?.split('/')[0]
32
+ return { authorName, username }
33
+ }, memoizeOne.EqualityFirstArgument)
34
+
35
+ module.exports = () => {
36
+ const rules = {
37
+ author: [
38
+ toAuthor(($, url) => {
39
+ const { authorName } = getAuthorAndUsername(url, $)
40
+ return authorName
41
+ })
42
+ ],
43
+ title: [
44
+ toTitle(($, url) => {
45
+ const { authorName, username } = getAuthorAndUsername(url, $)
46
+ return authorName && username
47
+ ? `${authorName} (@${username}) on TikTok`
48
+ : authorName
49
+ })
50
+ ],
51
+ date: [
52
+ toDate((_, url) => {
53
+ const id = url.split('/video/')[1]?.split('?')[0]
54
+ return id ? getTimestampFromId(id) : undefined
55
+ }),
56
+ toDate($ => {
57
+ const content = $(
58
+ 'script[id="__UNIVERSAL_DATA_FOR_REHYDRATION__"]'
59
+ ).text()
60
+ if (!content) return
61
+ try {
62
+ const json = JSON.parse(content)
63
+ const createTime =
64
+ json.__DEFAULT_SCOPE__?.['webapp.user-detail']?.userInfo?.user
65
+ ?.createTime
66
+ return createTime
67
+ ? new Date(createTime * 1000).toISOString()
68
+ : undefined
69
+ } catch (_) {}
70
+ })
71
+ ]
72
+ }
73
+
74
+ rules.test = ({ url }) => test(url)
75
+ rules.pkgName = 'metascraper-tiktok'
76
+
77
+ return rules
78
+ }
79
+
80
+ module.exports.test = test