Reputation: 55
I'm trying to detect if a file is deleted in my own dropbox application's folder.
So, i set up a webhook to receive notifications and i used DropboxClient.delta method to know what actually changes.
According to the documentation, delta method returns a dictionary with 4 keys, and 'entries' key contains A list of "delta entries"
. An entry is [path, metadata]
(path is a string, metadata is a dict) and metadata is None when a file is deleted. But i never got it to be None, even if a file is actually deleted from the directory.
Here is my webhook notifications receiver:
@app.route('/webhook', methods=['POST', 'GET'])
def webhook():
if request.method == 'GET':
return request.args.get('challenge')
fh = open('dropbox.txt', 'a') # log file
client = dropbox.client.DropboxClient('<access token>')
for uid in json.loads(request.data)['delta']['users']:
fh.write('User %d is doing something:\n' % uid)
cursor = None
has_more = True
while has_more:
result = client.delta(cursor)
i = 1
for path, metadata in result['entries']:
fh.write('Metatadata: %s\n' % json.dumps(metadata) )
i = i + 1
cursor = result['cursor']
has_more = result['has_more']
fh.write('End logging activities (#%d) of user %d\n' % (i - 1, uid))
fh.close()
return ''
So, I added some files (3) in my directory, and in my log file i could see:
User <uid> is doing something:
Metatadata: {"revision": 3, "bytes": 3952, "thumb_exists": false, "rev": "33bb68089", "modified": "Fri, 28 Aug 2015 13:40:32 +0000", "mime_type": "text/x-python", "path": "/a.py", "is_dir": false, "size": "3.9 KB", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:32 +0000", "icon": "page_white_code"}
Metatadata: {"revision": 4, "bytes": 812, "thumb_exists": false, "rev": "43bb68089", "modified": "Fri, 28 Aug 2015 13:40:34 +0000", "mime_type": "text/x-python", "path": "/b.py", "is_dir": false, "size": "812 bytes", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:34 +0000", "icon": "page_white_code"}
Metatadata: {"revision": 5, "bytes": 295, "thumb_exists": false, "rev": "53bb68089", "modified": "Fri, 28 Aug 2015 13:40:35 +0000", "mime_type": "text/x-python", "path": "/c.py", "is_dir": false, "size": "295 bytes", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:35 +0000", "icon": "page_white_code"}
End logging activities (#3) of user <uid>
I deleted one file (c.py) and in my log file i saw:
User <uid> is doing something:
Metatadata: {"revision": 3, "bytes": 3952, "thumb_exists": false, "rev": "33bb68089", "modified": "Fri, 28 Aug 2015 13:40:32 +0000", "mime_type": "text/x-python", "path": "/a.py", "is_dir": false, "size": "3.9 KB", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:32 +0000", "icon": "page_white_code"}
Metatadata: {"revision": 4, "bytes": 812, "thumb_exists": false, "rev": "43bb68089", "modified": "Fri, 28 Aug 2015 13:40:34 +0000", "mime_type": "text/x-python", "path": "/c.py", "is_dir": false, "size": "812 bytes", "root": "app_folder", "client_mtime": "Fri, 28 Aug 2015 13:40:34 +0000", "icon": "page_white_code"}
End logging activities (#2) of user <uid>
Every time something changes in the directory, i get a list of all the files in that directory also the non-modified one. No trace of deleted ones though. I thought that i would only received modified or deleted files.
Is this a normal behaviour? Thanks.
Upvotes: 1
Views: 590
Reputation: 60143
It looks like you're not keeping track of the cursor between notifications. So each time there's a change, you're calling delta
with an empty cursor, which means "Tell me about all the files." What you want to be doing is calling it with the cursor you got from the last time around, which means "Tell me what's changed since (cursor)."
You'll need to persist the cursor somewhere (e.g. in a database) on the server and be sure to always use the latest cursor when asking Dropbox for changes.
Upvotes: 1