initial commit

This commit is contained in:
Zoe Moore
2022-06-24 15:24:07 -07:00
commit e80543d576
2 changed files with 50 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
bt

49
bt.nim Normal file
View File

@@ -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()