tesla-inventory 3.2.0-0 → 3.2.0-2
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/package.json +5 -6
- package/src/codes.json +1 -0
- package/src/index.js +70 -61
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "tesla-inventory",
|
|
3
3
|
"description": "Retrieve real-time data from Tesla Inventory.",
|
|
4
4
|
"homepage": "https://github.com/Kikobeats/tesla-inventory",
|
|
5
|
-
"version": "3.2.0-
|
|
5
|
+
"version": "3.2.0-2",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "josefrancisco.verdu@gmail.com",
|
|
@@ -34,14 +34,12 @@
|
|
|
34
34
|
"teslaapi",
|
|
35
35
|
"teslamotors"
|
|
36
36
|
],
|
|
37
|
-
"dependencies": {
|
|
38
|
-
"got": "11"
|
|
39
|
-
},
|
|
40
37
|
"devDependencies": {
|
|
41
38
|
"@commitlint/cli": "latest",
|
|
42
39
|
"@commitlint/config-conventional": "latest",
|
|
43
40
|
"@ksmithut/prettier-standard": "latest",
|
|
44
41
|
"ava": "latest",
|
|
42
|
+
"browserless": "latest",
|
|
45
43
|
"c8": "latest",
|
|
46
44
|
"ci-publish": "latest",
|
|
47
45
|
"conventional-github-releaser": "latest",
|
|
@@ -55,6 +53,8 @@
|
|
|
55
53
|
"markdown-tables-to-json": "latest",
|
|
56
54
|
"nano-staged": "latest",
|
|
57
55
|
"npm-check-updates": "latest",
|
|
56
|
+
"puppeteer": "latest",
|
|
57
|
+
"signal-exit": "latest",
|
|
58
58
|
"simple-git-hooks": "latest",
|
|
59
59
|
"standard": "latest",
|
|
60
60
|
"standard-markdown": "latest",
|
|
@@ -103,8 +103,7 @@
|
|
|
103
103
|
},
|
|
104
104
|
"nano-staged": {
|
|
105
105
|
"*.js": [
|
|
106
|
-
"prettier-standard"
|
|
107
|
-
"standard --fix"
|
|
106
|
+
"prettier-standard"
|
|
108
107
|
],
|
|
109
108
|
"*.md": [
|
|
110
109
|
"standard-markdown"
|
package/src/codes.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,73 +3,82 @@
|
|
|
3
3
|
const debug = require('debug-logfmt')('tesla-inventory')
|
|
4
4
|
const inventories = require('./inventories')
|
|
5
5
|
|
|
6
|
-
const ITEMS_PER_PAGE = 50
|
|
7
|
-
|
|
8
|
-
const uniqBy = (arr, prop) =>
|
|
9
|
-
arr.filter((x, i, self) => i === self.findIndex(y => x[prop] === y[prop]))
|
|
10
|
-
|
|
11
6
|
const timestamp =
|
|
12
7
|
(start = process.hrtime.bigint()) =>
|
|
13
8
|
() =>
|
|
14
9
|
Math.round(Number(process.hrtime.bigint() - start) / 1e6) + 'ms'
|
|
15
10
|
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
headers: { 'user-agent': undefined }
|
|
21
|
-
}
|
|
11
|
+
const uniqBy = (arr, prop) =>
|
|
12
|
+
arr.filter((x, i, self) => i === self.findIndex(y => x[prop] === y[prop]))
|
|
13
|
+
|
|
14
|
+
const ITEMS_PER_PAGE = 50
|
|
22
15
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return async (inventory, opts, gotOpts) => {
|
|
27
|
-
if (!inventories[inventory]) {
|
|
28
|
-
throw new TypeError(`Tesla inventory \`${inventory}\` not found!`)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (opts.model && !opts.model.startsWith('m')) {
|
|
32
|
-
opts.model = `m${opts.model}`
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const { country, ...query } = { ...inventories[inventory], ...opts }
|
|
36
|
-
|
|
37
|
-
const duration = timestamp()
|
|
38
|
-
|
|
39
|
-
const paginate = async (offset = 0) =>
|
|
40
|
-
got(
|
|
41
|
-
got.mergeOptions(
|
|
42
|
-
got.defaults.options,
|
|
43
|
-
{
|
|
44
|
-
searchParams: {
|
|
45
|
-
query: JSON.stringify({
|
|
46
|
-
query,
|
|
47
|
-
count: ITEMS_PER_PAGE,
|
|
48
|
-
offset,
|
|
49
|
-
outsideOffset: 0,
|
|
50
|
-
outsideSearch: false
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
gotOpts
|
|
55
|
-
)
|
|
56
|
-
).then(({ results }) => ({ items: results }))
|
|
57
|
-
|
|
58
|
-
let offset = 0
|
|
59
|
-
let items = []
|
|
60
|
-
let page
|
|
61
|
-
|
|
62
|
-
do {
|
|
63
|
-
page = await paginate(offset)
|
|
64
|
-
items = uniqBy(items.concat(page.items), 'VIN')
|
|
65
|
-
offset = items.length
|
|
66
|
-
debug({ items: items.length, duration: duration() })
|
|
67
|
-
} while (page.items.length >= ITEMS_PER_PAGE)
|
|
68
|
-
|
|
69
|
-
return items.filter(item => item.Model === opts.model)
|
|
16
|
+
module.exports = getBrowserless => async (inventory, opts, gotoOpts) => {
|
|
17
|
+
if (!inventories[inventory]) {
|
|
18
|
+
throw new TypeError(`Tesla inventory \`${inventory}\` not found!`)
|
|
70
19
|
}
|
|
71
|
-
}
|
|
72
20
|
|
|
73
|
-
|
|
21
|
+
let teardown
|
|
22
|
+
const browserless = await getBrowserless(fn => (teardown = fn))
|
|
74
23
|
|
|
75
|
-
|
|
24
|
+
if (opts.model && !opts.model.startsWith('m')) {
|
|
25
|
+
opts.model = `m${opts.model}`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { country, ...query } = { ...inventories[inventory], ...opts }
|
|
29
|
+
|
|
30
|
+
const duration = timestamp()
|
|
31
|
+
|
|
32
|
+
const paginate = async (offset = 0) => {
|
|
33
|
+
const fn = browserless.evaluate(async (page, response) => response.text(), {
|
|
34
|
+
abortTypes: ['image', 'stylesheet', 'font', 'other'],
|
|
35
|
+
adblock: false,
|
|
36
|
+
waitUntil: 'networkidle0',
|
|
37
|
+
animations: true,
|
|
38
|
+
...gotoOpts
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const url = new URL(
|
|
42
|
+
`https://www.tesla.com/inventory/api/v1/inventory-results?${new URLSearchParams({
|
|
43
|
+
query: JSON.stringify({
|
|
44
|
+
query,
|
|
45
|
+
count: ITEMS_PER_PAGE,
|
|
46
|
+
offset,
|
|
47
|
+
outsideOffset: 0,
|
|
48
|
+
outsideSearch: false
|
|
49
|
+
})
|
|
50
|
+
}).toString()}`
|
|
51
|
+
).toString()
|
|
52
|
+
|
|
53
|
+
debug.debug({ url, query })
|
|
54
|
+
|
|
55
|
+
const result = await fn(url).then(async text => {
|
|
56
|
+
try {
|
|
57
|
+
const body = JSON.parse(text)
|
|
58
|
+
return { items: body.results }
|
|
59
|
+
} catch (error) {
|
|
60
|
+
debug.error({ text, error })
|
|
61
|
+
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
|
|
62
|
+
await delay(10000)
|
|
63
|
+
return paginate(offset)
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
return result
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let offset = 0
|
|
71
|
+
let items = []
|
|
72
|
+
let page
|
|
73
|
+
|
|
74
|
+
do {
|
|
75
|
+
page = await paginate(offset)
|
|
76
|
+
items = uniqBy(items.concat(page.items), 'VIN')
|
|
77
|
+
offset = items.length
|
|
78
|
+
debug({ items: items.length, duration: duration() })
|
|
79
|
+
} while (page.items.length >= ITEMS_PER_PAGE)
|
|
80
|
+
|
|
81
|
+
const result = items.filter(item => item.Model === opts.model)
|
|
82
|
+
if (teardown) await teardown()
|
|
83
|
+
return result
|
|
84
|
+
}
|