|
|
""" |
|
|
cli_demo.py |
|
|
|
|
|
Author: Eugenia Tate |
|
|
Date: August 24, 2025 |
|
|
|
|
|
A simple command-line application for a car dealership that allows users |
|
|
to look up vehicle information (year, make, and model) by VIN number. |
|
|
The app also lets a user view top 3 looked up vehicle makes. |
|
|
The data is retrieved from the NHTSA (National Highway Traffic Safety |
|
|
Administration) VIN decoding API via the helper module `nhtsa_api_call.py`. |
|
|
|
|
|
Usage: |
|
|
python cli_demo.py |
|
|
""" |
|
|
import nhtsa_api_call, re, csv, os |
|
|
import pandas as pd |
|
|
|
|
|
TOP3 = 3 |
|
|
|
|
|
def is_valid_vin (vin): |
|
|
""" |
|
|
Check whether vin is valid. |
|
|
A valid VIN is a VIN of 17 alphanumeric characters long and can not |
|
|
include I, O, Q. |
|
|
""" |
|
|
vin = vin.upper() |
|
|
if len(vin) != 17: |
|
|
return False |
|
|
if not re.match(r'^[A-HJ-NPR-Z0-9]{17}$', vin): |
|
|
return False |
|
|
return True |
|
|
|
|
|
def save_vehicle(year, make, model, filename = "vin_lookup_history.csv"): |
|
|
"""Append VIN lookup result to a CSV file.""" |
|
|
file_exists = os.path.isfile(filename) |
|
|
with open(filename, "a", newline="") as f: |
|
|
writer = csv.writer(f) |
|
|
if not file_exists: |
|
|
|
|
|
writer.writerow(["year", "make", "model"]) |
|
|
writer.writerow([year, make, model]) |
|
|
|
|
|
def display_top3(filename = "vin_lookup_history.csv"): |
|
|
"""Show top 3 car makes by lookup frequency""" |
|
|
|
|
|
if not os.path.isfile(filename): |
|
|
print("\nNo VIN lookup history found. Please look up some VINs first.\n") |
|
|
return |
|
|
|
|
|
df = pd.read_csv(filename) |
|
|
|
|
|
counts = df['make'].value_counts() |
|
|
|
|
|
if len(counts) == 0: |
|
|
print("\n No data in the log yet.\n") |
|
|
return |
|
|
|
|
|
|
|
|
if len(counts) < TOP3: |
|
|
print(f"\nNot enough data for displaying top 3. Showing top {len(counts)} instead:\n") |
|
|
top_makes = counts.head(len(counts)) |
|
|
else: |
|
|
print(f"\n Top {TOP3} Car Makes from Lookups:\n") |
|
|
top_makes = counts.head(TOP3) |
|
|
|
|
|
for make, count in top_makes.items(): |
|
|
print(f" {make}: {count} lookups") |
|
|
|
|
|
print() |
|
|
|
|
|
def main(): |
|
|
""" |
|
|
Run the command-line VIN lookup loop. |
|
|
|
|
|
Prompts the user for a VIN number, calls the NHTSA API, |
|
|
and prints out the vehicle information if found. |
|
|
Enter '0' to exit. |
|
|
""" |
|
|
print("π Welcome to the Car Dealership VIN Lookup!\n") |
|
|
|
|
|
while True: |
|
|
print("Choose an option from the menu:") |
|
|
print("1. Lookup a VIN") |
|
|
print("2. Show Top 3 Most Looked-up Makes") |
|
|
print("0. Exit") |
|
|
choice = input("Enter your choice: ").strip() |
|
|
|
|
|
|
|
|
if choice == "0": |
|
|
print("π You chose to exit VIN lookup. Goodbye!") |
|
|
break |
|
|
|
|
|
elif choice == "1": |
|
|
vin = input("Please enter a valid VIN or 0 (zero) to exit: \n").strip() |
|
|
if vin == "0": |
|
|
continue |
|
|
|
|
|
if not is_valid_vin(vin): |
|
|
print("Invalid VIN format. Must be 17 letters/numbers (no I, O, Q). Try again.\n") |
|
|
continue |
|
|
|
|
|
else: |
|
|
year, make, model = nhtsa_api_call.get_vehicle_info(vin) |
|
|
if year and make and model: |
|
|
print(f"β
VIN {vin}") |
|
|
print(f" Year: {year}") |
|
|
print(f" Make: {make}") |
|
|
print(f" Model: {model}") |
|
|
save_vehicle(year, make, model) |
|
|
else: |
|
|
print(f"β VIN {vin} not found in database. Try again.") |
|
|
|
|
|
elif choice == "2": |
|
|
display_top3() |
|
|
|
|
|
else: |
|
|
print("Invalid option. Please choose 1, 2, or 0.\n") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|