python备份trello笔记

担心哪天trello关闭或者访问太慢,备份一下离线的cards&comments&attachments心里比较踏实

参照:https://developers.trello.com/advanced-reference

# -*- coding: UTF-8 -*-

import requests
import json
import time

# FIXME: 参照 https://trello.com/app-key 填入key和token
key = 'your app key'
token = 'your token'

# get me board ids
get_boards_url = 'https://api.trello.com/1/members/me/boards?fields=name,url,dateLastActivity,idTags,starred,invited,pinned,dateLastView,closed&key={key}&token={token}'

# get xx by board id
get_cards_url = 'https://api.trello.com/1/boards/{board_id}/cards?fields=name,desc,idList,idBoard,url,closed,dateLastActivity,idAttachmentCover,idLabels&filter=all&key={key}&token={token}'
get_lists_url = 'https://api.trello.com/1/boards/{board_id}/lists?fields=name,closed&key={key}&token={token}'
get_labels_url = 'https://api.trello.com/1/boards/{board_id}/labels?fields=name,color,uses&key={key}&token={token}'

# get card attachments by card id
get_attachments_url = 'https://api.trello.com/1/cards/{card_id}/attachments?key={key}&token={token}'

# get card actions/comments by card id
get_actions_url = 'https://api.trello.com/1/cards/{card_id}/actions?key={key}&token={token}'


# output:
# board | list | card | actions(comments) & attachments

def _get_boards():
    url = get_boards_url.format(key=key, token=token)
    r = requests.get(url)
    return [b['id'] for b in r.json()]


def _get_cards():
    cards = []
    for board_id in _get_boards():
        url = get_cards_url.format(board_id=board_id, key=key, token=token)
        r = requests.get(url)
        cards.extend([c for c in r.json()])
    return cards


def save_actions_attachments():
    for card in _get_cards():
        card_id = card['id']
        list_id = card['idList']
        board_id = card['idBoard']

        # 获取actions
        url = get_actions_url.format(card_id=card_id, key=key, token=token)
        r = requests.get(url)
        actions = r.json()
        result_actions = []
        for action in actions:
            action['idCard'] = card_id
            action['idList'] = list_id
            action['idBoard'] = board_id
            result_actions.append(action)

        # 获取attachments
        result_attachments = []
        attachment_id = card['idAttachmentCover']
        if attachment_id:
            url = get_attachments_url.format(card_id=card_id, key=key, token=token)
            r = requests.get(url)
            attachments = r.json()
            for attachment in attachments:
                attachment['idCard'] = card_id
                attachment['idList'] = list_id
                attachment['idBoard'] = board_id
                result_attachments.append(attachment)

        if result_actions or result_attachments:
            result = {'idCard': card_id, 'idList': list_id, 'idBoard': board_id,
                      'actions': result_actions, 'attachments': result_attachments}
            s_result = json.dumps(result)
            with open('trello_backup.%s' % time.strftime('%Y%m%d'), 'a+') as f:
                f.write(s_result + '\n')
                f.flush()


save_actions_attachments()

转载于:https://my.oschina.net/1123581321/blog/841445

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值