是否有任何特定方法可以过滤 Google Drive 更改?

时间:2020-12-23 09:59:35

标签: python google-api google-drive-api google-api-php-client

我开始使用 Google Drive Changes API (https://developers.google.com/drive/api/v3/reference/changes)。虽然我的代码可以跟踪“我的云端硬盘”中发生的所有更改,但我似乎无法仅过滤掉特定的更改,例如创建新文件。每当我将文件上传到文件夹时,都会发生 2 个更改,一个是关于文件更改的,另一个是关于文件夹更改的。 到目前为止,我的代码如下:

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import schedule
import time

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

def job():
    print("Inside job()")
    global saved_start_page_token
    page_token = saved_start_page_token

    while page_token is not None:
        response = service.changes().list(pageToken=page_token, spaces='drive', restrictToMyDrive=True).execute()
        for change in response.get('changes'):
            # Process change
            file_id = change.get('fileId')
            print('Change found for file: %s' % file_id)
            print('Change type: %s' % change.get('changeType'))
            print('Filename: %s' % change.get('file').get('name'))
        if 'newStartPageToken' in response:
            # Last page, save this token for the next polling interval
            saved_start_page_token = response.get('newStartPageToken')
        page_token = response.get('nextPageToken')

def main():
    creds = None
    pickle_file = 'token.pickle'
    # 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(pickle_file):
        with open(pickle_file, '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(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open(pickle_file, 'wb') as token:
            pickle.dump(creds, token)
    
    global service

    service = build('drive', 'v3', credentials=creds)

    # Call the Drive v3 API
    response = service.changes().getStartPageToken().execute()

    global saved_start_page_token
    saved_start_page_token = response.get('startPageToken')

    schedule.every().minute.do(job)

    while True:
        schedule.run_pending()
        time.sleep(1)

if __name__ == '__main__':
    main()

我可以过滤掉更改以仅显示文件/文件夹创建更改吗?

0 个答案:

没有答案