Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python receive Google Drive push notification

Since the Drive SDK v3 we are able to receive push notifications from Google Drive whenever a file has changed. At the moment I'm working on a Drive application in Python and I would like to receive such notifications. Do I really need a web server for this or can I implement this maybe with a socket or something like this?

I know that I can get changes by polling the changes.list method but I want to avoid this because of so many API calls. Is there maybe a better way to get informed if a file has changed?

EDIT: I captured my web traffic and saw, that the original Google Drive Client for Windows uses push notifications. So in some way it must be possible to get push notifications in a desktop application but is this maybe some sort of Google magic which we can't use with the current API

like image 606
Cilenco Avatar asked Oct 30 '22 23:10

Cilenco


1 Answers

For Google Drive apps that need to keep track of changes to files, the Changes collection provides an efficient way to detect changes to all files, including those that have been shared with a user. The collection works by providing the current state of each file, if and only if the file has changed since a given point in time.

Retrieving changes requires a pageToken to indicate a point in time to fetch changes from.

# Begin with our last saved start token for this user or the
# current token from getStartPageToken()
page_token = saved_start_page_token;
while page_token is not None:
response = drive_service.changes().list(pageToken=page_token,
fields='*',
spaces='drive').execute()
for change in response.get('changes'):
# Process change
print 'Change found for file: %s' % change.get('fileId')
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')
like image 93
Android Enthusiast Avatar answered Jan 04 '23 14:01

Android Enthusiast