Dropbox webhook,检测已删除的文件

时间:2015-08-28 14:15:59

标签: python python-2.7 dropbox-api webhooks

我试图检测我自己的Dropbox应用程序文件夹中是否删除了某个文件。 所以,我设置了一个webhook接收通知,我使用 DropboxClient.delta 方法来了解实际发生的变化。 根据文档, delta 方法返回一个包含4个键的字典,以及'条目'密钥包含A list of "delta entries"。删除文件时,条目为[path, metadata](路径为字符串,元数据为字典),元数据为。但我从来没有得到它,即使文件实际上已从目录中删除。 这是我的webhook通知接收器:

@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 ''

所以,我在我的目录中添加了一些文件(3),在我的日志文件中我可以看到:

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>

我删除了一个文件(c.py)并在我的日志文件中看到:

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>

每次目录中的某些内容发生变化时,我都会得到该目录中所有文件的列表,也是未修改过的文件。虽然没有删除的痕迹。 我以为我只会收到修改或删除的文件。

这是正常行为吗? 感谢。

1 个答案:

答案 0 :(得分:1)

看起来你没有跟踪通知之间的光标。因此,每次有更改时,您都会使用空光标调用delta,这意味着“告诉我有关所有文件的信息”。你想要做的是用最后一次光标调用它,这意味着“告诉我自从(光标)以来发生了什么变化。”

您需要将光标保留在服务器上的某个位置(例如,在数据库中),并确保在向Dropbox请求更改时始终使用最新光标。