zinoubm/ClientTest
0
1import datetime2from googleapiclient.discovery import build3from googleapiclient.errors import HttpError4import logging5 6logging.basicConfig(level=logging.INFO)7 8 9def save_doc(creds, title, content):10 try:11 service = build("docs", "v1", credentials=creds)12 13 # create a document14 title = title15 body = {"title": title}16 doc = service.documents().create(body=body).execute()17 logging.info("Created document with title: {0}".format(doc.get("title")))18 19 # Get the ID of the new file20 doc_id = doc["documentId"]21 22 # Write "Hello World" in the new file23 doc_content = content24 service.documents().get(documentId=doc_id).execute()25 requests = [{"insertText": {"location": {"index": 1}, "text": doc_content}}]26 result = (27 service.documents()28 .batchUpdate(documentId=doc_id, body={"requests": requests})29 .execute()30 )31 32 logging.info(f"A new Google Doc file has been created with ID: {doc_id}")33 return result34 35 except HttpError as err:36 logging.info(err)37 38 39def move_doc(creds, document_id, folder_id):40 service = build("drive", "v3", credentials=creds)41 42 try:43 # Get the current parents of the document44 file = service.files().get(fileId=document_id, fields="parents").execute()45 current_parents = ",".join(file.get("parents"))46 47 # Move the document to the new folder48 file = (49 service.files()50 .update(51 fileId=document_id,52 addParents=folder_id,53 removeParents=current_parents,54 fields="id, parents",55 )56 .execute()57 )58 59 logging.info(60 f'The document with ID {file.get("id")} was moved to the folder with ID {folder_id}.'61 )62 63 except HttpError as error:64 logging.info(f"An error occurred: {error}")65 66 67def name_doc():68 """69 Gets and format the time to generate document name70 """71 now = datetime.datetime.now()72 timestamp = now.strftime("%Y-%m-%d_%H-%M-%S")73 74 return f"summary_{timestamp}.txt"75 