📚

【python】TrelloのデータをAPIで取得してCSVで出力する

2024/05/26に公開

Trello API Keyの取得

https://trello.com/power-ups/admin にアクセス
Power-Up & 統合を新規作成


APIキーとトークンを作成

TrelloAPIでボードからデータを取得する

パッケージのインストール

pip install requests

CSVファイルの出力

import requests
import json
import csv

# TrelloのAPIキーとトークン
API_KEY = 'your key'
API_TOKEN = 'your api'
USER_NAME = 'your name'


# APIエンドポイント
base_url = "https://api.trello.com/1"
boards_url = f"{base_url}/members/{USER_NAME}/boards"


# パラメータ
params = {
    'key': API_KEY,
    'token': API_TOKEN
}

# リクエストの送信
response = requests.get(boards_url, params=params)

# レスポンスのJSONデータを取得
boards_data = response.json()

# 取得したデータを整形して表示
print(json.dumps(boards_data, indent=4))

# 各ボードのIDを表示
for board in boards_data:
    print(f"Board ID: {board['id']}, Name: {board['name']}")

BOARD_ID = 'target board id'
    
# APIエンドポイント
lists_url = f"{base_url}/boards/{BOARD_ID}/lists"
cards_url = f"{base_url}/boards/{BOARD_ID}/cards"
members_url = f"{base_url}/boards/{BOARD_ID}/members"

# リスト情報の取得
lists_response = requests.get(lists_url, params=params)
lists_data = lists_response.json()

# カード情報の取得
cards_response = requests.get(cards_url, params=params)
cards_data = cards_response.json()

# メンバー情報の取得
members_response = requests.get(members_url, params=params)
members_data = members_response.json()

# メンバー情報を辞書に格納
members_dict = {member["id"]: member["fullName"] for member in members_data}

# リストIDと名前の辞書を作成
trello_lists = {trello_list["id"]: trello_list["name"] for trello_list in lists_data}

# アクティビティログの取得
def get_card_activities(card_id, params):
    activities_url = f"https://api.trello.com/1/cards/{card_id}/actions"
    activities_response = requests.get(activities_url, params=params)
    activities_data = activities_response.json()
        
    return activities_data

# カード情報を整形
row_data_list = []
for card in cards_data:
    member_names = [members_dict.get(member_id, 'Unknown Member') for member_id in card.get("idMembers", [])]
    activities = get_card_activities(card["id"], params)
    activity_log = "; ".join([
        f"{activity['type']} by {activity.get('memberCreator', {}).get('fullName', 'Unknown')} at {activity['date']}"
        for activity in activities if 'memberCreator' in activity and 'date' in activity
    ])
    row_data = {
        "list": trello_lists.get(card.get("idList", "Unknown List"), "Unknown List"),
        "name": card["name"],
        "label": " ".join([label["name"] for label in card.get("labels", [])]),
        "members": ", ".join(member_names),
    "due": card.get("due", "N/A"),
        "dateLastActivity": card.get("dateLastActivity", "N/A"),
        "activityLog": activity_log
    }
    row_data_list.append(row_data)

# CSVファイルへの書き込み
output_path = "output.csv"
with open(output_path, "w", encoding="utf-8", newline="") as f:
    writer = csv.DictWriter(f, fieldnames=row_data_list[0].keys())
    writer.writeheader()
    writer.writerows(row_data_list)

print(f"Data has been exported to {output_path}")

output.csv

https://developer.atlassian.com/cloud/trello/guides/rest-api/api-introduction/
https://qiita.com/HiguchiTakahiro/items/94f52eee07d257f8f995
https://www.gachatsuku.com/entry/2018/12/08/221500
https://qiita.com/The-town/items/f3694e48709fdfceb30a
https://qiita.com/r-wakatsuki/items/11b7d4e3dc1b91745ab4

Discussion