Last active
September 21, 2019 00:45
-
-
Save peterfoxflick/c33f2defb2fd0717add032e79a5119a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var key = "xxxxx"; // for trello | |
var token = "xxxxx"; // for trello | |
function main() { | |
//First get all homework assignments | |
var homework = UrlFetchApp.fetch('https://byui.instructure.com/api/v1/users/self/upcoming_events?access_token=xxxxxx'); | |
homework = JSON.parse(homework); | |
//Now create cards for each one | |
homework.forEach(createCard); | |
} | |
//Currate the creation of a card. | |
function createCard(assignment){ | |
var label = getLabel(assignment); | |
var day = getListId(assignment); | |
var des = ""; | |
if(assignment.description) { | |
var regex = new RegExp('<[^>]*>', 'gi'); | |
des = assignment.description.replace(regex,''); | |
} | |
var trelloParams = { | |
name: assignment.title, // Id of the card to copy if you copy a card | |
desc: des, | |
due: assignment.assignment.due_at, | |
idList: day, | |
idLabels: label, | |
key: key, | |
token: token | |
} | |
var options = { | |
method : 'POST', | |
contentType: 'application/json', | |
payload : JSON.stringify(trelloParams) | |
} | |
var url = 'https://api.trello.com/1/cards'; | |
var trelloResponce = UrlFetchApp.fetch(url, options); | |
trelloResponce = JSON.parse(trelloResponce) | |
addLink(trelloResponce.id, assignment.assignment.html_url, "Canvas"); | |
} | |
//Get the label for the course based on which course it is. | |
function getLabel(assignment){ | |
var id = assignment.context_code; | |
if(id == "course_xxx") | |
return "xxx" | |
if(id == "course_xxx") | |
return "xxx" | |
if(id == "course_xxx") | |
return "xxx" | |
} | |
//Add a link to a card. | |
function addLink(id, link, name) { | |
//Creating the data to be sent to trello | |
var addLinkUrl = 'https://api.trello.com/1/cards/' + id + '/attachments?'; | |
var trelloParams = { | |
name: name, // name of attachment | |
url: link, // URL to attach | |
key: key, | |
token: token | |
}; | |
//JSON options | |
var options = { | |
method: 'POST', | |
contentType: 'application/json', | |
payload: JSON.stringify(trelloParams) | |
}; | |
//Send attachment creation request | |
UrlFetchApp.fetch(addLinkUrl, options); | |
} | |
//Get the day its due and add it to that list. | |
function getListId(assignment){ | |
var due = new Date(assignment.assignment.due_at); | |
Logger.log(due.getDay()); | |
switch(due.getDay()) { | |
case 0: // sunday | |
return "xxx" | |
break; | |
case 1: // monday | |
return "xxx"; | |
break; | |
case 2: // tuesday | |
return "xxx"; | |
break; | |
case 3: // wednesday | |
return "xxx"; | |
break; | |
case 4: // thrusday | |
return "xxx"; | |
break; | |
case 5: // friday | |
return "xxx"; | |
break; | |
case 6: // satruday | |
return "xxx"; | |
break; | |
default: //inbox | |
return "xxx"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment