File size: 2,153 Bytes
3cd8a78
 
 
3ac7c08
3cd8a78
42dc391
 
3cd8a78
 
 
 
 
 
 
 
 
3ac7c08
3cd8a78
 
 
 
 
 
 
 
 
 
 
 
 
 
3ac7c08
3cd8a78
 
 
3ac7c08
3cd8a78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ac7c08
3cd8a78
 
 
 
3ac7c08
3cd8a78
 
 
 
 
 
 
 
 
 
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 datetime
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import logging

logging.basicConfig(level=logging.INFO)


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()
        logging.info("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()
        )

        logging.info(f"A new Google Doc file has been created with ID: {doc_id}")
        return result

    except HttpError as err:
        logging.info(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()
        )

        logging.info(
            f'The document with ID {file.get("id")} was moved to the folder with ID {folder_id}.'
        )

    except HttpError as error:
        logging.info(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"