""" Services module """ import requests import json from .utils import get_jwt_token from pyvin import VIN from django.conf import settings from openai import OpenAI from .models import Car def normalize_name(name): return name.replace(' ', '').replace('-', '').lower() def decode_vin_pyvin(vin): vehicle = VIN(vin) data = { 'Make': vehicle.Make, 'Model': vehicle.Model, 'ModelYear': vehicle.ModelYear, } print(data) return data # vehicle-info # c2729afb # 6d397471920412d672af1b8a02ca52ea # option-info # 367974ed # 046b0412c1b4d3f8c39ec6375d6f3030 def elm(vin): headers = { "Content-Type": "application/json", 'app-id': 'c2729afb', 'app-key': '6d397471920412d672af1b8a02ca52ea', 'client-id': '94142c27-2536-47e9-8e28-9ca7728b9442', } url = 'https://vehicle-maintenance.api.elm.sa/api/v1/vehicles/vehicle-info?vin='+vin payload = {} response = requests.request("GET", url, headers=headers, data=payload) car_info = json.loads(response.text) return car_info def get_unique_colors(api_response): print(api_response) colors = api_response.get("data", []) print(colors) unique_colors = {} for color in colors: color_name = color.get("name") rgb = color.get("rgb") if color_name not in unique_colors: unique_colors[color_name] = rgb return [{"name": name, "rgb": rgb} for name, rgb in unique_colors.items()] def fetch_colors(car_data): car_colors = { "data": [ {"rgb": "192, 192, 192", "name": "Silver Metallic"}, {"rgb": "0, 0, 0", "name": "Jet Black"}, {"rgb": "255, 255, 255", "name": "Bright White"}, {"rgb": "128, 128, 128", "name": "Graphite Gray"}, {"rgb": "80, 80, 80", "name": "Gunmetal Gray"}, {"rgb": "255, 0, 0", "name": "Racing Red"}, {"rgb": "255, 69, 0", "name": "Inferno Orange"}, {"rgb": "0, 0, 255", "name": "Deep Blue Pearl"}, {"rgb": "75, 0, 130", "name": "Indigo Night"}, {"rgb": "255, 215, 0", "name": "Solar Gold"}, {"rgb": "34, 139, 34", "name": "Emerald Green"}, {"rgb": "60, 179, 113", "name": "Forest Mist Green"}, {"rgb": "255, 140, 0", "name": "Burnt Amber"}, {"rgb": "160, 82, 45", "name": "Copper Brown"}, {"rgb": "128, 0, 0", "name": "Crimson Maroon"}, {"rgb": "245, 245, 220", "name": "Beige Champagne"}, {"rgb": "169, 169, 169", "name": "Shadow Gray"}, {"rgb": "255, 250, 205", "name": "Lemon Pearl"}, {"rgb": "220, 220, 220", "name": "Platinum Silver"}, {"rgb": "105, 105, 105", "name": "Charcoal Metallic"}, {"rgb": "128, 0, 128", "name": "Royal Purple"}, {"rgb": "210, 105, 30", "name": "Sunset Bronze"}, {"rgb": "0, 128, 128", "name": "Teal Lagoon"}, {"rgb": "72, 61, 139", "name": "Midnight Blue"}, {"rgb": "255, 20, 147", "name": "Blazing Pink"}, {"rgb": "192, 57, 43", "name": "Crimson Flame"}, {"rgb": "255, 228, 196", "name": "Cream Sand"}, {"rgb": "112, 128, 144", "name": "Steel Gray"}, {"rgb": "0, 100, 0", "name": "Hunter Green"}, {"rgb": "255, 223, 0", "name": "Bright Yellow"}, {"rgb": "85, 107, 47", "name": "Olive Metallic"}, {"rgb": "128, 128, 0", "name": "Mustard Gold"}, {"rgb": "139, 69, 19", "name": "Cocoa Brown"}, {"rgb": "255, 165, 0", "name": "Tangerine Flame"}, {"rgb": "0, 0, 139", "name": "Navy Sapphire"}, {"rgb": "70, 130, 180", "name": "Skyline Blue"}, {"rgb": "220, 20, 60", "name": "Crimson Passion"}, {"rgb": "189, 183, 107", "name": "Khaki Dune"}, {"rgb": "50, 205, 50", "name": "Lime Essence"}, {"rgb": "139, 0, 139", "name": "Amethyst Glow"}, {"rgb": "255, 215, 180", "name": "Rosé Gold"}, {"rgb": "46, 139, 87", "name": "Moss Green"}, {"rgb": "72, 209, 204", "name": "Caribbean Aqua"}, {"rgb": "255, 240, 245", "name": "Pearl Blush"}, {"rgb": "244, 164, 96", "name": "Sierra Sunset"}, {"rgb": "139, 0, 0", "name": "Crimson Ruby"}, {"rgb": "192, 192, 192", "name": "Chrome"}, {"rgb": "255, 105, 180", "name": "Hot Magenta"}, {"rgb": "0, 255, 255", "name": "Ice Blue"}, {"rgb": "184, 134, 11", "name": "Golden Bronze"}, {"rgb": "128, 128, 64", "name": "Bronze Olive"}, {"rgb": "245, 222, 179", "name": "Wheat Cream"} ] } jwt_token = get_jwt_token() if not jwt_token: print("Failed to retrieve JWT token.") return None year = car_data['year'] make = car_data['make'] model = car_data['model'] url = "https://carapi.app/api/exterior-colors?year={}&make={}&model={}".format(year, make, model) params = { 'limit': '1000', 'sort': 'name', 'direction': 'asc', 'verbose': 'no', 'all_trims': 'no', } headers = { 'Accept': 'application/json', 'Authorization': f'Bearer {jwt_token}', } try: response = requests.get(url, headers=headers, params=params) color_info = response.json() if not color_info["data"] == []: return get_unique_colors(color_info) else: return car_colors["data"] except requests.exceptions.RequestException as e: print(f"Error fetching color information: {e}") return None def fetch_interior_colors(car_data): car_colors = { "data": [ {"rgb": "0, 0, 0", "name": "Jet Black"}, {"rgb": "54, 69, 79", "name": "Charcoal Black"}, {"rgb": "255, 255, 255", "name": "Bright White"}, {"rgb": "245, 245, 220", "name": "Off-White"}, {"rgb": "210, 180, 140", "name": "Beige"}, {"rgb": "205, 133, 63", "name": "Tan"}, {"rgb": "128, 128, 128", "name": "Gray"}, {"rgb": "80, 80, 80", "name": "Graphite Gray"}, {"rgb": "112, 128, 144", "name": "Gunmetal Gray"}, {"rgb": "192, 192, 192", "name": "Silver Metallic"}, {"rgb": "139, 69, 19", "name": "Cognac Brown"}, {"rgb": "149, 94, 55", "name": "Chestnut Brown"}, {"rgb": "97, 63, 43", "name": "Espresso Brown"}, {"rgb": "72, 40, 34", "name": "Dark Chocolate"}, {"rgb": "139, 69, 19", "name": "Saddle Brown"}, {"rgb": "124, 79, 58", "name": "Mocha"}, {"rgb": "193, 154, 107", "name": "Camel Tan"}, {"rgb": "128, 0, 32", "name": "Burgundy"}, {"rgb": "128, 0, 0", "name": "Maroon"}, {"rgb": "139, 0, 0", "name": "Deep Red"}, {"rgb": "0, 0, 128", "name": "Navy Blue"}, {"rgb": "65, 105, 225", "name": "Royal Blue"}, {"rgb": "34, 139, 34", "name": "Forest Green"}, {"rgb": "80, 200, 120", "name": "Emerald Green"}, {"rgb": "255, 255, 240", "name": "Ivory"}, {"rgb": "242, 242, 242", "name": "Pearl White"}, {"rgb": "169, 169, 169", "name": "Stone Gray"}, {"rgb": "112, 128, 144", "name": "Slate Gray"}, {"rgb": "150, 111, 51", "name": "Ash Brown"}, {"rgb": "128, 128, 0", "name": "Olive Green"}, {"rgb": "25, 25, 112", "name": "Midnight Blue"}, {"rgb": "72, 60, 50", "name": "Taupe"} ] } jwt_token = get_jwt_token() if not jwt_token: print("Failed to retrieve JWT token.") return None year = car_data['year'] make = car_data['make'] model = car_data['model'] url = "https://carapi.app/api/interior-colors?year={}&make={}&model={}".format(year, make, model) params = { 'limit': '100', 'sort': 'name', 'direction': 'asc', 'verbose': 'no', 'all_trims': 'no', } headers = { 'Accept': 'application/json', 'Authorization': f'Bearer {jwt_token}', } try: response = requests.get(url, headers=headers, params=params) color_info = response.json() if not color_info["data"] == []: return get_unique_colors(color_info) else: return car_colors["data"] except requests.exceptions.RequestException as e: print(f"Error fetching color information: {e}") return None def translate(content, *args, **kwargs): client = OpenAI(api_key=settings.OPENAI_API_KEY) completion = client.chat.completions.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a translation assistant that translates English to Arabic."}, {"role": "user", "content": content} ], temperature=0.3, ) translation = completion.choices[0].message.content.strip() return translation def calculate_stock_value(): cars = Car.objects.all() total_value = sum(car.selling_price for car in cars) return total_value