Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import requests
- from time import sleep
- url = "http://www.superchips.co.uk/api/vehicleapi.php/"
- def get_makes():
- while True:
- try:
- return requests.get(url + "makes?key=simontest", timeout=10)
- except Exception as e:
- print('makes: Try again after 10 seconds: ' + str(e))
- sleep(10)
- def get_fuels(_make):
- while True:
- try:
- return requests.get(url + _make + "/fuel?key=simontest", timeout=10)
- except Exception as e:
- print("fuels: " + _make + ': Try again after 10 seconds: ' + str(e))
- sleep(10)
- def get_models(_make, _fuel):
- while True:
- try:
- return requests.get(url + _make + "/" + _fuel + "?key=simontest", timeout=10)
- except Exception as e:
- print('models: ' + _make + ": " + _fuel + ': Try again after 10 seconds: ' + str(e))
- sleep(10)
- def get_variants(_fuel, _model):
- global tries
- while True:
- try:
- return requests.get(url + _fuel + "/" + str(_model) + "?key=simontest", timeout=10)
- except Exception as e:
- print('variants: ' + _fuel + ": " + str(_model) + ': Try again after 10 seconds: ' + str(e))
- sleep(10)
- def get_details(_variant):
- while True:
- try:
- return requests.get(url + "variant/" + str(_variant) + "?key=simontest", timeout=10)
- except Exception as e:
- print('details: ' + str(_variant) + ': Try again after 10 seconds: ' + str(e))
- sleep(10)
- if __name__ == '__main__':
- file = open("auto.csv", "w")
- makes = get_makes().json()["makes"]
- for make in makes:
- fuels = get_fuels(make["name"]).json()["fuel_types"]
- for fuel in fuels:
- models = get_models(make["name"], fuel["name"]).json()["models"]
- for model in models:
- variants = get_variants(fuel["name"], model["id"]).json()["variants"]
- for variant in variants:
- vehicle_info_json = get_details(variant["id"]).json()
- vehicle_info = None
- if "vehicle_info" in vehicle_info_json:
- vehicle_info = vehicle_info_json["vehicle_info"]
- else:
- print("error: " + make["name"] + ";" + fuel["name"] + ";" + model["name"])
- print(json.dumps(vehicle_info_json, indent=4))
- continue
- original_bhp = vehicle_info["original_bhp"]
- original_nm = vehicle_info["original_nm"]
- modified_bhp = vehicle_info["modified_bhp"]
- modified_nm = vehicle_info["modified_nm"]
- total_bhp = ""
- try:
- total_bhp = str(int(original_bhp) + int(modified_bhp))
- except:
- total_bhp = original_bhp
- total_nm = ""
- try:
- total_nm = str(int(original_nm) + int(modified_nm))
- except:
- total_nm = original_nm
- powercurve_url = vehicle_info["powercurve_url"] if "powercurve_url" in vehicle_info else ""
- value = make["name"] + ";" + fuel["name"] + ";" + model["name"] + ";" + variant["variant"] + ";" + str(vehicle_info["workshop_price"]) + ";" + original_bhp + ";" + original_nm + ";" + total_bhp + ";" + total_nm + ";" + powercurve_url
- file.write(str(value) + "\n")
- print(value)
- file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement