Created
June 27, 2023 10:27
-
-
Save Urkman/242965acd1e1504f6083a72d5fc0286f to your computer and use it in GitHub Desktop.
Tesla Assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import TeslaSwift | |
class TeslaAssets { | |
static let shared: TeslaAssets = TeslaAssets() | |
var carType: String = "" | |
var optionCodes: [String] = [] | |
func generateImageUrl(for vehicleConfig: VehicleConfig) -> String { | |
let baseUrl = "https://static-assets.tesla.com" | |
resetData() | |
setOptionCodes(for: vehicleConfig) | |
setCarType(for: vehicleConfig) | |
let optinCodeString = optionCodes.filter{ !$0.isEmpty }.joined(separator: ",") | |
var url: String = "" | |
var crop = "" | |
switch carType { | |
case "my": | |
crop = "crop=1450,500,250,300" | |
case "m3": | |
crop = "crop=1450,500,250,300" | |
case "ms": | |
crop = "crop=1450,500,250,300" | |
default: | |
crop = "crop=1300,450,300,300" | |
} | |
// There are two types of Tesla assets urls.... | |
// the newer one: | |
// url = "\(baseUrl)/configurator/compositor?context=design_studio_2&options=\(optinCodeString)&view=FRONT34&model=\(carType)&size=400&bkba_opt=1&crop=\(crop)" | |
// the older one: | |
url = "\(baseUrl)/v1/compositor/?model=\(carType)&view=STUD_3QTR&size=400&bkba_opt=1&options=\(optinCodeString)&\(crop)" | |
return url | |
} | |
func getMarketingName(for vehicleConfig: VehicleConfig) -> String { | |
// {"S", "100D", "lychee"} -> "LR" | |
// {"S", "P100D", "lychee"} -> "Plaid" | |
// {"3", "P74D", _} -> "LR AWD Performance" | |
// {"3", "74D", _} -> "LR AWD" | |
// {"3", "74", _} -> "LR" | |
// {"3", "62", _} -> "MR" | |
// {"3", "50", _} -> "SR+" | |
// {"X", "100D", "tamarind"} -> "LR" | |
// {"X", "P100D", "tamarind"} -> "Plaid" | |
// {"Y", "P74D", _} -> "LR AWD Performance" | |
// {"Y", "74D", _} -> "LR AWD" | |
return "" | |
} | |
private func resetData() { | |
optionCodes = [] | |
carType = "m3" | |
} | |
private func setOptionCodes(for vehicleConfig: VehicleConfig) { | |
optionCodes.append(colorNameToCode(vehicleConfig.exteriorColor)) | |
optionCodes.append(wheelTypeToCode(vehicleConfig)) | |
if vehicleConfig.spoilerType != "None" { | |
optionCodes.append("SLR1") | |
} | |
} | |
private func setCarType(for vehicleConfig: VehicleConfig) { | |
let appSettings = AppSettings.shared | |
if vehicleConfig.carType?.prefix(6) == "model3" { carType = "m3" } | |
if vehicleConfig.carType?.prefix(6) == "modely" { carType = "my" } | |
if vehicleConfig.carType?.prefix(6) == "modelx" { carType = "mx" } | |
if vehicleConfig.carType?.prefix(6) == "models" { carType = "ms" } | |
if vehicleConfig.carType?.prefix(6) == "lychee" { carType = "ms" } | |
if vehicleConfig.carType?.prefix(8) == "tamarind" { carType = "mx" } | |
if vehicleConfig.carType == "models2" { | |
optionCodes.append("MI01") | |
} | |
if !appSettings.useAssetsBackup { | |
if carType == "m3" { | |
if vehicleConfig.trimBadging?.lowercased() == "p74d" { | |
optionCodes.append("MT325") | |
optionCodes.append("IPB1") | |
} else if vehicleConfig.trimBadging?.lowercased() == "74d" { | |
optionCodes.append("IPB1") | |
if optionCodes.contains("W38B") { | |
optionCodes.append("MT310") | |
} else { | |
optionCodes.append("MT316") | |
} | |
} else if vehicleConfig.trimBadging?.lowercased() == "50" { | |
optionCodes.append("MT322") | |
optionCodes.append("IBB1") | |
} | |
} else if carType == "my" { | |
if vehicleConfig.trimBadging?.lowercased() == "74d" { | |
optionCodes.append("MTY09") | |
optionCodes.append("INPB0") | |
} else if vehicleConfig.trimBadging?.lowercased() == "p74d" { | |
optionCodes.append("MTY12") | |
optionCodes.append("INPB0") | |
} | |
} else if carType == "mx" { | |
if vehicleConfig.trimBadging?.lowercased() == "100d" { | |
optionCodes.append("MTX07") | |
optionCodes.append("IBE00") | |
} else { | |
optionCodes.append("MTX13") | |
optionCodes.append("IBE00") | |
} | |
} else if carType == "ms" { | |
if vehicleConfig.trimBadging?.lowercased() == "p100d" { | |
optionCodes.append("MTS14") | |
optionCodes.append("IBC00") | |
} else if vehicleConfig.trimBadging?.lowercased() == "100d" { | |
} | |
} | |
} | |
} | |
private func wheelTypeToCode(_ vehicleConfig: VehicleConfig) -> String { | |
guard let wheelType = vehicleConfig.wheelType | |
else { return "" } | |
var wheel: String = "" | |
if carType == "m3" && vehicleConfig.trimBadging?.lowercased() == "p74d" { | |
return "W33D" | |
} else if carType == "my" && vehicleConfig.trimBadging?.lowercased() == "p74d" { | |
return "WY21P" | |
} | |
switch wheelType { | |
case "Slipstream19Silver": | |
wheel = "WTAS" | |
case "Slipstream19Carbon": | |
wheel = "WTDS" | |
case "Slipstream20Carbon": | |
wheel = "WTSC" | |
case "Slipstream20Silver": | |
wheel = "WT20" | |
case "Pinwheel18": | |
wheel = "W38B" | |
case "Pinwheel18CapKit", "PinwheelRefresh18": | |
wheel = "W40B" | |
case "AeroTurbine20": | |
wheel = "WT20" | |
case "Aero19": | |
wheel = "WTAE" | |
case "Sportwheel19", "Stiletto19": | |
wheel = "W39B" | |
case "AeroTurbine19": | |
wheel = "WTAS" | |
case "Turbine19": | |
wheel = "WTTB" | |
case "Arachnid21Grey": | |
wheel = "WTAB" | |
case "Performancewheel20", "Stiletto20DarkSquare", "Stiletto20": | |
wheel = "W32P" | |
case "AeroTurbine22": | |
wheel = "WT22" | |
case "Super21Gray": | |
wheel = "WTSG" | |
case "Induction20Black": | |
wheel = "WY20P" | |
case "Gemini19", "Apollo19": | |
wheel = "WY19B" | |
case "Aero18", "StilettoRefresh19": | |
wheel = "W41B" | |
case "Sport19": | |
wheel = "W40B" | |
case "Turbine22Dark": | |
wheel = "WTUT" | |
case "Turbine22Light": | |
wheel = "WT22" | |
case "UberTurbine20Gunpowder": | |
wheel = "W33D" | |
case "UberTurbine21Black": | |
wheel = "WY21P" | |
case "Cardenio19": | |
wheel = "WS90" | |
default: | |
wheel = "" | |
} | |
if wheel.isEmpty { | |
if wheelType.contains("Slipstream19") { | |
wheel = "WTAS" | |
} else if wheelType.contains("Turbine22") { | |
wheel = "WTUT" | |
} else if wheelType.contains("UberTurbine20") { | |
wheel = "W33D" | |
} else if wheelType.contains("Base19") { | |
wheel = "WT19" | |
} else if wheelType.contains("Stiletto20") { | |
wheel = "W32P" | |
} else if wheelType.contains("Cyberstream") { | |
wheel = "WX00" | |
} | |
} | |
return wheel | |
} | |
private func colorNameToCode(_ colorName: String?) -> String { | |
var color: String | |
switch colorName { | |
case "ObsidianBlack", "MetallicBlack": | |
color = "PMBL" | |
case "SolidBlack": | |
color = "PBSB" | |
case "DeepBlueMetallic", "DeepBlue": | |
color = "PPSB" | |
case "RedMulticoat", "Red": | |
color = "PPMR" | |
case "MidnightSilverMetallic", "MidnightSilver", "SteelGrey", "SilverMetallic": | |
color = "PMNG" | |
case "MetallicBrown", "Brown": | |
color = "PMAB" | |
case "Silver": | |
color = "PMSS" | |
case "TitaniumCopper": | |
color = "PPTI" | |
case "DolphinGrey": | |
color = "PMTG" | |
case "Green", "MetallicGreen": | |
color = "PMSG" | |
case "PearlWhiteMulticoat", "PearlWhite", "Pearl", "White": | |
color = "PPSW" | |
case "SolidWhite": | |
color = "PBCW" | |
case "SignatureBlue", "MetallicBlue": | |
color = "PMMB" | |
case "SignatureRed": | |
color = "PPSR" | |
case "Quicksilver": | |
color = "PN00" | |
case "MidnightCherryRed": | |
color = "PR00" | |
default: | |
color = "PBSB" | |
} | |
return color | |
} | |
} | |
extension TeslaAssets { | |
private var testVehicleConfig: VehicleConfig? { | |
let configString = """ | |
{"wheel_type":"Pinwheel18CapKit","trim_badging":"74d","rear_seat_type":0,"can_accept_navigation_requests":true,"roof_color":"RoofColorGlass","rhd":false,"spoiler_type":"None","car_special_type":"base","has_ludicrous_mode":false,"timestamp":1673549643635,"plg":true,"motorized_charge_port":true,"eu_vehicle":true,"rear_seat_heaters":1,"third_row_seats":"None","has_air_suspension":false,"can_actuate_trunks":true,"exterior_color":"PearlWhite","charge_port_type":"CCS","car_type":"model3"} | |
""" | |
let value = try? JSONDecoder().decode(VehicleConfig.self, from: configString.data(using: .utf8) ?? Data()) | |
return value | |
} | |
func printImageUrlFromTestVehicle() { | |
guard let testVehicleConfig else { return } | |
print (generateImageUrl(for: testVehicleConfig)) | |
} | |
} | |
// MARK: - Example images | |
// | |
// https://static-assets.tesla.com/v1/compositor/?model=m3&view=STUD_3QTR&size=1440&options=$APBS,$DV2W,$IBB1,$PMNG,$PRM30,$SC04,$MDL3,$W41B,$MT322,$CPF1,$RSF1,$CW03&bkba_opt=1&crop=1450,600,250,300 | |
// https://static-assets.tesla.com/v1/compositor/?model=m3&view=STUD_3QTR&size=1440&options=$APPB,$APBS,$DV2W,$IN3B2,$PBSB,$PRM30,$SC04,$MDL3,$W38B,$MT336,$CPF1,$RSF1&bkba_opt=1&crop=1450,600,250,300 | |
// https://static-assets.tesla.com/v1/compositor/?model=m3&view=STUD_3QTR&size=1440&options=$IBB1,$PMNG,$W41B,$MT322&bkba_opt=1&crop=1450,600,250,300 | |
// https://static-assets.tesla.com/v1/compositor/?model=m3&view=STUD_3QTR&size=1440&options=$IN3B2,$PBSB,$W38B&bkba_opt=1&crop=1450,600,250,300 | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment