📅  最后修改于: 2022-03-11 14:46:04.045000             🧑  作者: Mango
#Trello.py
import requests
from datetime import datetime
from datetime import timedelta
class TrelloClient:
def __init__(self, user_id, key, secret, token):
self.user_id = user_id
self.key = key
self.secret = secret
self.token = token
self.URL = 'https://trello.com/1/'
def get_board_id(self, board_name):
"""
ãã¼ãåãããã¼ãidãç¹å®ãã
"""
end_point = f"members/{self.user_id}/boards?key={self.key}&token={self.token}&fields=name"
json_data = requests.get(self.URL + end_point).json()
for json in json_data:
if json['name'] == board_name:
return json['id']
def get_board_names(self):
"""
ãã¼ãåã®ãªã¹ããè¿ã
"""
end_point = f"members/{self.user_id}/boards?key={self.key}&token={self.token}&fields=name"
json_data = requests.get(self.URL + end_point).json()
return [json['name'] for json in json_data]
def get_list_id(self, board_id, list_name):
"""
ãã¼ãidã¨Trelloãªã¹ãåããTrelloãªã¹ãidãç¹å®ãã¦è¿ã
"""
end_point = f"boards/{board_id}/lists?key={self.key}&token={self.token}&fields=name"
json_data = requests.get(self.URL + end_point).json()
for json in json_data:
if json['name'] == list_name:
return json['id']
def get_list_ids_and_names(self, board_id):
"""
idã¨nameãã¿ãã«ã«ãªã£ããªã¹ããè¿ã
"""
end_point = f"boards/{board_id}/lists?key={self.key}&token={self.token}&fields=name"
json_data = requests.get(self.URL + end_point).json()
return [(json['id'], json['name']) for json in json_data]
def add_task(self, list_id, card_name, due_date=None, due_time=None, desc=None):
"""
ã«ã¼ããç¹å®ã®ãªã¹ãã«è¿½å ãã
"""
end_point = "cards"
if due_date and due_time:
due = datetime.strptime(due_date + ' ' + due_time, '%Y/%m/%d %H:%M')
# ãã®ã¾ã¾ç»é²ããã¨13æéå¾ã«ãªãä»æ§ã®ãã
due = due - timedelta(hours=13)
due = due.isoformat()
else:
due = ""
query = {
'key': self.key,
'token': self.token,
'idList': list_id,
'name': card_name,
'desc': desc,
'due': due}
requests.request("POST", self.URL + end_point, params=query)
def get_cards_in_list(self, list_id):
"""
Trelloãªã¹ãã®ä¸ã®ã«ã¼ããjsonå½¢å¼ã§è¿ã
"""
end_point = f"lists/{list_id}/cards"
query = {
'key': self.key,
'token': self.token
}
response = requests.request(
"GET",
self.URL + end_point,
params=query
)
return response.json()