forked from rfink/node-edmunds-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
165 lines (132 loc) · 4.7 KB
/
app.js
File metadata and controls
165 lines (132 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* app.js
* created by Marcel Baur (marcel.baur@soom-it.ch)
*/
var EdmundsClient = require('./test/..');
var server = require('./test/server');
var readlineSync = require('readline-sync');
var colors = require('colors');
var util = require('util');
var port = 4001;
var client = new EdmundsClient({apiKey: 'tf45g986ttqefx389t36ppmg'});
var make = "Audi";
var model = "A4";
var year = "2010";
/*
var make = "Tesla";
var model = "Model S";
var year = "2015";
*/
var treeCO2ConsumptionYearly = 21.8;
var averageKmYearly = 15000;
var kplcity;
var kplhighway;
var engine;
var emissionPerKm;
var emissionPerYear;
function readInput() {
make = readlineSync.question('Auto Marke: ');
model = readlineSync.question('Auto Modell: ');
year = readlineSync.question('Auto Jahrgang: ');
console.log('Suche nach ' + make.green + ' ' + model.green + ' ' + year.green + '...');
apiCall();
};
function apiCall() {
server.listen(port);
console.log('Server listening on port '.cyan + port);
var opts = {};
opts.make = make;
opts.model = model;
opts.year = year;
opts.view = 'full';
//server.setFileName('getallcardetails');
client.getAllCarDetails(opts, function onResponse(err, res) {
if (err) {
console.log('An error occurred while querying the API!'.red);
}
if (res.stylesCount == 0) {
console.log("Car could not be found!".red);
} else {
// Debug: log entire response
//console.log(util.inspect(res, {showHidden: false, depth: null}));
//console.log("ID: " + res.styles[0].make.id);
//console.log("HP: " + res.styles[0].engine.horsepower);
console.log(make + " " + model + " " + year + " Spezifikationen:");
engine = res.styles[0].engine.type;
console.log("Motor: " + engine);
//console.log("Getriebe: " + res.styles[0].transmission.transmissionType);
console.log("Gänge: " + res.styles[0].transmission.numberOfSpeeds);
console.log("Türen: " + res.styles[0].numOfDoors);
console.log("Preis: " + res.styles[0].price.baseMSRP);
console.log("Fahrzeugtyp: " + res.styles[0].categories.vehicleStyle);
kplcity = doMath(res.styles[0].MPG.city, 1, 1);
console.log("KPL Stadt: " + kplcity);
console.log("Kosten für 100km Stadt: " + doMath(kplcity, 1, 2) + "0 CHF");
kplhighway = doMath(res.styles[0].MPG.highway, 1, 1);
console.log("KPL Autobahn: " + kplhighway);
console.log("Kosten für 100km Autobahn: " + doMath(kplhighway, 1, 2) + "0 CHF");
emissionPerKm = doMath(res.styles[0].MPG.city, 0, 3);
console.log("Emissionen pro km: " + emissionPerKm + "g/km");
emissionPerYear = emissionPerKm * averageKmYearly / 1000;
console.log("Emissionen pro Jahr: " + doMath(emissionPerYear / 1000, 2, 0) + " Tonnen CO2");
console.log("Anzahl Bäume pro Jahr: " + doMath(emissionPerYear, 0, 4) + " Bäume");
}
//getImage();
console.log('Query done! '.green);
server.close();
console.log('Server stopped listening. Exiting.'.yellow);
});
}
function doMath(value, decimals, opt) {
decimals = +decimals;
// Keep value unchanged if opt = 0
if (opt == 0) {
value = +value;
}
// Convert mpg to kpl if opt = 1
if (opt == 1) {
value = +value * 0.425144;
// Calculate fuel costs if opt = 2
}
if (opt == 2) {
value = (100 / +value) * 1.40;
// Calculate emissions based on fuel type if opt = 3
}
if (opt == 3) {
if (engine != "diesel" && engine != "electric") {
value = 6760 / +value;
}
if (engine == "diesel") {
value = 7440 / +value;
}
if (engine == "electric") {
value = 0;
}
// Calculate Trees per Year if opt = 4
}
if (opt == 4) {
value = +value / treeCO2ConsumptionYearly;
}
if (isNaN(value))
return NaN;
// Shift
value = value.toString().split('e');
value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + 2) : 2)));
// Shift back
value = value.toString().split('e');
return (+(value[0] + 'e' + (value[1] ? (+value[1] - 2) : -2))).toFixed(decimals);
}
function getImage() {
var opts = {};
opts.make = make;
opts.model = model;
opts.year = year;
client.getCarImage(opts, function onResponse(err, res) {
if (err) {
console.log('An error occurred while querying the API!'.red);
} else {
console.log(util.inspect(res, {showHidden: false, depth: null}));
}
});
}
readInput();