File size: 2,764 Bytes
f5fcf59
 
 
 
01d63c5
f5fcf59
 
 
 
 
f091c04
f5fcf59
 
 
 
 
 
 
 
 
 
 
 
 
 
ffb160e
51faeb5
f5fcf59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20f291a
 
f5fcf59
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import time
import pickle
import io
import json
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseUpload


# If modifying these SCOPES, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive']

def authenticate_google_drive():
    """Authenticate and create the Google Drive service."""
    creds = None
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    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_console()
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)
    service = build('drive', 'v3', credentials=creds)
    return service

def read_google_doc(service, file_id):
    """Reads the content of a Google Doc as a string."""
    try:
        doc = service.files().export(fileId=file_id, mimeType='text/plain').execute()
        return doc.decode('utf-8')
    except Exception as e:
        print(f"An error occurred: {e}")
        return ""

def write_google_doc(service, file_id, content):
    """Writes content to a Google Doc."""
    try:
        media = MediaIoBaseUpload(io.BytesIO(content.encode()), mimetype='text/plain')
        service.files().update(fileId=file_id, media_body=media).execute()
        print("Content written successfully.")
    except Exception as e:
        print(f"An error occurred: {e}")

def write_google_doc_add(service, file_id, new_content):
    """Appends new content to a Google Doc."""
    try:
        # Read the current content
        current_content = read_google_doc(service, file_id)
        # Append the new content
        updated_content = current_content + "\n" + new_content
        # Write the updated content back to the document
        media = MediaIoBaseUpload(io.BytesIO(updated_content.encode()), mimetype='text/plain')
        service.files().update(fileId=file_id, media_body=media).execute()
        print("Content appended successfully.")
    except Exception as e:
        print(f"An error occurred: {e}")

def monitor_google_doc(service, file_id):
    """Monitors the Google Doc for changes."""
    last_content = read_google_doc(service, file_id)
    while True:
        current_content = read_google_doc(service, file_id)
        if current_content != last_content:
            return current_content
        last_content = current_content
        time.sleep(10)