Spaces:
Build error
Build error
Upload logs.py
Browse files
logs.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from google.oauth2 import service_account
|
| 3 |
+
from googleapiclient.discovery import build
|
| 4 |
+
from googleapiclient.http import MediaFileUpload
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
def save_logs(query,response, folder_id = "16Vv728HPW2J0BYzgTaBV00nUEc5pRKT-"):
|
| 8 |
+
to_save = f"LOG ENTRY\nQUERY\n{query}\n=================================\nRESPONSE\n{response}\n****************************************\n"
|
| 9 |
+
|
| 10 |
+
# Get the current date and time
|
| 11 |
+
now = datetime.now()
|
| 12 |
+
filename = str(now).replace(":","").replace(" ","").replace("-","").replace(".","")+".txt"
|
| 13 |
+
with open(filename, 'w') as file:
|
| 14 |
+
file.write(to_save)
|
| 15 |
+
# Path to the service account key file
|
| 16 |
+
SERVICE_ACCOUNT_FILE = 'secret_google_service_account.json'
|
| 17 |
+
|
| 18 |
+
# Define the required scopes
|
| 19 |
+
SCOPES = ['https://www.googleapis.com/auth/drive.file']
|
| 20 |
+
|
| 21 |
+
# Authenticate using the service account key file
|
| 22 |
+
credentials = service_account.Credentials.from_service_account_file(
|
| 23 |
+
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
| 24 |
+
|
| 25 |
+
# Build the Google Drive API client
|
| 26 |
+
service = build('drive', 'v3', credentials=credentials)
|
| 27 |
+
|
| 28 |
+
# Specify the folder ID where you want to upload the file
|
| 29 |
+
|
| 30 |
+
# Metadata of the file to be uploaded
|
| 31 |
+
file_metadata = {
|
| 32 |
+
'name': filename, # Name of the file to be uploaded
|
| 33 |
+
'parents': [folder_id] # Folder ID
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Path to the file you want to upload
|
| 37 |
+
file_path = filename
|
| 38 |
+
|
| 39 |
+
# Create a MediaFileUpload object to upload the file
|
| 40 |
+
media = MediaFileUpload(file_path, mimetype='text/plain')
|
| 41 |
+
|
| 42 |
+
# Use the Drive API to upload the file
|
| 43 |
+
file = service.files().create(
|
| 44 |
+
body=file_metadata,
|
| 45 |
+
media_body=media,
|
| 46 |
+
fields='id'
|
| 47 |
+
).execute()
|
| 48 |
+
|
| 49 |
+
# Print the file ID of the uploaded file
|
| 50 |
+
print('Saved in Google Drive - File ID: %s' % file.get('id'))
|