예제 #1
0
def ezLaunchDoc(selection):
    """Uses the Docs API.
        Makes a new document with a summery from POST request.
        """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists(os.path.join(THIS_FOLDER, 'token.pickle')):
        with open(os.path.join(THIS_FOLDER, 'token.pickle'), 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    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(
                os.path.join(THIS_FOLDER, 'credentials.txt'), SCOPES)
            creds = flow.run_local_server()
        # Save the credentials for the next run
        with open(os.path.join(THIS_FOLDER, 'token.pickle'), 'wb') as token:
            pickle.dump(creds, token)

    service = build('docs', 'v1', credentials=creds)

    # Retrieve the documents contents from the Docs service.
    # document = service.dtitle = 'My Document'
    summs = eZ.ezRank(selection)
    body = {
        'title' : "New Summary"
    }
    requests = [
        {
            'insertText':{
                'location':{
                    'index':1,

                },
                'text':"\n" + summs
            }
        }
    ]
    document = service.documents().create(body=body).execute()
    result = service.documents().batchUpdate(documentId=document.get("documentId"), body={'requests':requests}).execute()