-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvin_decoder.py
More file actions
executable file
·47 lines (41 loc) · 1.51 KB
/
vin_decoder.py
File metadata and controls
executable file
·47 lines (41 loc) · 1.51 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
#!/usr/local/opt/python/bin/python2.7
"""Decode VINs from the NHTSA API as of 6/16/28."""
import urllib2
import json
class VinDecoder():
"""Super simple module to decode VINs using the NHTSA API."""
def __init__(self):
"""Initialize the decoder."""
return None
def decode(self, vin):
"""Decode the given VIN."""
url = 'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/\
%s?format=json' % vin
url = url.strip()
url = url.replace(" ", "")
res = urllib2.urlopen(url).read()
obj = json.loads(res)
if 'Results' in obj:
o = obj['Results']
for i in o:
if 'Variable' not in i:
return None
if i['Variable'] == 'Model Year':
year = i['Value']
if i['Variable'] == 'Make':
make = i['Value']
if i['Variable'] == 'Model':
model = i['Value']
if i['Variable'] == 'Displacement (L)':
disp = i['Value']
if i['Variable'] == 'Engine Number of Cylinders':
cyl = i['Value']
print "vin: %s %s %s %s, Disp: %s Cyl: %s" \
% (vin, year, make, model, disp, cyl)
return {'vin': vin, 'year': year, 'make': make,
'model': model, 'disp': disp, 'cyl': cyl}
if __name__ == "__main__":
vd = VinDecoder()
vin = '1GNGK56K19R227051'
d = vd.decode(vin)
print d