Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created December 3, 2022 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturaz/dbf86878fc13bb3e484dfded606be3f3 to your computer and use it in GitHub Desktop.
Save arturaz/dbf86878fc13bb3e484dfded606be3f3 to your computer and use it in GitHub Desktop.
Parser for plutus transactions PDF
val src = """
Card Transaction
CRV*UAB ABUNDO, Vilnius, LT CD 0108 2022/01/12, 18:26 - €1.5
"""
import java.time.LocalDateTime
val Re = """^(.*?)(\d{4})/(\d\d)/(\d\d),\s*(\d\d):(\d\d)\s*(-)?\s*€([\d\\.]+)$""".r
val amounts = src.linesIterator.sliding(2).withPadding("").flatMap { case Seq(prevLine, line) =>
if (
line.contains("Card Deposit")
|| line.contains("Load a card with deposit")
) None
else Re.findFirstMatchIn(line).map { m =>
val description = m.group(1)
val fullDescription = if (description == "") prevLine else description
val year = m.group(2)
val day = m.group(3)
val month = m.group(4)
val hour = m.group(5)
val minute = m.group(6)
val at = LocalDateTime.of(year.toInt, month.toInt, day.toInt, hour.toInt, minute.toInt)
val sign = if (m.group(7) == null) 1 else -1
val amount = m.group(8).toDouble
(at, fullDescription, sign * amount)
}
}.toVector.sortBy(_._1)
amounts.foldLeft(0d) { case (current, (at, description, amount)) =>
val next = current + amount
println(s"${at.toLocalDate} ${at.toLocalTime} - $description")
println(f"$current%.2f\t+\t$amount\t=\t$next%.2f")
println()
next
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment