From e80543d576518705339a39376af9b51765f4ceee Mon Sep 17 00:00:00 2001 From: Zoe Moore Date: Fri, 24 Jun 2022 15:24:07 -0700 Subject: [PATCH] initial commit --- .gitignore | 1 + bt.nim | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 .gitignore create mode 100644 bt.nim diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc5962c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bt diff --git a/bt.nim b/bt.nim new file mode 100644 index 0000000..80fa780 --- /dev/null +++ b/bt.nim @@ -0,0 +1,49 @@ +import std/[json, httpclient, sequtils] +import strformat +import os +import terminal + +let client = newHttpClient() + +proc getDepartures(station: string): JSONNode = + let response = client.get(&"http://api.bart.gov/api/etd.aspx?cmd=etd&orig={station}&key=MW9S-E7SL-26DU-VV8V&json=y") + parseJson(response.body) + +proc formatEstimateMinutes(minutes: string): string = + case minutes: + of "Leaving": + "leaving" + of "1": + "1 min" + else: + &"{minutes} mins" + +proc formatDepartures(etd: JSONNode): string = + let name = etd["root"]["station"][0]["name"].getStr + let lines = etd["root"]["station"][0]["etd"] + + styledEcho styleBright, name + for line in lines: + let dest = line["destination"].getStr + let estimate = formatEstimateMinutes(line["estimate"][0]["minutes"].getStr) + + echo &"{dest}\t\t{estimate}" + +proc usage() = + echo """i +bt - bart times cli + +usage: bt station [direction n or s] +""" + + +proc main = + let params = commandLineParams() + + if len(params) == 0: + usage() + elif len(params) == 1: + let station = params[0] + echo formatDepartures(getDepartures(station)) + +main()