label automation based on age of card (solved)
Asked by Tiffany Bilgore:

Hello Everyone,

Thanks in advance for any help anyone can give me. I am trying to determine the best way to automate adding one of four labels to all cards in a list based on there age. I have been fiddling with various add ons but I am having trouble succeeding and I was hoping for some recommendations.

Basically I want when a card is created a green label be automatically assigned to it saying 0-30 days

Then once the card is 31 days old a yellow one saying 31-60 days replace the green one

then once day 61 hits an orange one saying 61-90 would replace the yellow

then on day 91 a final red 91+ label would replace the orange one.

Again any help is much appreciated.

Thanks,


Answers:
author: Tiffany Bilgore
body: I solved the problem by creating the following python script that I run once per day as a cron job:

#!/usr/bin/python  
import requests  
import json  
import datetime  
import calendar  
import time  

# Api Key  
key = {put your api key here}  

# Api Token  
token = {put your api token here}  

# List ID  
list = {put your list id here}

<br />

# Green Label (0-30 days) ID  
greenLabel = {id for the green label}  

# Yellow Label (31-60 days) ID  
yellowLabel = {id for the yellow label}  

# Orange Label (61-90 days) ID  
orangeLabel = {id for the orange label}  

# Red Label (91+ days) ID  
redLabel = {id for the red label}  

#Get Current Time  
currentTime = int(time.time())  

#############################  
#Get Date Object was created#  
#############################  
def GetCreationTimestamp(objID):  
return int(objID\[0:8\],16)  

#############################  
#---Get cards on the list---#  
#############################  
def GetCards():  
url = "<https://api.trello.com/1/lists/%s/cards>" % list  
querystring = {"key":key,"token":token}  
response = requests.request("GET", url, params=querystring)  
data = json.loads(response.text)  
return data  

#############################  
#---Set label on a cards----#  
#############################  
def SetLabel(cardID, labelID):  
url = "<https://api.trello.com/1/cards/%s/idLabels>" % cardID  
querystring = {"value":labelID,"key":key,"token":token}  
response = requests.request("POST", url, params=querystring)  
return response.text  

#############################  
#--Delete label on a card--#  
#############################  
def DeleteLabel(cardID, labelID):  
url = "<https://api.trello.com/1/cards/%s/idLabels/%s>" % (cardID, labelID)  
querystring = {"key":key,"token":token}  
response = requests.request("DELETE", url, params=querystring)

#loop through every card on the list  
for val in GetCards():  
#sleep for 10 seconds this will ensure the api rate limit is never reached  
time.sleep(10)  

#get the id for the current card in the loop  
id = val\['id'\]  

#prints out the id of the current card in the loop  
print("ID: %s" % val\['id'\])

#gets the UNIX timestamp of the current card and current time  
d1 = GetCreationTimestamp(id)  
d2 = currentTime  

#prints out the time the current card was created as UNIX timestamp  
print("Time Created: %s" % d1)  

#prints the current time as a UNIX timestamp  
print("Current Time: %s" % d2)  

#gets the age of the current card in days  
age = ((d2 - d1) / 86400)  

#prints out the age of the current card  
print("Age: %s days" % age)  

#if/elif block to set labels to the card  
if(age \>= 91):  
DeleteLabel(id, orangeLabel)  
print("Setting 91+ tag")  
SetLabel(id, redLabel)  
elif(age \>= 61):  
DeleteLabel(id, yellowLabel)  
print("Setting 61-90 tag")  
SetLabel(id, orangeLabel)  
elif(age \>= 31):  
DeleteLabel(id, greenLabel)  
print("Setting 31-60 tag")  
SetLabel(id, yellowLabel)  
elif(age \<= 30):  
print("Setting 0-30 tag")  
SetLabel(id, greenLabel)

comments: author: Alice
body: Awesome! Thank you for sharing.


author: Alice
body: It isn't possible to automate that with the basic Trello functions, but you try to do that using Butler! <https://butlerfortrello.com/>

comments: author: Tiffany Bilgore
body: Ah well I actually resolved the problem with a python script I made using the trello api running once a day as a cron job I will post it below for anyone that wants to use that solution.