python bsdumper:转储Basecamp Classic项目中的所有文件。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python bsdumper:转储Basecamp Classic项目中的所有文件。相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python

"""
bsdumper: Dumps all files from a Basecamp Classic projects.

Description:
  Why??? - Because existing solutions lacked attachment support and you
  can't export without owner permissions.

Author: Daniel Smith (https://github.com/ifnull/)

Usage: python ./bsdumper.py {domain} {api_key} {project_id}
  - domain: yourcompany.basecamphq.com
  - api_key: Found in 'My info' in the Authentication tokens section
  - project_id: Found in URL

Example:
  python ./bsdumper.py foobar.basecamphq.com 99999999999999999 1234567

Requirements:
  pip install requests elementtree

"""

import os
import sys
import requests
import xml.etree.ElementTree as ET

domain = sys.argv[1]
api_key = sys.argv[2]
project_id = sys.argv[3]
api_basepath = 'https://{0}/'.format(domain)
outdir = 'bsdumper-{0}'.format(project_id)
headers = {
    'Accept': 'application/xml',
    'Content-Type': 'application/xml'
}


def main():
    get_attachements()


def api(endpoint):
    url = api_basepath + endpoint
    req = requests.get(url, auth=(api_key, '.'), headers=headers)
    res = ET.fromstring(req.text)
    return res


def download(name, url):
    req = requests.get(url, auth=(api_key, '.'), headers=headers, stream=True)
    filename = '{0}/{1}'.format(outdir, name)
    with open(filename, 'wb') as f:
        for chunk in req.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
                f.flush()


def get_attachements():
    if not os.path.exists(outdir):
        print 'Creating directory: {0}'.format(outdir)
        os.makedirs(outdir)

    pagination_increment = 100  # Set by API itself
    pagination_offset = 0
    pagination_next = True

    while pagination_next:

        endpoint = 'projects/{0}/attachments.xml?n={1}'.format(
            project_id, pagination_offset)

        attachments = api(endpoint)
        attachment_count = len(attachments)

        print 'Downloading {0} attachments.'.format(attachment_count)

        for attachment in attachments:
            name = attachment.find('name').text
            url = attachment.find('download-url').text

            print '-- {0}'.format(name)
            download(name, url)

        if attachment_count == 0:
            pagination_next = False
        else:
            pagination_offset = pagination_offset + pagination_increment


if __name__ == "__main__":
    main()

以上是关于python bsdumper:转储Basecamp Classic项目中的所有文件。的主要内容,如果未能解决你的问题,请参考以下文章

python 将MySQL数据库转储到PG兼容的SQL转储中

使用 python 线程时,python 因核心转储而崩溃

打开 python 核心转储后调试器中的 Python shell

python python转储变量异常

Python - 将 dict 转储为 json 字符串

python DNS转储程序:转储域分隔文件的名称服务器。只需传递包含行分隔域的文件即可。