Spaces:
Sleeping
Sleeping
Upload google_api_deploy.py
Browse files- google_api_deploy.py +73 -0
google_api_deploy.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import pickle
|
| 4 |
+
import io
|
| 5 |
+
from googleapiclient.discovery import build
|
| 6 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 7 |
+
from google.auth.transport.requests import Request
|
| 8 |
+
from googleapiclient.http import MediaIoBaseUpload
|
| 9 |
+
|
| 10 |
+
# If modifying these SCOPES, delete the file token.pickle.
|
| 11 |
+
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 12 |
+
|
| 13 |
+
def authenticate_google_drive():
|
| 14 |
+
"""Authenticate and create the Google Drive service."""
|
| 15 |
+
creds = None
|
| 16 |
+
if os.path.exists('token.pickle'):
|
| 17 |
+
with open('token.pickle', 'rb') as token:
|
| 18 |
+
creds = pickle.load(token)
|
| 19 |
+
if not creds or not creds.valid:
|
| 20 |
+
if creds and creds.expired and creds.refresh_token:
|
| 21 |
+
creds.refresh(Request())
|
| 22 |
+
else:
|
| 23 |
+
flow = InstalledAppFlow.from_client_secrets_file(
|
| 24 |
+
'credentials.json', SCOPES)
|
| 25 |
+
creds = flow.run_local_server(port=0)
|
| 26 |
+
with open('token.pickle', 'wb') as token:
|
| 27 |
+
pickle.dump(creds, token)
|
| 28 |
+
service = build('drive', 'v3', credentials=creds)
|
| 29 |
+
return service
|
| 30 |
+
|
| 31 |
+
def read_google_doc(service, file_id):
|
| 32 |
+
"""Reads the content of a Google Doc as a string."""
|
| 33 |
+
try:
|
| 34 |
+
doc = service.files().export(fileId=file_id, mimeType='text/plain').execute()
|
| 35 |
+
return doc.decode('utf-8')
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"An error occurred: {e}")
|
| 38 |
+
return ""
|
| 39 |
+
|
| 40 |
+
def write_google_doc(service, file_id, content):
|
| 41 |
+
"""Writes content to a Google Doc."""
|
| 42 |
+
try:
|
| 43 |
+
media = MediaIoBaseUpload(io.BytesIO(content.encode()), mimetype='text/plain')
|
| 44 |
+
service.files().update(fileId=file_id, media_body=media).execute()
|
| 45 |
+
print("Content written successfully.")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"An error occurred: {e}")
|
| 48 |
+
|
| 49 |
+
def write_google_doc_add(service, file_id, new_content):
|
| 50 |
+
"""Appends new content to a Google Doc."""
|
| 51 |
+
try:
|
| 52 |
+
# Read the current content
|
| 53 |
+
current_content = read_google_doc(service, file_id)
|
| 54 |
+
# Append the new content
|
| 55 |
+
updated_content = current_content + "\n" + new_content
|
| 56 |
+
# Write the updated content back to the document
|
| 57 |
+
media = MediaIoBaseUpload(io.BytesIO(updated_content.encode()), mimetype='text/plain')
|
| 58 |
+
service.files().update(fileId=file_id, media_body=media).execute()
|
| 59 |
+
print("Content appended successfully.")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"An error occurred: {e}")
|
| 62 |
+
|
| 63 |
+
def monitor_google_doc(service, file_id):
|
| 64 |
+
"""Monitors the Google Doc for changes."""
|
| 65 |
+
last_content = read_google_doc(service, file_id)
|
| 66 |
+
while True:
|
| 67 |
+
current_content = read_google_doc(service, file_id)
|
| 68 |
+
if current_content != last_content:
|
| 69 |
+
print(f"Document has been modified:\n{current_content}")
|
| 70 |
+
last_content = current_content
|
| 71 |
+
time.sleep(10)
|
| 72 |
+
|
| 73 |
+
|