Created
July 26, 2018 14:44
-
-
Save deecewan/39a865e393edfb6de850541bc7ab23bd to your computer and use it in GitHub Desktop.
Some stuff to update the uni trello boards I use.
This file contains hidden or 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
// this runs every time i get to the page. it updates the list of weeks, puts the old weeks at the bottom, | |
// and adds a label to this week. It runs using TamperMonkey | |
(async () => { | |
const promisify = fn => (...args) => new Promise((resolve, reject) => { | |
fn(...args, resolve, reject); | |
}); | |
// go to https://trello.com/app-key for the key | |
const token = '<ENTER KEY HERE>'; | |
const jquery = await import('https://code.jquery.com/jquery-3.3.1.min.js'); | |
const trello = await import(`https://api.trello.com/1/client.js?key=${token}`); | |
const BOARD_NAME = 'Semester 2, 2018'; | |
const auth = await new Promise((resolve, reject) => { | |
Trello.authorize({ | |
type: 'popup', | |
name: 'Getting Started Application', | |
scope: { | |
read: 'true', | |
write: 'true' }, | |
expiration: 'never', | |
success: resolve, | |
error: reject, | |
}) | |
}) | |
const get = promisify(Trello.get.bind(Trello)); | |
const put = promisify(Trello.put.bind(Trello)); | |
const post = promisify(Trello.post.bind(Trello)); | |
const boards = await get(`/members/davidbuchanswanson/boards`); | |
const board = boards.find(b => b.name === BOARD_NAME); | |
const labels = await get(`/boards/${board.id}/labels`); | |
const thisWeekLabel = labels.find(l => l.name === 'This Week'); | |
const lists = await get(`/boards/${board.id}/lists`); | |
const list = lists.find(l => l.name === 'Weeks'); | |
const cards = (await get(`/lists/${list.id}/cards`)) | |
.sort((a, b) => a.due < b.due ? -1 : 1); | |
// the aim here is to look through all the cards, and find ones that are | |
// before the current week. We then mark them all as done and move them to | |
// the bottom of the list. | |
const lastSunday = new Date(); | |
const delta = -lastSunday.getDay(); | |
lastSunday.setDate(lastSunday.getDate() + delta); | |
const past = cards | |
.filter(c => new Date(c.due) < lastSunday); | |
await past.reduce((p, card) => | |
p.then(() => put(`/cards/${card.id}`, { pos: 'bottom', dueComplete: true })), | |
Promise.resolve(), | |
); | |
// finally, even though 'This Week' will *always* be the top card, we're going | |
// to add a label that says that for some extra effect. | |
// We'll also mark it as 'done' so that it has the green date | |
// find works optimistically, so it will stop at the first it finds | |
const thisWeek = cards.find(c => new Date(c.due) > lastSunday); | |
await put(`/cards/${thisWeek.id}`, { dueComplete: true, idLabels: thisWeekLabel.id }); | |
})(); |
This file contains hidden or 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
// this runs as a once off. paste into chrome snippets and hit run. | |
// this adds all the weeks to the 'weeks' column so i can keep track of what | |
// week I'm currently in | |
(async () => { | |
const promisify = fn => (...args) => new Promise((resolve, reject) => { | |
fn(...args, resolve, reject); | |
}); | |
// go to https://trello.com/app-key for the key | |
const token = '<ENTER KEY HERE>'; | |
const jquery = await import('https://code.jquery.com/jquery-3.3.1.min.js'); | |
const trello = await import(`https://api.trello.com/1/client.js?key=${token}`); | |
const BOARD_NAME = 'Semester 2, 2018'; | |
const auth = await new Promise((resolve, reject) => { | |
Trello.authorize({ | |
type: 'popup', | |
name: 'Getting Started Application', | |
scope: { | |
read: 'true', | |
write: 'true' }, | |
expiration: 'never', | |
success: resolve, | |
error: reject, | |
}) | |
}) | |
const get = promisify(Trello.get.bind(Trello)); | |
const post = promisify(Trello.post.bind(Trello)); | |
const boards = await get(`/members/davidbuchanswanson/boards`); | |
const board = boards.find(b => b.name === BOARD_NAME); | |
console.log(board); | |
const lists = await get(`/boards/${board.id}/lists`); | |
const list = lists.find(l => l.name === 'Weeks'); | |
console.log(lists); | |
const start = new Date('2018-07-23T09:00:00'); | |
const cards = []; | |
let week = 1; | |
while (start < new Date('2018-09-21')) { | |
cards.push({ | |
name: `Week ${week++}`, | |
idList: list.id, | |
due: start.toISOString(), | |
dueComplete: start < new Date(), | |
}); | |
start.setDate(start.getDate() + 7); | |
} | |
let holidays = 1; | |
while (start < new Date('2018-10-01')) { | |
cards.push({ | |
name: `Holidays ${holidays++}`, | |
idList: list.id, | |
due: start.toISOString() | |
}); | |
start.setDate(start.getDate() + 7); | |
} | |
while (start < new Date('2018-10-26')) { | |
cards.push({ | |
name: `Week ${week++}`, | |
idList: list.id, | |
due: start.toISOString() | |
}); | |
start.setDate(start.getDate() + 7); | |
} | |
let exams = 1; | |
start.setTime(new Date('2018-11-01')); | |
while (start < new Date('2018-11-16')) { | |
cards.push({ | |
name: `Exams ${exams++}`, | |
idList: list.id, | |
due: start.toISOString() | |
}); | |
if (start.getDay() !== 1) { | |
start.setDate((start.getDate() + (1 - start.getDay()))) | |
} | |
start.setDate(start.getDate() + 7); | |
} | |
await cards.reduce((p, card, i) => { | |
return p.then(() => post('cards', { ...card, pos: i + 1 })); | |
}, Promise.resolve()); | |
console.log('cards created'); | |
})(); 'test'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These aren't really designed for (re)use. The code is sloppy and definitely not as good as it could be.