Last active
December 30, 2020 18:05
-
-
Save eiannone/90da6cd236bd25d3b708623f0b98d5ae to your computer and use it in GitHub Desktop.
Script per ottenere il vehicle id di un'auto Tesla
This file contains 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
import {request} from 'https'; | |
function errore(msg) { | |
console.log(msg) | |
process.exit(-1); | |
} | |
if (process.argv.length < 3) { | |
errore("Richiamare lo script passando il token di accesso per le API Tesla"); | |
} | |
const req = request('https://owner-api.teslamotors.com/api/1/vehicles/', { | |
headers: { 'user-agent': "TeslaEma", 'Authorization': "Bearer " + process.argv[2] }, | |
timeout: 30000, | |
method: 'GET' | |
}, res => { | |
if (res.statusCode > 199 && res.statusCode < 300) { | |
res.setEncoding('utf8'); | |
let rawData = ''; | |
res.on('data', chunk => { rawData += chunk; }); | |
res.on('end', () => { | |
try { | |
const vehicles = JSON.parse(rawData).response; | |
vehicles.forEach(v => { | |
console.log(v.display_name+": vehicle_id="+v.id+", state="+v.state); | |
}); | |
} catch(err) { | |
errore("Errore: " + err); | |
} | |
}); | |
} else { | |
errore("Errore: " + res.statusMessage + " ("+res.statusCode+")"); | |
} | |
}); | |
req.on('error', e => errore("Errore: " + e.message + " ("+e.code+")")); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment