|
|
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" |
|
|
|
|
|
|
|
|
now = datetime.now() |
|
|
filename = str(now).replace(":","").replace(" ","").replace("-","").replace(".","")+".txt" |
|
|
with open(filename, 'w') as file: |
|
|
file.write(to_save) |
|
|
|
|
|
SERVICE_ACCOUNT_FILE = 'secret_google_service_account.json' |
|
|
|
|
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/drive.file'] |
|
|
|
|
|
|
|
|
credentials = service_account.Credentials.from_service_account_file( |
|
|
SERVICE_ACCOUNT_FILE, scopes=SCOPES) |
|
|
|
|
|
|
|
|
service = build('drive', 'v3', credentials=credentials) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file_metadata = { |
|
|
'name': filename, |
|
|
'parents': [folder_id] |
|
|
} |
|
|
|
|
|
|
|
|
file_path = filename |
|
|
|
|
|
|
|
|
media = MediaFileUpload(file_path, mimetype='text/plain') |
|
|
|
|
|
|
|
|
file = service.files().create( |
|
|
body=file_metadata, |
|
|
media_body=media, |
|
|
fields='id' |
|
|
).execute() |
|
|
|
|
|
|
|
|
print('Saved in Google Drive - File ID: %s' % file.get('id')) |