从Dropbox API构建目录树 [英] Build directory tree from dropbox API
问题描述
我想做的是使用python绑定,从dropbox API为给定路径构建树,并为每个路径共享链接。
What I'd like to do is to build a tree from the dropbox API, for a given path, with share links for each path, using the python bindings.
我建议的结构如下:
[
{
'path': '/a',
'is_dir': True,
'contents': [
{
'path': '/a/b',
'is_dir': True,
'contents': [etc]
},
{
'path': '/a/readme.txt',
'is_dir': False,
'share_link': 'http://etc'
}
]
},
etc.
]
我有一些东西大多数使用 metadata()
可以工作,但这很丑陋
I've got something that mostly works using metadata()
but it's hideously slow as it needs to make an API call per directory traversed.
我要使用的是 delta()
,它将在一个请求中为我提供每个文件,然后将其构建到树中,但是我在弄清楚具体如何操作方面存在问题,尤其是如何将路径解析到树中。
What I'd like to use instead is delta()
, which will get me every file in one request, then build it into a tree, but I'm having problems figuring out exactly how, in particular how to parse the paths into a tree.
编辑:意识到每个共享链接都有一个调用,因此我将忽略这些链接,仅在需要时获取它们。
And I've realised there's a call for each share link so I'm going to omit those and just get them when requested.
以下是我必须获取所需数据的一些代码,因此远:
Here's some code I have to get the data I need so far:
paths = []
for path, metadata in client.delta(path_prefix='/whatever')['entries']:
paths.append({
'path': path,
'is_dir': metadata['is_dir']
})
所以我想我很难弄清楚如何嵌套这些路径。可以肯定的是,我在这里需要一个递归函数,但是无法弄清楚。
So I guess I'm having trouble figuring out how to get those paths nested. Pretty sure I need a recursive function here but can't quite figure it out.
推荐答案
我稍微调整了一下结构。 ..这是以下代码产生的JSON表示形式。请注意,我已将content字段设置为按路径而不是数组索引的字典。这样做稍微容易一些,并且可以提高查找效率,但是如果需要,可以很容易地将其转换为上面的内容:
I tweaked your structure just a bit... here's the JSON representation of what the below code produces. Note that I've made the contents field a dictionary indexed by path instead of an array. This is just a little easier and makes for more efficient lookup, but it should be pretty easy to convert to exactly what you have above if you want:
{
"is_dir": true,
"contents": {
"/foo.txt": {
"is_dir": false,
"contents": {}
},
"/a": {
"is_dir": true,
"contents": {
"/a/bar.txt": {
"is_dir": false,
"contents": {}
},
"/a/b": {
"is_dir": true,
"contents": {
"/a/b/hello.txt": {
"is_dir": false,
"contents": {}
}
}
}
}
}
}
}
这是产生该输出的代码:
Here's the code that produces that output:
ACCESS_TOKEN = '<REDACTED>'
from collections import defaultdict
import json
from dropbox.client import DropboxClient
def make_tree():
return {
'is_dir': True,
'contents': defaultdict(make_tree)
}
tree = defaultdict(make_tree)
client = DropboxClient(ACCESS_TOKEN)
has_more = True
cursor = None
while has_more:
delta = client.delta(cursor)
cursor = delta['cursor']
has_more = delta['has_more']
for path, metadata in delta['entries']:
if metadata is not None:
# find the right place in the tree
segments = path.split('/')
location = tree['/']
for i in xrange(1, len(segments)-1):
current_path = '/'.join(segments[:i+1])
location = location['contents'][current_path]
# insert the new entry
location['contents'][path] = {
'is_dir': metadata['is_dir'],
'contents': {}
}
print json.dumps(tree['/'], indent=4)
这篇关于从Dropbox API构建目录树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!