担心哪天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()