-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgas.py
executable file
·431 lines (344 loc) · 12 KB
/
gas.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/local/bin python
#
##############################################
# Copyright 2013 Michael Neeley
#
# See the file LICENSE for copying permission.
##############################################
"""
This application will calculate how much you will spend on gas per month.
It will ask for home address, work address, and year, make, and model of
the vehicle being driven.
API Credits:
Google Maps Directions Matrix
Google Maps Geocode
myGasFeed.com
fueleconomy.gov
"""
# TODO
#
# !2 debug functions for user error
# !2 debug functions for connection errors
# imports
import time
import urllib2 as url
import simplejson as json
from xml.etree import ElementTree as et
#from PIL import Image
def _googleStrip(input):
"""This will parse a string into Google Maps API format"""
output = ""
for char in input:
if char not in (" ", ","):
output += char
plus = False
else:
if plus == False:
output += "+"
plus = True
return output
def _processDistanceAPI(response):
html = response.read()
mapsJSON = json.loads(html)
distanceStr = mapsJSON[u"rows"][0][u"elements"][0][u"distance"][u"text"]
distance = int(float(distanceStr.split(" ")[0]))
return distance
def _processGeocodeAPI(response):
html = response.read()
mapsJSON = json.loads(html)
address = mapsJSON[u"results"][0][u"formatted_address"]
addressParts = address.split(", ")
name = addressParts[0] + ", " + addressParts[1] + ", " + addressParts[2][0:2]
lat = float(mapsJSON[u"results"][0][u"geometry"][u"location"][u"lat"])
long = float(mapsJSON[u"results"][0][u"geometry"][u"location"][u"lng"])
return [name, lat, long]
def _processGasAPI(response):
html = response.read()
gasJSON = json.loads(html)
totalPrice = 0
noPrice = 0
prices = []
addresses = []
for i, station in enumerate(gasJSON[u"stations"]):
if station[u"price"] != "N/A":
price = float(station[u"price"])
date = station[u"date"]
address = station[u"address"] + " " + station[u"city"] + " " + station[u"region"]
prices.append(price)
addresses.append(address)
totalPrice += price
else: noPrice += 1
avgPrice = totalPrice / (i + 1 - noPrice)
return [avgPrice, prices, addresses]
def _processYearMenuAPI(response):
html = response.read()
root = et.fromstring(html)
yearList = []
for menuItem in root:
if len(menuItem):
yearList.append(menuItem[0].text)
return [int(yearList[len(yearList) - 1]), int(yearList[0])]
def _processMakeMenuAPI(response):
html = response.read()
root = et.fromstring(html)
makeList = []
for menuItem in root:
if len(menuItem):
makeList.append(menuItem[0].text)
return makeList
def _processModelMenuAPI(response):
html = response.read()
root = et.fromstring(html)
modelList = []
for menuItem in root:
modelList.append(menuItem[0].text)
return modelList
def _processModelTypeMenuAPI(response):
html = response.read()
root = et.fromstring(html)
modelTypeList = []
for menuItem in root:
modelType = menuItem.find("text").text
vehicleID = int(menuItem.find("value").text)
modelTypeList.append([modelType, vehicleID])
return modelTypeList
def _processFuelEconomyAPI(response):
html = response.read()
root = et.fromstring(html)
return int(root.find("comb08").text)
def getDistance(startStr = "Start", endStr = "End", question = "How far?\n"):
"""Gets the distance between two locations."""
# ask for the origin and destination
startR = raw_input("%s address: " % startStr)
endR = raw_input("%s address: " % endStr)
# parse raw inputs
start = _googleStrip(startR)
end = _googleStrip(endR)
# Google Maps Distance API - get distance to work
request = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="
request += start
request += "&destinations="
request += end
request += "&sensor=false&units=imperial"
distance = 0
try:
print "Getting distance..."
response = url.urlopen(request)
distance = _processDistanceAPI(response)
# build a static Google Map
request = "http://maps.googleapis.com/maps/api/staticmap?size=480x480&markers=size:large%7Ccolor:red"
request += "%7C" + start + "%7C" + end + "%7C&sensor=false"
imgData = url.urlopen(request).read()
# save map file
f = open("./distance.png", "wb")
f.write("")
f.write(imgData)
f.close()
# bmp = Image.open("./distance.png")
except url.URLError:
print "Can't connect to Google Maps"
# ask for the distance to work in miles
while True:
try:
distance = float(raw_input(question))
break
except ValueError:
print "Please enter a number..."
if startStr == "Home":
return [start, end, distance]
# bmp.show()
return distance
def getGasPrice(locationR = "Columbia, SC"):
"""Gets the average gas price."""
try:
location = _googleStrip(locationR)
# Google Maps Geocode API - get lat/long and name of home address
request = "https://maps.googleapis.com/maps/api/geocode/json?address="
request += location
request += "&sensor=false&units=imperial"
response = url.urlopen(request)
homeList = _processGeocodeAPI(response)
# myGasFeed API - get prices of gas near home address
lat = str(homeList[1])
long = str(homeList[2])
radius = "5"
type = "reg" # [reg|mid|pre|diesel]
apiKey = "fk6pblixp6"
request = "http://api.mygasfeed.com/stations/radius/"
request += lat + "/" + long + "/" + radius + "/" + type + "/price/" + apiKey + ".json"
print "Getting gas prices..."
response = url.urlopen(request)
gasData = _processGasAPI(response)
price = gasData[0]
print "The average price of gas near {0} is ${1:.2f}".format(homeList[0], price)
# build a static Google Map
request = "http://maps.googleapis.com/maps/api/staticmap?size=480x480&markers=size:medium%7Ccolor:green"
for i, address in enumerate(gasData[2]):
request += "%7C" + _googleStrip(address)
if i == 4: break
request += "%7C&sensor=false"
imgData = url.urlopen(request).read()
# save map file
f = open("./stations.png", "wb")
f.write("")
f.write(imgData)
f.close()
# bmp = Image.open("./stations.png")
except url.URLError:
print "Can't connect to Google Maps and myGasFeed.com"
# ask for price of gas
while True:
try:
price = float(raw_input("How much does gas cost? "))
break
except ValueError:
print "Please enter a number..."
print ("Showing the 5 cheapest stations near you...")
# bmp.show()
return price
def getMPG():
"""Gets the fuel economy of your vehicle."""
try:
# ask for year
request = "http://www.fueleconomy.gov/ws/rest/vehicle/menu/year"
response = url.urlopen(request)
years = _processYearMenuAPI(response)
while True:
try:
year = int(raw_input("What is the year of your vehicle? "))
if year < years[0] or year > years[1]:
print "Year must be between {0} and {1}.".format(years[0], years[1])
continue
break
except ValueError:
print "Please enter a year..."
# get list of makes
print "Getting list of makes..."
request = "http://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year={0}".format(year)
response = url.urlopen(request)
makeList = _processMakeMenuAPI(response)
# let the user help find the make
userMake = raw_input("What is the make of your vehicle (Press 'Enter' to see full list)? ")
userMakeList = []
for makeOption in makeList:
if userMake == "":
userMakeList = makeList
break
if makeOption[0].upper() == userMake[0].upper():
userMakeList.append(makeOption)
if userMakeList == []: userMakeList = makeList
# list out make choices
for i, make in enumerate(userMakeList):
print "[" + str(i + 1) + "] " + make
# choose the make from the list
while True:
try:
choice = int(raw_input("Choose your make by number: "))
if choice < 1 or choice > len(userMakeList):
print "That's not a choice!"
continue
make = userMakeList[choice - 1]
makeURL = make.replace(" ", "%20")
break
except ValueError:
print "Please enter a number..."
request = "http://www.fueleconomy.gov/ws/rest/vehicle/menu/model?year="
request += str(year) + "&make=" + makeURL
# get list of models
print "Getting list of", make[0].upper() + make[1:], "models..."
response = url.urlopen(request)
modelList = _processModelMenuAPI(response)
# let the user help find the model
userModel = raw_input("What is the model of your vehicle (Press 'Enter' to see full list)? ")
userModelList = []
for modelOption in modelList:
if userModel == "":
userModelList = modelList
break
if modelOption[0].upper() == userModel[0].upper():
userModelList.append(modelOption)
if userModelList == []: userModelList = modelList
# list out model choices
for i, model in enumerate(userModelList):
print "[" + str(i + 1) + "] " + model
# choose the model from the list
while True:
try:
choice = int(raw_input("Choose your model by number: "))
if choice < 1 or choice > len(userModelList):
print "That's not a choice!"
continue
model = userModelList[choice - 1]
modelURL = model.replace(" ", "%20")
break
except ValueError:
print "Please enter a number..."
# get the list of model types
request = "http://www.fueleconomy.gov/ws/rest/vehicle/menu/options?year="
request += str(year) + "&make=" + make + "&model=" + modelURL
print "Getting list of", make[0].upper() + make[1:], model, "types..."
response = url.urlopen(request)
modelTypeList = _processModelTypeMenuAPI(response)
# list out model type choices
for i, modelTypePair in enumerate(modelTypeList):
print "[" + str(i + 1) + "] " + modelTypePair[0]
# choose the model type from the list
while True:
try:
choice = int(raw_input("Choose your model type by number: "))
if choice < 1 or choice > len(modelTypeList):
print "That's not a choice!"
continue
modelID = modelTypeList[choice - 1][1]
break
except ValueError:
print "Please enter a number..."
# get the EPA vehicle data
request = "http://www.fueleconomy.gov/ws/rest/vehicle/"
request += str(modelID)
print "Getting data for your", make, model + "..."
response = url.urlopen(request)
fuelEfficiency = _processFuelEconomyAPI(response)
print "Your", make, model, "gets an estimated", str(fuelEfficiency), "MPG"
except url.URLError:
print "Can't connect to fueleconomy.gov"
# ask for fuel efficiency
while True:
try:
fuelEfficiency = float(raw_input("What is the fuel efficiency of your vehicle? (MPG): "))
break
except ValueError:
print "Please enter a number..."
return fuelEfficiency
def getExpenses():
"""Calculates cost of fuel per month using commute to work."""
distanceList = getDistance("Home", "Work", "How many miles to work? ")
home = distanceList[0]
work = distanceList[1]
distanceToWork = distanceList[2]
# ask for other destinations
otherDistance = 0;
while True:
choice = raw_input("Are you going anywhere else this month? (y/n): ")
if choice in ("n", "N", "no"): break
elif choice not in ("y", "Y", "yes"):
print "Yes or no!"
continue
else:
otherDistance += getDistance()
# add up various driving distances
print "Adding round trip distance to work every day for four weeks..."
time.sleep(1)
if otherDistance: print "Adding other destination round trips..."
time.sleep(1)
distance = distanceToWork * 40 + otherDistance
print "You will travel", distance, "miles"
priceOfGas = getGasPrice(home)
fuelEfficiency = getMPG()
# calculate cost
print "Calculating total gallons of gas used..."
gallons = distance / fuelEfficiency
cost = priceOfGas * gallons
print "You will spend ${0:.2f} on fuel per month".format(cost)
if __name__ == "__main__":
getExpenses()