import sys import requests from dbfread import DBF from datetime import date, datetime import csv def convert_dates_to_strings(data): """ Recursively convert date and datetime objects in the data to strings. """ if isinstance(data, list): return [convert_dates_to_strings(item) for item in data] elif isinstance(data, dict): return {key: convert_dates_to_strings(value) for key, value in data.items()} elif isinstance(data, (date, datetime)): return data.isoformat() else: return data def read_dbf_file(file_path, columns): data = [] try: # Open the .dbf file with DBF(file_path) as dbf: # Iterate over each record in the .dbf file for record in dbf: # Filter the record to include only the specified columns filtered_record = {column: record.get(column) for column in columns} data.append(filtered_record) except Exception as e: print("Error:", e) return data def save_data_to_csv(data, file_path, columns): try: with open(file_path, 'w', newline='') as csv_file: writer = csv.DictWriter(csv_file, fieldnames=columns) writer.writeheader() writer.writerows(data) print(f"CSV saved to {file_path}") except Exception as e: print("Error saving CSV to file:", e) def send_data_to_api(csv_file_path): try: # API endpoint url = "https://127.0.0.1/api/v1/migrateUsers" # Send CSV file with data as multipart form-data files = {'file': open(csv_file_path, 'rb')} response = requests.post(url, files=files) # Check response status if response.status_code == 200: response_data = response.json() # Print the message print(f"Message from server: {response_data.get('message')}") print("CSV file sent successfully to the API") else: # Print the error message and response content print("Failed to send CSV file to the API. Status code:", response.status_code) print("Response content:", response.content.decode('utf-8')) except Exception as e: # Print the general error message print("Error:", e) if __name__ == "__main__": # Check if the file path is provided as a command-line argument if len(sys.argv) < 2: print("Usage: python script.py ") else: # Get the file path from the command-line arguments dbf_file_path = sys.argv[1] # Specify the columns to be extracted columns_to_extract = ["ACTP", "ACNO", "NAME2", "ADD1", "ADD2", "ADD3", "PLACE", "PIN", "PHONE"] # Call the function to read the .dbf file with specific columns dbf_data = read_dbf_file(dbf_file_path, columns_to_extract) # Save the data to a CSV file save_data_to_csv(dbf_data, 'users.csv', columns_to_extract) # Print the data as JSON array print("Sending data to the API...") # Call the function to send data to the API send_data_to_api('users.csv')