0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

指定したTrelloボード上にあるカード情報をCSVへ定期的にエクスポートしたい

Posted at

概要

Trelloボードのデータをエクスポートする方法はいくつかあります。
JsonやCSV形式でのエクスポート方法です。

ただ、これらの方法はWEBブラウザでのGUI経由になってしまい、自動で定期的なエクスポートをしたい場合に向いていません。
例えば、Trelloボード上のデータをバックアップしたい場合や、時間経過でデータを解析したい場合は定期的なエクスポートが必要です。

Pythonで簡単なアプリケーションを作成したため、その技術的な内容を記載します。
なお、コードはGithubのTrelloCsvExporterというリポジトリで公開しています。

環境

  • Windows10
  • Python3.7

前提

TrelloAPIのアクセストークンが必要です。
開発者向けAPIキー

コード概要

使っているのは以下の3ファイルです。

  • config.py:設定ファイルのインポート
  • convertJsonToCsv.py:JsonデータをCSVへ変換
  • getTrelloBoard.py :TrelloAPIを使ってTrelloボードのデータをJson形式で取得

またconfig.pyが読み込む設定ファイルとしてconfig.iniを使用します。

コード詳細

convertJsonToCsv.py

TrelloAPIで取得したJsonデータをCSVへ変換しています。

Trelloボードの情報を取得するAPI経由だと、 Trelloのリストが名前ではなくID として取得されます。
そのため

trello_lists: dict = {trello_list["id"]: trello_list["name"] for trello_list in get_lists_on_trello_board()}

という風にIDと名前の辞書を作成しています。

また、CSVファイルへエクスポートする際にopen関数のオプションでnewline=""としています。
これはエクスポートされたファイルに不要な空白行が入り込まないようにするためです。

CSVファイル(newline="" なし) CSVファイル(newline="" あり)
hogehoge1 hogehoge1
空白行 hogehoge2
hogehoge2 hogehoge3
空白行  
hogehoge3  

CSVエクスポートに関してはこちらのサイトが参考になりました。
PythonでCSVファイルを読み込み・書き込み(入力・出力)

convertJsonToCsv.py
import csv
from typing import List

from getTrelloBoard import get_cards_on_trello_board, get_lists_on_trello_board

output_path: str = "output.csv"

row_data_list: List = []

trello_lists: dict = {trello_list["id"]: trello_list["name"] for trello_list in get_lists_on_trello_board()}

for card in get_cards_on_trello_board():
    row_data: dict = {
        "list": trello_lists[card["idList"]],
        "name": card["name"],
        "label": " ".join([label["name"] for label in card["labels"]]),
        "link": card['url']
    }
    row_data_list.append(row_data)


with open(output_path, "w", encoding="utf-8", newline="") as f:
    writer: csv.DictWriter = csv.DictWriter(f, row_data_list[0].keys())
    writer.writeheader()
    writer.writerows(row_data_list)

getTrelloBoard.py

TrelloAPIを使ってTrelloボードのデータをJson形式で取得しています。

requestsライブラリを使うことで簡単にAPIへアクセスしデータを取得出来ます。

getTrelloBoard.py
import requests

import config


def get_cards_on_trello_board() -> list:
    """
    Trelloボード上にある全てのカードを取得する関数
    Returns
    --------
    r.json(): list
        カードの情報
    """
    payload: dict = {
        "key": config.rule_file["trello-api-key"]["key"],
        "token": config.rule_file["trello-api-key"]["token"]
    }

    r = requests.get(f"https://api.trello.com/1/boards/{config.rule_file['trello-board-id']['id']}/cards", payload)
    return r.json()


def get_lists_on_trello_board() -> list:
    """
    Trelloボード上にある全てのリストを取得する関数
    Returns
    --------
    r.json(): dict
        リストの情報
    """
    payload: dict = {
        "key": config.rule_file["trello-api-key"]["key"],
        "token": config.rule_file["trello-api-key"]["token"]
    }

    r = requests.get(f"https://api.trello.com/1/boards/{config.rule_file['trello-board-id']['id']}/lists", payload)
    return r.json()

config.py

config.iniファイルを読み込みます。
このモジュールを各ファイルで読み込むことで、設定ファイルの読み込みを一か所だけに記載すればよくなります。

config.py
import configparser

rule_file: configparser.ConfigParser = configparser.ConfigParser()
rule_file.read("./config.ini", "UTF-8")

config.ini

config.ini
[trello-api-key]
key=TrelloのAPIキー
token=TrelloのAPIトークン

[trello-board-id]
id=TrelloのボードID

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?