initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
bt
|
||||||
49
bt.nim
Normal file
49
bt.nim
Normal 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()
|
||||||
Reference in New Issue
Block a user