import requests import csv # Replace with your actual API token and secret key API_TOKEN = 'f5204a00-6f31-4de2-96d8-ed998e0d230c' SECRET_KEY = 'ae430502a5c66e818d9722919c8b5584' BASE_URL = 'https://carapi.app/api/v1' def fetch_and_save_car_makes_models(api_url, headers, output_csv): TRANSLATION = [] page = 1 pages_per_batch = 1 while True: responses = [] # Fetch 100 pages in one batch for i in range(pages_per_batch): current_page = page + i response = requests.get(f"{api_url}&page={current_page}", headers=headers) if response.status_code == 200 and page < 100: responses.append(response.json()) else: break # Process the batch of responses for data in responses: if 'data' not in data: continue for item in data['data']: make_name = item['make_model']['make']['name'] model_name = item['make_model']['name'] # Create dictionary for each make and model combination translation_entry = { 'make': f"{make_name} ", 'model': f"{model_name} " # Replace with actual Arabic translation if available } TRANSLATION.append(translation_entry) # Increment the page number for the next batch page += pages_per_batch # Check if there are more pages to fetch if not responses or ('next' in responses[-1]['collection'] and not responses[-1]['collection']['next']): break # Save the TRANSLATION data to a CSV file with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile: fieldnames = ['make', 'model'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for entry in TRANSLATION: writer.writerow(entry) print(f"Data saved to {output_csv}") # Example usage: api_url = "https://carapi.app/api/trims?sort=make_model_id&direction=asc&verbose=yes" headers = { 'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjYXJhcGkuYXBwIiwic3ViIjoiYjU1OGYzMDMtODI0Ni00NjgzLTkwYTQtZmYwMGQxYWNmNGU3IiwiYXVkIjoiYjU1OGYzMDMtODI0Ni00NjgzLTkwYTQtZmYwMGQxYWNmNGU3IiwiZXhwIjoxNzIzNzMxODMyLCJpYXQiOjE3MjMxMjcwMzIsImp0aSI6IjNkMGJhMzA4LWUzZTAtNGJhZC1iZmMxLTBiMDA3YzNmMmE2NSIsInVzZXIiOnsic3Vic2NyaWJlZCI6dHJ1ZSwic3Vic2NyaXB0aW9uIjoic3RhcnRlciIsInJhdGVfbGltaXRfdHlwZSI6ImhhcmQiLCJhZGRvbnMiOnsiYW50aXF1ZV92ZWhpY2xlcyI6ZmFsc2UsImRhdGFfZmVlZCI6ZmFsc2V9fX0.t__L53yN0OndnOP3_YxaAbrwgQXSYwVUgEqE1IwH8Nk', # Replace with actual token 'Content-Type': 'application/json' } output_csv = 'car_makes_models.csv' fetch_and_save_car_makes_models(api_url, headers, output_csv)