如何以编程方式确定 Dropbox 文件夹位置? [英] How to determine the Dropbox folder location programmatically?

查看:31
本文介绍了如何以编程方式确定 Dropbox 文件夹位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,供多个用户在多台计算机上运行,​​但他们的 Dropbox 文件夹并不都在各自的主目录中.我不想在脚本中硬编码路径.我更愿意以编程方式找出路径.

I have a script that is intended to be run by multiple users on multiple computers, and they don't all have their Dropbox folders in their respective home directories. I'd hate to have to hard code paths in the script. I'd much rather figure out the path programatically.

欢迎提出任何建议.

我没有在脚本中使用 Dropbox API,脚本只是读取用户共享的特定 Dropbox 文件夹中的文件.我唯一需要的是 Dropbox 文件夹的路径,因为我当然已经知道 Dropbox 文件结构中的相对路径.

I am not using the Dropbox API in the script, the script simply reads files in a specific Dropbox folder shared between the users. The only thing I need is the path to the Dropbox folder, as I of course already know the relative path within the Dropbox file structure.

如果重要的话,我使用的是 Windows 7.

If it matters, I am using Windows 7.

推荐答案

我找到了答案 此处.将 s 设置为 ~AppDataRoamingDropboxhost.db 中的第 2 行,然后用 base64 解码给出路径.

I found the answer here. Setting s equal to the 2nd line in ~AppDataRoamingDropboxhost.db and then decoding it with base64 gives the path.

def _get_appdata_path():
    import ctypes
    from ctypes import wintypes, windll
    CSIDL_APPDATA = 26
    _SHGetFolderPath = windll.shell32.SHGetFolderPathW
    _SHGetFolderPath.argtypes = [wintypes.HWND,
                                 ctypes.c_int,
                                 wintypes.HANDLE,
                                 wintypes.DWORD,
                                 wintypes.LPCWSTR]
    path_buf = wintypes.create_unicode_buffer(wintypes.MAX_PATH)
    result = _SHGetFolderPath(0, CSIDL_APPDATA, 0, 0, path_buf)
    return path_buf.value

def dropbox_home():
    from platform import system
    import base64
    import os.path
    _system = system()
    if _system in ('Windows', 'cli'):
        host_db_path = os.path.join(_get_appdata_path(),
                                    'Dropbox',
                                    'host.db')
    elif _system in ('Linux', 'Darwin'):
        host_db_path = os.path.expanduser('~'
                                          '/.dropbox'
                                          '/host.db')
    else:
        raise RuntimeError('Unknown system={}'
                           .format(_system))
    if not os.path.exists(host_db_path):
        raise RuntimeError("Config path={} doesn't exists"
                           .format(host_db_path))
    with open(host_db_path, 'r') as f:
        data = f.read().split()

    return base64.b64decode(data[1])

这篇关于如何以编程方式确定 Dropbox 文件夹位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆