38 lines
1.4 KiB
Nim
38 lines
1.4 KiB
Nim
import std/[httpclient, json, sequtils, strutils, sugar]
|
|
import strformat
|
|
|
|
const API_KEY = "04F93AE4667F331EFD05EF31A2FF7DF6"
|
|
|
|
proc get_predictions*(client: HTTPClient, stop: string, route: string): JSONNode =
|
|
let url = &"https://api.actransit.org/transit/actrealtime/prediction?token={API_KEY}&stpid={stop}&rt={route}"
|
|
let response = client.getContent(url)
|
|
let prediction = parseJson(response)
|
|
prediction["bustime-response"]
|
|
|
|
proc format_predictions*(prd: JSONNode): string =
|
|
if not prd.hasKey("prd"):
|
|
"No prediction available"
|
|
else:
|
|
prd["prd"].getElems.map(time => time["prdctdn"].getStr).join(", ")
|
|
|
|
proc home_predictions*(client: HTTPClient): string =
|
|
const home_nl = "51067"
|
|
const home_12 = "58995"
|
|
const home_29 = "56557"
|
|
|
|
let nl_predictions = client.get_predictions(home_nl, "NL").format_predictions
|
|
let twelve_predictions = client.get_predictions(home_12, "12").format_predictions
|
|
let twenty_nine_predictions = client.get_predictions(home_29, "29").format_predictions
|
|
|
|
&"Home\nNL: {nl_predictions}\n12: {twelve_predictions}\n29: {twenty_nine_predictions}"
|
|
|
|
proc office_predictions*(client: HTTPClient): string =
|
|
let client = newHttpClient()
|
|
const office_nl = "56565"
|
|
const office_12 = "57111"
|
|
|
|
let nl_predictions = client.get_predictions(office_nl, "NL").format_predictions
|
|
let twelve_predictions = client.get_predictions(office_12, "12").format_predictions
|
|
|
|
&"Oakland Office\nNL: {nl_predictions}\n12: {twelve_predictions}"
|