Created
July 28, 2014 21:01
-
-
Save bfritz/74c0940cceafd5d36dd1 to your computer and use it in GitHub Desktop.
Trello json-to-csv converter with muster
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
package com.bfritz.trello.trellojsontocsv | |
import muster.{Consumer,MappingException} | |
import muster.ast.{AstNode,NullNode,ObjectNode,TextNode} | |
import muster.codec.jackson.JacksonCodec | |
import java.io.{File,FileWriter} | |
import au.com.bytecode.opencsv.CSVWriter | |
object TrelloJsonToCsvApp extends App { | |
val csvHeaders = List("List", "Card", "Labels", "Link", "Description", "Card Created") | |
implicit object ActionConsumer extends Consumer[Option[Action]] { | |
def consume(node: AstNode[_]): Option[Action] = node match { | |
case obj: ObjectNode => | |
val typeConsumer = implicitly[Consumer[String]] | |
for { | |
data <- obj.readObjectFieldOpt("data") | |
card <- data.readObjectFieldOpt("card") | |
id <- card.readStringFieldOpt("id") | |
} yield { | |
Action( | |
typeConsumer.consume(obj.readStringField("type")), | |
obj.readStringField("date").value, | |
id.value | |
) | |
} | |
case n => throw new MappingException(s"Can't convert a ${n.getClass} to a Action") | |
} | |
} | |
val b = JacksonCodec.as[Board](new File("trello.json")) | |
val listsByName = b.lists.map(_.name).zip(b.lists).toMap | |
val listsById = b.lists.map(_.id).zip(b.lists).toMap | |
val cardsByList = b.cards.groupBy(c => listsById(c.idList)) | |
val cardCreatedActions = b.actions.flatten.filter(_.atype == "createCard") | |
val createdDateByCard = cardCreatedActions.map{ a => (a.cardId, a.date) }.toMap | |
// println(createdDateByCard) | |
val writer = new CSVWriter(new FileWriter("trello.csv")) | |
writer.writeNext(csvHeaders) | |
for (c <- cardsByList(listsByName("Doing")).sortBy(_.pos)) writeRow(c) | |
for (c <- cardsByList(listsByName("To Do")).sortBy(_.pos)) writeRow(c) | |
for (c <- cardsByList(listsByName("Done")).sortBy(_.pos)) writeRow(c) | |
for (c <- cardsByList(listsByName("Postponed")).sortBy(_.pos)) writeRow(c) | |
def writeRow(card: Card) { | |
writer.writeNext(Array[String]( | |
listsById(card.idList).name, | |
card.name, | |
card.labels.map(_.name).mkString(", "), | |
card.shortUrl, | |
card.desc, | |
createdDateByCard.getOrElse(card.id, "") | |
)) | |
} | |
writer.close | |
case class Card(id: String, idList: String, name: String, desc: String, labels: List[Label], shortUrl: String, pos: Float) | |
case class CardList(id: String, name: String) | |
case class Label(name: String, color: String) | |
case class Action(atype: String, date: String, cardId: String) | |
case class Board(id: String, name: String, lists: List[CardList], cards: List[Card], actions: List[Option[Action]]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment