import requests import os # Set up the Looker API endpoint base_url = 'https://theorchard.looker.com:19999/api/4.0' # Prompt the user for API credentials client_id = input("Please enter your Looker API client_id: ").strip() client_secret = input("Please enter your Looker API client_secret: ").strip() def authenticate(client_id, client_secret): url = f'{base_url}/login' data = { 'client_id': client_id, 'client_secret': client_secret } response = requests.post(url, data=data) response.raise_for_status() return response.json().get('access_token') def get_all_users(access_token): url = f'{base_url}/users' headers = { 'Authorization': f'token {access_token}' } response = requests.get(url, headers=headers) response.raise_for_status() return response.json() def main(): try: access_token = authenticate(client_id, client_secret) users = get_all_users(access_token) # Manually filtering the users and creating a CSV string csv_str = "email,first_name,last_name\n" for user in users: if not user.get('verified_looker_employee', False) and not user.get('is_disabled', False): csv_str += f"{user.get('email', '')},{user.get('first_name', '')},{user.get('last_name', '')}\n" # Prompt user for path to save CSV path = input("Enter the path where you want to save the CSV file: ").strip() if not os.path.exists(path): os.makedirs(path) with open(f"{path}/filtered_users.csv", "w") as file: file.write(csv_str) print(f"CSV file has been saved successfully to {path}") except requests.HTTPError as e: print(f'HTTP Error occurred: {e}') except Exception as e: print(f'An error occurred: {e}') if __name__ == "__main__": main()