Reputation: 13
I'm working on a program in Python that takes a vehicle registration and returns its MOT history by using MOT Trade API. So far, I've got the program working for one vehicle. I would like the program to look-up multiple vehicles by importing an excel file that contains a list of vehicle registrations. Here is my code:
import requests
from datetime import datetime
import smtplib
import time
import pandas as pd
df = pd.read_excel(r'vehicles.xlsx') # Reading the excel file with the vehicle database
reg = df['registration']
headers = {
'Accept': 'application/json',
'x-api-key': apiKey,
}
params = (
('registration', reg),
)
response = requests.get('https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests',
headers=headers, params=params)
def check_days_left():
r = response.json() # This returns a list of dicts of the full MOT History data
carDetails = r[0] # Access the first dictionary of the list which is MOT tests
motTests = carDetails['motTests'] # This returns a list of dicts of the MOT tests
latestTest = motTests[0] # Access the first dictionary to get the details of the latest test
expDate = latestTest['expiryDate'] # This returns the expiry date key of the latest test dictionary
uk_date = datetime.strptime(expDate, '%Y.%m.%d') # Convert expDate to datetime format
difference = uk_date - datetime.now() # Gets the timedelta between now and expiry date
days_left = difference.days # Extract the number of days from the above as an int
print(days_left)
check_days_left()
At the moment, this returns the MOT details for the last vehicle in the excel file, ignoring the rest. Any help is much appreciated. Thanks!
Upvotes: 0
Views: 130
Reputation: 186
You can loop through all the registration number and request to the api to get result of each registration number.
A working solution might look like following code -
import requests
from datetime import datetime
import smtplib
import time
import pandas as pd
df = pd.read_excel(r'vehicles.xlsx') # Reading the excel file with the vehicle database
registrations = df['registration'].tolist()
r = []
# loop through all the registration number in registrations array.
for reg in registrations:
headers = {
'Accept': 'application/json',
'x-api-key': apiKey,
}
params = {
'registration': reg,
}
# Add the response from the api in the array `r`
r.append(requests.get('https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests', headers=headers, params=params).json())
def check_days_left():
r = response.json() # This returns a list of dicts of the full MOT History data
carDetails = r[0] # Access the first dictionary of the list which is MOT tests
motTests = carDetails['motTests'] # This returns a list of dicts of the MOT tests
latestTest = motTests[0] # Access the first dictionary to get the details of the latest test
expDate = latestTest['expiryDate'] # This returns the expiry date key of the latest test dictionary
uk_date = datetime.strptime(expDate, '%Y.%m.%d') # Convert expDate to datetime format
difference = uk_date - datetime.now() # Gets the timedelta between now and expiry date
days_left = difference.days # Extract the number of days from the above as an int
print(days_left)
check_days_left()
Upvotes: 1