Last active
June 11, 2019 16:19
-
-
Save MattJeanes/0f7b65ee17bc8b71b5e897aa0e734be7 to your computer and use it in GitHub Desktop.
Checks delivery estimate and updates if it changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Nightmare = require('nightmare') | |
const request = require('request-promise') | |
const fs = require('fs'); | |
const email = "xxxxx"; | |
const encodedPassword = "xxxxx"; | |
const expectedFile = "./expected.json"; | |
const expected = require(expectedFile); | |
const pushoverToken = "xxxxx"; | |
const pushoverUser = "xxxxx"; | |
const isLiveFile = "/var/www/isthemodel3ukconfiguratorliveyet.mattjeanes.com/index.html" | |
async function main() { | |
try { | |
const args = process.argv.slice(2); | |
if (args.includes("--delivery")) { | |
await checkDeliveryEstimate(); | |
} | |
if (args.includes("--orderpage")) { | |
await checkOrderPageDestination(); | |
} | |
if (args.includes("--data")) { | |
await checkProductData(); | |
} | |
} catch (e) { | |
const error = `Failed to check Tesla info: ${e.toString()}`; | |
console.log(error); | |
await sendAlert(error); | |
} | |
} | |
async function checkDeliveryEstimate() { | |
console.log("Getting delivery estimate from Tesla"); | |
const deliveryEstimate = await getDeliveryEstimate(); | |
console.log(`Delivery estimate is currently ${deliveryEstimate}, expected is ${expected.deliveryEstimate}`); | |
if (deliveryEstimate !== expected.deliveryEstimate) { | |
console.log(`OMG! Delivery estimate has changed!! Tell Matt!`); | |
await sendAlert(`Tesla delivery estimate has changed from ${expected.deliveryEstimate} to ${deliveryEstimate}`); | |
expected.deliveryEstimate = deliveryEstimate; | |
fs.writeFileSync(expectedFile, JSON.stringify(expected)); | |
} | |
} | |
async function checkOrderPageDestination() { | |
console.log("Getting order page destination from Tesla") | |
const orderPageDestination = await getOrderPageDestination(); | |
console.log(`Order page destination is currently ${orderPageDestination}, expected is ${expected.orderPageDestination}`); | |
if (orderPageDestination !== expected.orderPageDestination) { | |
console.log('OMG! Order page destination has changed!! Tell Matt!'); | |
await sendAlert(`Tesla order page has changed from ${expected.orderPageDestination} to ${orderPageDestination}`); | |
expected.orderPageDestination = orderPageDestination; | |
fs.writeFileSync(expectedFile, JSON.stringify(expected)); | |
fs.writeFileSync(isLiveFile, "<h1>Yes!!! 😍😍😍</h1>"); | |
} | |
} | |
function removeKeys(data, keys) { | |
for (const key in data) { | |
if (!data.hasOwnProperty(key)) { continue; } | |
if (keys.some(x => key.toLowerCase() == x.toLowerCase())) { | |
delete data[key]; | |
} else if (typeof (data[key]) === "object") { | |
removeKeys(data[key], keys); | |
} | |
} | |
} | |
async function checkProductData() { | |
console.log("Checking Product data"); | |
const productData = await getProductData(); | |
removeKeys(productData, ["labels", "states", "formattedAutoPilotStrings", "token", "form", "referrerToken", "assistance", "assistance_US_CA", "orderAgreementLink", "financingInstructionsLink", "Locale", "OwnersManualUrl"]); | |
console.log(`Product data is currently ${JSON.stringify(productData)}`) | |
console.log("---------------------------------------------------------------------"); | |
console.log(`Expected is ${JSON.stringify(expected.productData)}`); | |
if (JSON.stringify(productData) !== JSON.stringify(expected.productData)) { | |
console.log('OMG! Product data has changed!! Tell Matt!'); | |
await sendAlert(`Tesla product data has changed, VIN is ${productData.Vin}, check logs for full details`); | |
expected.productData = productData; | |
fs.writeFileSync(expectedFile, JSON.stringify(expected)); | |
} | |
} | |
async function sendAlert(message) { | |
await request("https://api.pushover.net/1/messages.json", { | |
method: "POST", json: { | |
token: pushoverToken, | |
user: pushoverUser, | |
priority: 1, | |
// retry: 30, | |
// expire: 10800, | |
sound: "echo", | |
message | |
} | |
}) | |
} | |
function getDeliveryEstimate() { | |
const nightmare = new Nightmare(); | |
return nightmare | |
.goto('https://tesla.com/model3/delivery-estimate') | |
.wait('form') | |
.insert('input[placeholder="Email"]', email) | |
.insert('input[placeholder="Password"]', Buffer.from(encodedPassword, "base64").toString("utf8")) | |
.click('.login-button') | |
.wait('.option-date') | |
.evaluate(() => document.querySelector(".option-date").innerHTML) | |
.end(); | |
} | |
function getOrderPageDestination() { | |
const nightmare = new Nightmare(); | |
return nightmare | |
.goto('https://www.tesla.com/en_GB/model3/design') | |
.evaluate(() => window.location.href) | |
.end(); | |
} | |
function getProductData() { | |
const nightmare = new Nightmare(); | |
return nightmare | |
.goto('https://www.tesla.com/en_GB/teslaaccount/product-finalize?rn=RNxxxxxxx') | |
.wait('form') | |
.insert('input[placeholder="Email"]', email) | |
.insert('input[placeholder="Password"]', Buffer.from(encodedPassword, "base64").toString("utf8")) | |
.click('.login-button') | |
.wait('.product-manage') | |
.click('.product-manage') | |
.wait('.content-value') | |
.evaluate(() => window.Tesla.ProductF.Data) | |
.end(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment