Write a script that will leverage true randomness provided by random.org, to give me…
- User
Write a script that will leverage true randomness provided by random.org, to give me five terms from a file held on google drive
Assistant
To create a script that leverages true randomness from random.org to select five terms from a file stored on Google Drive, you can use Python with the following libraries:
requests: To fetch random numbers from random.org.google-auth,google-auth-oauthlib,google-auth-httplib2, andgoogle-api-python-client: To interact with Google Drive and download the file.
Below is a Python script that accomplishes this task:
import os import random import requests from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.http import MediaIoBaseDownload from googleapiclient.errors import HttpError from io import BytesIO # If modifying these SCOPES, delete the file token.json. SCOPES = ['https://www.googleapis.com/auth/drive.readonly'] # Function to authenticate and get Google Drive service def get_google_drive_service(): creds = None if os.path.exists('token.json'): creds = Credentials.from_authorized_user_file('token.json', SCOPES) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) with open('token.json', 'w') as token: token.write(creds.to_json()) return build('drive', 'v3', credentials=creds) # Function to download a file from Google Drive def download_file_from_drive(service, file_id): request = service.files().get_media(fileId=file_id) fh = BytesIO() downloader = MediaIoBaseDownload(fh, request) done = False while not done: status, done = downloader.next_chunk() fh.seek(0) return fh.read().decode('utf-8') # Function to fetch true random numbers from random.org def get_random_numbers(n, min_val, max_val): url = "https://api.random.org/json-rpc/4/invoke" payload = { "jsonrpc": "2.0", "method": "generateIntegers", "params": { "apiKey": "YOUR_RANDOM_ORG_API_KEY", # Replace with your API key "n": n, "min": min_val, "max": max_val, "replacement": False # Ensure unique numbers }, "id": 1 } response = requests.post(url, json=payload) data = response.json() if 'error' in data: raise Exception(f"Random.org API error: {data['error']}") return data['result']['random']['data'] # Main function def main(): # Google Drive file ID (replace with your file ID) file_id = "YOUR_GOOGLE_DRIVE_FILE_ID" # Authenticate and get Google Drive service service = get_google_drive_service() # Download the file content file_content = download_file_from_drive(service, file_id) # Split the file content into terms terms = file_content.splitlines() # Get the number of terms in the file num_terms = len(terms) # Fetch 5 unique random indices from random.org random_indices = get_random_numbers(5, 0, num_terms - 1) # Select the terms based on the random indices selected_terms = [terms[i] for i in random_indices] # Print the selected terms print("Selected terms:") for term in selected_terms: print(term) if __name__ == '__main__': main()Steps to Use the Script:
-
Google Drive Setup:
- Create a Google Cloud project and enable the Google Drive API.
- Download the OAuth 2.0 credentials (
credentials.json) and place it in the same directory as the script. - Share the file in Google Drive with the email associated with your Google Cloud project or use the file ID directly.
-
Random.org API Key:
- Sign up at random.org and obtain an API key. Replace
"YOUR_RANDOM_ORG_API_KEY"in the script with your actual API key.
- Sign up at random.org and obtain an API key. Replace
-
Google Drive File ID:
- Replace
"YOUR_GOOGLE_DRIVE_FILE_ID"with the actual file ID of the file stored in Google Drive.
- Replace
-
Install Required Libraries:
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib requests -
Run the Script:
python script_name.py
This script will authenticate with Google Drive, download the specified file, fetch five unique random indices from random.org, and print the corresponding terms from the file.