zinoubm's picture
adding google docs functionality
3cd8a78
raw
history blame
2.06 kB
import datetime
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def save_doc(creds, title, content):
try:
service = build("docs", "v1", credentials=creds)
# create a document
title = title
body = {"title": title}
doc = service.documents().create(body=body).execute()
print("Created document with title: {0}".format(doc.get("title")))
# Get the ID of the new file
doc_id = doc["documentId"]
# Write "Hello World" in the new file
doc_content = content
service.documents().get(documentId=doc_id).execute()
requests = [{"insertText": {"location": {"index": 1}, "text": doc_content}}]
result = (
service.documents()
.batchUpdate(documentId=doc_id, body={"requests": requests})
.execute()
)
print(f"A new Google Doc file has been created with ID: {doc_id}")
return result
except HttpError as err:
print(err)
def move_doc(creds, document_id, folder_id):
service = build("drive", "v3", credentials=creds)
try:
# Get the current parents of the document
file = service.files().get(fileId=document_id, fields="parents").execute()
current_parents = ",".join(file.get("parents"))
# Move the document to the new folder
file = (
service.files()
.update(
fileId=document_id,
addParents=folder_id,
removeParents=current_parents,
fields="id, parents",
)
.execute()
)
print(
f'The document with ID {file.get("id")} was moved to the folder with ID {folder_id}.'
)
except HttpError as error:
print(f"An error occurred: {error}")
def name_doc():
"""
Gets and format the time to generate document name
"""
now = datetime.datetime.now()
timestamp = now.strftime("%Y-%m-%d_%H-%M-%S")
return f"summary_{timestamp}.txt"