tesla-inventory 3.0.237 → 3.0.239
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 +4 -1
- package/src/index.js +27 -22
- package/src/prices/dk.json +1 -1
- package/src/prices/gb.json +1 -1
- package/src/prices/mx.json +1 -1
- package/src/prices/us.json +5 -5
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.0.
|
|
5
|
+
"version": "3.0.239",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "josefrancisco.verdu@gmail.com",
|
|
@@ -82,6 +82,9 @@
|
|
|
82
82
|
"update:dependencies": "ncu -u"
|
|
83
83
|
},
|
|
84
84
|
"license": "MIT",
|
|
85
|
+
"ava": {
|
|
86
|
+
"timeout": "2m"
|
|
87
|
+
},
|
|
85
88
|
"commitlint": {
|
|
86
89
|
"extends": [
|
|
87
90
|
"@commitlint/config-conventional"
|
package/src/index.js
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const debug = require('debug-logfmt')('tesla-inventory')
|
|
3
4
|
const inventories = require('./inventories')
|
|
4
5
|
|
|
6
|
+
const timestamp = (start = process.hrtime.bigint()) => () =>
|
|
7
|
+
Math.round(Number(process.hrtime.bigint() - start) / 1e6) + 'ms'
|
|
8
|
+
|
|
5
9
|
const got = require('got').extend({
|
|
6
10
|
url: 'https://www.tesla.com/inventory/api/v1/inventory-results',
|
|
7
11
|
responseType: 'json',
|
|
8
12
|
resolveBodyOnly: true
|
|
9
13
|
})
|
|
10
14
|
|
|
11
|
-
const toLowerCase = ({ results: items, total_matches_found: totalMatchesFound }) =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
const toLowerCase = ({ results: items, total_matches_found: totalMatchesFound, ...rest }) => {
|
|
16
|
+
return {
|
|
17
|
+
items,
|
|
18
|
+
total: Number(totalMatchesFound)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const uniqBy = (arr, prop) =>
|
|
23
|
+
arr.filter((x, i, self) => i === self.findIndex(y => x[prop] === y[prop]))
|
|
15
24
|
|
|
16
25
|
const ITEMS_PER_PAGE = 50
|
|
17
26
|
|
|
@@ -26,12 +35,14 @@ module.exports = async (inventory, opts, { headers, ...gotOpts } = {}) => {
|
|
|
26
35
|
opts.model = `m${opts.model}`
|
|
27
36
|
}
|
|
28
37
|
|
|
29
|
-
const
|
|
38
|
+
const duration = timestamp()
|
|
39
|
+
|
|
40
|
+
const paginate = async (outsideOffset = 0) =>
|
|
30
41
|
got({
|
|
31
42
|
searchParams: {
|
|
32
43
|
query: JSON.stringify({
|
|
33
44
|
outsideOffset,
|
|
34
|
-
count:
|
|
45
|
+
count: ITEMS_PER_PAGE,
|
|
35
46
|
query: {
|
|
36
47
|
...inventoryProps,
|
|
37
48
|
...opts
|
|
@@ -42,22 +53,16 @@ module.exports = async (inventory, opts, { headers, ...gotOpts } = {}) => {
|
|
|
42
53
|
headers: { 'user-agent': undefined, ...headers }
|
|
43
54
|
}).then(toLowerCase)
|
|
44
55
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const nRequests = Math.ceil(page.total / ITEMS_PER_PAGE) - 1
|
|
49
|
-
const offsets = [...Array(nRequests).keys()].map(n => (n + 1) * page.items.length)
|
|
50
|
-
const pages = await Promise.all(offsets.map(paginate))
|
|
51
|
-
return [page, ...pages].reduce((acc, page) => {
|
|
52
|
-
page.items.forEach(item => {
|
|
53
|
-
const isAlready = acc.some(({ VIN }) => VIN === item.VIN)
|
|
54
|
-
if (!isAlready) acc.push(item)
|
|
55
|
-
})
|
|
56
|
-
return acc
|
|
57
|
-
}, [])
|
|
58
|
-
}
|
|
56
|
+
let offset = 0
|
|
57
|
+
let items = []
|
|
58
|
+
let page
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
do {
|
|
61
|
+
page = await paginate(offset)
|
|
62
|
+
items = uniqBy(items.concat(page.items), 'VIN')
|
|
63
|
+
offset = items.length
|
|
64
|
+
debug({ items: items.length, duration: duration() })
|
|
65
|
+
} while (page.items.length >= ITEMS_PER_PAGE)
|
|
61
66
|
|
|
62
|
-
return
|
|
67
|
+
return items.filter(item => item.Model === opts.model)
|
|
63
68
|
}
|
package/src/prices/dk.json
CHANGED
package/src/prices/gb.json
CHANGED
package/src/prices/mx.json
CHANGED
package/src/prices/us.json
CHANGED
|
@@ -41,21 +41,21 @@
|
|
|
41
41
|
"IWW00": 2000,
|
|
42
42
|
"MT10A": 15000,
|
|
43
43
|
"MT10L": 36590,
|
|
44
|
-
"MT301":
|
|
44
|
+
"MT301": 7990,
|
|
45
45
|
"MT302": 8900,
|
|
46
46
|
"MT303": 13990,
|
|
47
47
|
"MT304": 18990,
|
|
48
48
|
"MT305": 15900,
|
|
49
|
-
"MT308":
|
|
49
|
+
"MT308": 7990,
|
|
50
50
|
"MT310": 14990,
|
|
51
51
|
"MT311": 18990,
|
|
52
|
-
"MT314":
|
|
52
|
+
"MT314": 7990,
|
|
53
53
|
"MT315": 14990,
|
|
54
54
|
"MT317": 18990,
|
|
55
55
|
"MT321": 14990,
|
|
56
|
-
"MT322":
|
|
56
|
+
"MT322": 7990,
|
|
57
57
|
"MT324": 14990,
|
|
58
|
-
"MT337":
|
|
58
|
+
"MT337": 7990,
|
|
59
59
|
"MT75A": 15000,
|
|
60
60
|
"MT75R": 18000,
|
|
61
61
|
"MT90A": 15000,
|