forked from h3/python-libvin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoding.py
More file actions
225 lines (199 loc) · 7.07 KB
/
Copy pathdecoding.py
File metadata and controls
225 lines (199 loc) · 7.07 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
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
"""
libvin - VIN Vehicle information number checker
(c) Copyright 2012 Maxime Haineault <[email protected]>
(c) Copyright 2016 Dan Kegel <[email protected]>
"""
from libvin.static import *
class Vin(object):
def __init__(self, vin):
self.vin = vin.upper()
@property
def country(self):
"""
Returns the World Manufacturer's Country.
"""
countries = WORLD_MANUFACTURER_MAP[self.vin[0]]['countries']
for codes in countries:
if self.vin[1] in codes:
return countries[codes]
return 'Unknown'
def decode(self):
return self.vin
@property
def is_pre_2010(self):
"""
Returns true if the model year is in the 1980-2009 range
In order to identify exact year in passenger cars and multipurpose
passenger vehicles with a GVWR of 10,000 or less, one must read
position 7 as well as position 10. For passenger cars, and for
multipurpose passenger vehicles and trucks with a gross vehicle
weight rating of 10,000 lb (4,500 kg) or less, if position 7 is
numeric, the model year in position 10 of the VIN refers to a year
in the range 1980-2009. If position 7 is alphabetic, the model year
in position 10 of VIN refers to a year in the range 2010-2039.
"""
return self.vin[6].isdigit()
@property
def is_valid(self):
"""
Returns True if a VIN is valid, otherwise returns False.
"""
if len(self.vin) != 17:
"""
For model years 1981 to present, the VIN is composed of 17
alphanumeric values
"""
return False
if any(x in 'IOQ' for x in self.vin):
"""
The letters I,O, Q are prohibited from any VIN position
"""
return False
if self.vin[9] in 'UZ0':
"""
The tenth position of the VIN represents the Model Year and
does not permit the use of the characters U and Z, as well
as the numeric zero (0)
"""
return False
products = [VIN_WEIGHT[i] * VIN_TRANSLATION[j] for i, j in enumerate(self.vin)]
check_digit = sum(products) % 11
if check_digit == 10:
check_digit = 'X'
if self.vin[8] != str(check_digit):
"""
The ninth position of the VIN is a calculated value based on
the other 16 alphanumeric values, it's called the
"Check Digit". The result of the check digit can ONLY be a
numeric 0-9 or letter "X".
"""
return False
return True
@property
def less_than_500_built_per_year(self):
"""
A manufacturer who builds fewer than 500 vehicles
per year uses a 9 as the third digit
"""
try:
return int(self.vin[2]) is 9
except ValueError:
return False
@property
def region(self):
"""
Returns the World Manufacturer's Region. Possible results:
"""
return WORLD_MANUFACTURER_MAP[self.vin[0]]['region']
@property
def vis(self):
"""
Returns the Vehicle Idendifier Sequence (ISO 3779)
Model Year, Manufacturer Plant and/or Serial Number
"""
return self.vin[-8:]
@property
def vds(self):
"""
Returns the Vehicle Descriptor Section (ISO 3779)
Assigned by Manufacturer; Check Digit is Calculated
"""
return self.vin[3:9]
@property
def vsn(self):
"""
Returns the Vehicle Sequential Number
"""
if self.less_than_500_built_per_year:
return self.vin[-3:]
else:
return self.vin[-6:]
@property
def wmi(self):
"""
Returns the World Manufacturer Identifier (any standards)
Assigned by SAE
"""
return self.vin[0:3]
@property
def manufacturer(self):
wmi = self.wmi
if wmi[:3] in WMI_MAP:
return WMI_MAP[wmi[:3]]
if wmi[:2] in WMI_MAP:
return WMI_MAP[wmi[:2]]
return 'Unknown'
@property
def make(self):
'''
This is like manufacturer, but without country or other suffixes, and should be short common name.
Should be same as values from e.g. http://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=2012
Should probably have a static table instead of doing late fixup like this.
'''
man = self.manufacturer
for suffix in [
'Argentina',
'Brazil',
'Canada',
'Cars',
'France',
'Hungary',
'Hungary',
'Mexico',
'Motor Company',
'Truck USA',
'Trucks & Buses',
'Trucks',
'Turkey',
'USA - trucks',
'USA (AutoAlliance International)',
'USA',
]:
if man.endswith(suffix):
man = man.replace(" %s" % suffix, "")
if man.endswith("(%s)" % suffix):
man = man.replace(" (%s)" % suffix, "")
if man == "General Motors":
return "GMC"
if man == 'Chrysler':
# 2012 and later: first 3 positions became overloaded, some 'make' aka brand info moved further in; see
# https://en.wikibooks.org/wiki/Vehicle_Identification_Numbers_(VIN_codes)/Chrysler/VIN_Codes
# http://www.allpar.com/mopar/vin-decoder.html
if self.year > 2011:
brandcode = self.vin[4]
if brandcode == 'D':
return 'Dodge'
if brandcode == 'F':
return 'Fiat'
if brandcode == 'J':
return 'Jeep'
if man == "Fuji Heavy Industries (Subaru)":
return 'Subaru'
if man == 'Nissan':
# ftp://safercar.gov/MfrMail/ORG7377.pdf "MY12 Nissan VIN Coding System"
# https://vpic.nhtsa.dot.gov/mid/home/displayfile/29173 "MY16 Nissan VIN Coding System"
# say Ininiti if offset 4 is [JVY], Nissan otherwise.
# ftp://safercar.gov/MfrMail/ORG6337.pdf "MY11 Nissan VIN Coding System"
# says that plus Infiniti if offset 4 + 5 are S1. (Nissan Rogue is S5.)
# ftp://ftp.nhtsa.dot.gov/mfrmail/ORG7846.pdf "MY13 Nissan VIN Coding System"
# says that plus Infiniti if offset 4 + 5 are L0.
if self.vin[4] in "JVY" or self.vin[4:6] == 'S1' or self.vin[4:6] == 'L0':
return 'Infiniti'
if man == 'Renault Samsung':
# FIXME: they build other makes, too
return 'Nissan'
if man == 'Subaru-Isuzu Automotive':
return 'Subaru'
return man
@property
def year(self):
"""
Returns the model year of the vehicle
"""
if self.is_pre_2010:
return YEARS_CODES_PRE_2010[self.vin[9]]
else:
return YEARS_CODES_PRE_2040[self.vin[9]]
def decode(vin):
v = Vin(vin)
return v.decode()