File size: 1,772 Bytes
3289271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from datetime import datetime

def save_logs(query,response, folder_id = ""):
    to_save = f"LOG ENTRY\nQUERY\n{query}\n=================================\nRESPONSE\n{response}\n****************************************\n"

    # Get the current date and time
    now = datetime.now()
    filename = str(now).replace(":","").replace(" ","").replace("-","").replace(".","")+".txt"
    with open(filename, 'w') as file:
        file.write(to_save)
    # Path to the service account key file
    SERVICE_ACCOUNT_FILE = 'secret_google_service_account.json'

    # Define the required scopes
    SCOPES = ['https://www.googleapis.com/auth/drive.file']

    # Authenticate using the service account key file
    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

    # Build the Google Drive API client
    service = build('drive', 'v3', credentials=credentials)

    # Specify the folder ID where you want to upload the file

    # Metadata of the file to be uploaded
    file_metadata = {
        'name': filename,  # Name of the file to be uploaded
        'parents': [folder_id]  # Folder ID
    }

    # Path to the file you want to upload
    file_path = filename

    # Create a MediaFileUpload object to upload the file
    media = MediaFileUpload(file_path, mimetype='text/plain')

    # Use the Drive API to upload the file
    file = service.files().create(
        body=file_metadata,
        media_body=media,
        fields='id'
    ).execute()

    # Print the file ID of the uploaded file
    print('Saved in Google Drive - File ID: %s' % file.get('id'))