Advertisement
dark-s0ul

vehicleapi

Apr 28th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. import json
  2. import requests
  3. from time import sleep
  4.  
  5. url = "http://www.superchips.co.uk/api/vehicleapi.php/"
  6.  
  7. def get_makes():
  8.     while True:
  9.         try:
  10.             return requests.get(url + "makes?key=simontest", timeout=10)
  11.         except Exception as e:
  12.             print('makes: Try again after 10 seconds: ' + str(e))
  13.             sleep(10)
  14.  
  15.  
  16.  
  17. def get_fuels(_make):
  18.     while True:
  19.         try:
  20.             return requests.get(url + _make + "/fuel?key=simontest", timeout=10)
  21.         except Exception as e:
  22.             print("fuels: " + _make + ': Try again after 10 seconds: ' + str(e))
  23.             sleep(10)
  24.  
  25.  
  26. def get_models(_make, _fuel):
  27.     while True:
  28.         try:
  29.             return requests.get(url + _make + "/" + _fuel + "?key=simontest", timeout=10)
  30.         except Exception as e:
  31.             print('models: ' + _make + ": " + _fuel + ': Try again after 10 seconds: ' + str(e))
  32.             sleep(10)
  33.  
  34.  
  35. def get_variants(_fuel, _model):
  36.     global tries
  37.     while True:
  38.         try:
  39.             return requests.get(url + _fuel + "/" + str(_model) + "?key=simontest", timeout=10)
  40.         except Exception as e:
  41.             print('variants: ' + _fuel + ": " + str(_model) + ': Try again after 10 seconds: ' + str(e))
  42.             sleep(10)
  43.  
  44.  
  45. def get_details(_variant):
  46.     while True:
  47.         try:
  48.             return requests.get(url + "variant/" + str(_variant) + "?key=simontest", timeout=10)
  49.         except Exception as e:
  50.             print('details: ' + str(_variant) + ': Try again after 10 seconds: ' + str(e))
  51.             sleep(10)
  52.  
  53.  
  54. if __name__ == '__main__':
  55.     file = open("auto.csv", "w")
  56.  
  57.     makes = get_makes().json()["makes"]
  58.  
  59.     for make in makes:
  60.         fuels = get_fuels(make["name"]).json()["fuel_types"]
  61.  
  62.         for fuel in fuels:
  63.             models = get_models(make["name"], fuel["name"]).json()["models"]
  64.  
  65.             for model in models:
  66.                 variants = get_variants(fuel["name"], model["id"]).json()["variants"]
  67.                 for variant in variants:
  68.                     vehicle_info_json = get_details(variant["id"]).json()
  69.                     vehicle_info = None
  70.  
  71.                     if "vehicle_info" in vehicle_info_json:
  72.                         vehicle_info = vehicle_info_json["vehicle_info"]
  73.                     else:
  74.                         print("error: " + make["name"] + ";" + fuel["name"] + ";" + model["name"])
  75.                         print(json.dumps(vehicle_info_json, indent=4))
  76.                         continue
  77.  
  78.                     original_bhp = vehicle_info["original_bhp"]
  79.                     original_nm = vehicle_info["original_nm"]
  80.                     modified_bhp = vehicle_info["modified_bhp"]
  81.                     modified_nm = vehicle_info["modified_nm"]
  82.  
  83.                     total_bhp = ""
  84.                     try:
  85.                         total_bhp = str(int(original_bhp) + int(modified_bhp))
  86.                     except:
  87.                         total_bhp = original_bhp
  88.  
  89.                     total_nm = ""
  90.                     try:
  91.                         total_nm = str(int(original_nm) + int(modified_nm))
  92.                     except:
  93.                         total_nm = original_nm
  94.  
  95.                     powercurve_url = vehicle_info["powercurve_url"] if "powercurve_url" in vehicle_info else ""
  96.  
  97.                     value = make["name"] + ";" + fuel["name"] + ";" + model["name"] + ";" + variant["variant"] + ";" + str(vehicle_info["workshop_price"]) + ";" + original_bhp + ";" + original_nm + ";" + total_bhp + ";" + total_nm + ";" + powercurve_url
  98.                     file.write(str(value) + "\n")
  99.                     print(value)
  100.  
  101.     file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement