bart-tile/urbit/app/bartinfo.hoon

252 lines
7.4 KiB
Plaintext
Raw Normal View History

2020-05-10 04:24:46 -07:00
/+ *server, default-agent
/= index
/^ octs
/; as-octs:mimes:html
2020-05-12 00:10:53 -07:00
/: /===/app/bartinfo/index
2020-05-10 04:24:46 -07:00
/| /html/
/~ ~
==
/= tile-js
/^ octs
/; as-octs:mimes:html
2020-05-12 00:10:53 -07:00
/: /===/app/bartinfo/js/tile
2020-05-10 04:24:46 -07:00
/| /js/
/~ ~
==
/= script
/^ octs
/; as-octs:mimes:html
2020-05-12 00:10:53 -07:00
/: /===/app/bartinfo/js/index
2020-05-10 04:24:46 -07:00
/| /js/
/~ ~
==
/= style
/^ octs
/; as-octs:mimes:html
2020-05-12 00:10:53 -07:00
/: /===/app/bartinfo/css/index
2020-05-10 04:24:46 -07:00
/| /css/
/~ ~
==
2020-05-12 00:10:53 -07:00
/= bartinfo-png
2020-05-10 04:24:46 -07:00
/^ (map knot @)
2020-05-12 00:10:53 -07:00
/: /===/app/bartinfo/img /_ /png/
2020-05-10 04:24:46 -07:00
::
|%
+$ card card:agent:gall
--
^- agent:gall
=<
|_ bol=bowl:gall
+* this .
2020-05-12 00:10:53 -07:00
bartinfo-core +>
cc ~(. bartinfo-core bol)
2020-05-10 04:24:46 -07:00
def ~(. (default-agent this %|) bol)
::
++ on-init
^- (quip card _this)
2020-05-12 00:10:53 -07:00
=/ launcha [%launch-action !>([%add %bartinfo / '/~bartinfo/js/tile.js'])]
2020-05-10 04:24:46 -07:00
:_ this
2020-05-12 00:10:53 -07:00
:~ [%pass / %arvo %e %connect [~ /'~bartinfo'] %bartinfo]
[%pass /bartinfo %agent [our.bol %launch] %poke launcha]
2020-05-10 04:24:46 -07:00
==
++ on-poke
|= [=mark =vase]
^- (quip card _this)
?> (team:title our.bol src.bol)
?+ mark (on-poke:def mark vase)
%handle-http-request
=+ !<([eyre-id=@ta =inbound-request:eyre] vase)
:_ this
%+ give-simple-payload:app eyre-id
%+ require-authorization:app inbound-request
poke-handle-http-request:cc
2020-06-03 03:22:09 -07:00
%json
=+ !<(jon=json vase)
:_ this
(poke-handle-json:cc jon)
2020-05-10 04:24:46 -07:00
::
==
::
++ on-watch
|= =path
2020-05-24 00:56:31 -07:00
^- (quip card _this)
~& "on-watch path: {<path>}"
2020-05-31 04:28:47 -07:00
?: ?=([%bartstations *] path)
2020-05-24 00:56:31 -07:00
=/ bart-station-request
=/ out *outbound-config:iris
=/ req bart-api-request-stations:cc
[%pass /bartstationrequest %arvo %i %request req out]
2020-05-24 02:04:40 -07:00
[~[bart-station-request] this]
2020-06-01 00:24:08 -07:00
?: ?=([%elevators *] path)
=/ elevator-status-request
=/ out *outbound-config:iris
=/ req bart-api-elevator-status:cc
[%pass /elevators %arvo %i %request req out]
[~[elevator-status-request] this]
2020-06-03 03:30:21 -07:00
?: ?=([%routes *] path)
[[~] this]
2020-05-10 04:24:46 -07:00
?: ?=([%http-response *] path)
`this
?. =(/ path)
(on-watch:def path)
[[%give %fact ~ %json !>(*json)]~ this]
::
++ on-agent on-agent:def
::
++ on-arvo
|= [=wire =sign-arvo]
^- (quip card _this)
2020-05-31 02:37:43 -07:00
~& "The on-arvo wire: {<wire>}"
2020-05-23 06:10:11 -07:00
?: ?=(%http-response +<.sign-arvo)
2020-05-24 00:56:31 -07:00
=/ http-moves=(list card)
2020-05-24 02:02:12 -07:00
?+ wire ~
[%bartstationrequest *]
=/ value=json (parse-request-stations-response:cc client-response.sign-arvo)
2020-05-31 04:28:47 -07:00
?> ?=(%o -.value)
=/ update=json (pairs:enjs:format [update+o+p.value ~])
[%give %fact ~[/bartstations] %json !>(update)]~
2020-06-04 03:57:18 -07:00
::
2020-06-01 00:24:08 -07:00
[%elevators *]
=/ value=json (parse-elevator-status-response:cc client-response.sign-arvo)
?> ?=(%o -.value)
=/ update=json (pairs:enjs:format [update+o+p.value ~])
[%give %fact ~[/elevators] %json !>(update)]~
2020-06-04 03:57:18 -07:00
::
[%routeplan *]
=/ value=json (parse-routeplan-response:cc client-response.sign-arvo)
?> ?=(%o -.value)
=/ update=json (pairs:enjs:format [update+o+p.value ~])
[%give %fact ~[/routes] %json !>(update)]~
2020-05-24 02:02:12 -07:00
==
2020-05-24 00:56:31 -07:00
[http-moves this]
2020-05-10 04:24:46 -07:00
?. ?=(%bound +<.sign-arvo)
(on-arvo:def wire sign-arvo)
[~ this]
::
++ on-save on-save:def
++ on-load on-load:def
++ on-leave on-leave:def
++ on-peek on-peek:def
++ on-fail on-fail:def
--
::
::
|_ bol=bowl:gall
::
2020-05-21 02:49:43 -07:00
:: request to http://api.bart.gov/api/stn.aspx?cmd=stns&key=Q5RQ-PUEB-999T-DWEI&json=y
:: get .root | .stations | .station for list of stations
++ bart-api-key "Q5RQ-PUEB-999T-DWEI"
++ bart-api-url-base "http://api.bart.gov/api"
2020-06-01 01:07:07 -07:00
++ with-json-handler
|= [response=client-response:iris jsonhandler=$-(json json)]
^- json
=, format
?. ?=(%finished -.response)
%- pairs:enjs [fulltext+s+'bart response error' ~]
=/ data=(unit mime-data:iris) full-file.response
?~ data %- pairs:enjs ~
=/ ujon=(unit json) (de-json:html q.data.u.data)
?~ ujon %- pairs:enjs ~
?> ?=(%o -.u.ujon)
=/ parsed-json=json u.ujon
(jsonhandler parsed-json)
2020-05-24 00:56:31 -07:00
++ bart-api-request-stations
^- request:http
=/ url (crip "{bart-api-url-base}/stn.aspx?cmd=stns&key={bart-api-key}&json=y")
=/ headers [['Accept' 'application/json']]~
[%'GET' url headers *(unit octs)]
::
2020-05-24 02:02:12 -07:00
++ parse-request-stations-response
|= response=client-response:iris
^- json
2020-05-26 02:21:13 -07:00
=, format
2020-06-05 03:27:36 -07:00
=/ handler |= jon=json
=/ root ((ot:dejs ~[['root' same]]) jon)
=/ stations ((ot:dejs ~[['stations' same]]) root)
=/ station ((ot:dejs ~[['station' (ar:dejs same)]]) stations)
=/ abbr-and-name %- turn :- station |= item=json
2020-06-01 01:07:07 -07:00
^- json
2020-06-05 03:27:36 -07:00
=/ [name=tape abbr=tape]
((ot:dejs ~[['name' sa:dejs] ['abbr' sa:dejs]]) item)
(pairs:enjs ~[name+(tape:enjs name) abbr+(tape:enjs abbr)])
2020-06-01 01:07:07 -07:00
(pairs:enjs [[%stations %a abbr-and-name] ~])
(with-json-handler response handler)
2020-05-24 02:02:12 -07:00
::
2020-06-01 00:24:08 -07:00
++ bart-api-elevator-status
^- request:http
2020-06-01 00:36:08 -07:00
=/ url (crip "{bart-api-url-base}/bsa.aspx?cmd=elev&key={bart-api-key}&json=y")
2020-06-01 00:24:08 -07:00
=/ headers [['Accept' 'application/json']]~
[%'GET' url headers *(unit octs)]
++ parse-elevator-status-response
|= response=client-response:iris
^- json
=, format
2020-06-01 01:21:31 -07:00
=/ handler |= jon=json
2020-06-05 03:27:36 -07:00
=/ root=json ((ot:dejs ~[['root' same]]) jon)
2020-06-05 03:35:12 -07:00
=/ bsa=(list json) ((ot:dejs ~[['bsa' (ar:dejs same)]]) root)
(pairs:enjs [[%elevators %a bsa] ~])
2020-06-01 01:21:31 -07:00
(with-json-handler response handler)
2020-06-01 00:24:08 -07:00
::
2020-06-03 04:34:25 -07:00
++ bart-api-routeplan
2020-06-04 03:57:18 -07:00
|= [from=tape to=tape hour=@ min=@ ispm=?]
2020-06-03 04:34:25 -07:00
^- request:http
:: http://api.bart.gov/api/sched.aspx?cmd=depart&orig=ASHB&dest=CIVC&date=now
:: TODO cmd can be 'depart' or 'arrive', also 'fare'
2020-06-04 03:57:18 -07:00
=/ meridian ?:(ispm "pm" "am")
=/ time "{<hour>}:{<min>}{meridian}"
=/ url (crip "{bart-api-url-base}/sched.aspx?cmd=depart&orig={from}&dest={to}&time={time}&key={bart-api-key}&json=y")
2020-06-05 03:27:36 -07:00
~& "Making BART API request to {<url>}"
2020-06-03 04:34:25 -07:00
=/ headers [['Accept' 'application/json']]~
[%'GET' url headers *(unit octs)]
2020-06-04 03:57:18 -07:00
++ parse-routeplan-response
|= response=client-response:iris
^- json
2020-06-05 02:42:25 -07:00
=, format
=/ handler
|= jon=json
=/ root=json ((ot:dejs [['root' same] ~]) jon)
2020-06-05 03:35:12 -07:00
=/ schedule=json ((ot:dejs [['schedule' same] ~]) root)
(pairs:enjs ~[[%routes schedule]])
2020-06-05 02:42:25 -07:00
(with-json-handler response handler)
2020-06-03 03:22:09 -07:00
++ poke-handle-json
2020-06-03 03:30:21 -07:00
|= jon=json
2020-06-03 03:22:09 -07:00
^- (list card)
~& jon
2020-06-03 04:34:25 -07:00
=, format
?. ?=(%o -.jon)
[~]
2020-06-04 03:57:18 -07:00
=/ [hour=@ min=@ ispm=? from-station=tape to-station=tape]
%.
jon
%: ot:dejs
['hour' ni:dejs]
['min' ni:dejs]
['isPM' bo:dejs]
['from' sa:dejs]
['to' sa:dejs]
~
==
=/ req (bart-api-routeplan from-station to-station hour min ispm)
2020-06-03 04:34:25 -07:00
=/ out *outbound-config:iris
2020-06-04 03:57:18 -07:00
[[%pass /routeplan %arvo %i %request req out] ~]
2020-06-03 03:30:21 -07:00
::
2020-05-10 04:24:46 -07:00
++ poke-handle-http-request
|= =inbound-request:eyre
^- simple-payload:http
=+ url=(parse-request-line url.request.inbound-request)
?+ site.url not-found:gen
2020-05-12 00:10:53 -07:00
[%'~bartinfo' %css %index ~] (css-response:gen style)
[%'~bartinfo' %js %tile ~] (js-response:gen tile-js)
[%'~bartinfo' %js %index ~] (js-response:gen script)
2020-05-10 04:24:46 -07:00
::
2020-05-12 00:10:53 -07:00
[%'~bartinfo' %img @t *]
2020-05-10 04:24:46 -07:00
=/ name=@t i.t.t.site.url
2020-05-12 00:10:53 -07:00
=/ img (~(get by bartinfo-png) name)
2020-05-10 04:24:46 -07:00
?~ img
not-found:gen
(png-response:gen (as-octs:mimes:html u.img))
::
2020-05-12 00:10:53 -07:00
[%'~bartinfo' *] (html-response:gen index)
2020-05-10 04:24:46 -07:00
==
--