Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
116 lines (101 sloc) 4.15 KB
Rebol [
file: %rebol-flick-forecast-api.reb
author: "Graham"
date: 14-July-2017
notes: {api documentation obtained from
Also see
1. Download a rebol interpreter from here
2. Rename it to r3 (or r3.exe if using windows )
3. On linux - chmod +x ./r3
4. Download this script, use the raw view
5. Use an editor to change the last 3 values in this header
6. From a shell, run the script like this c:\users\path\to\download\r3 rebol-flick-api.reb
; NB: user details which do change!
password: "your-password-goes-here"
supply_node: "your-supply-node" ; Karori is "1791ac20-df64-4235-8d06-562cc24d22e6"
net-trace off
import <json>
import <webform>
infeasible: "Can not compute!" ; see
infeasible-price: 50'000
; API endpoints
; old api
; get-price:
; current api
get-price: rejoin [ system/script/header/supply_node "&number_of_periods_ahead=1"]
form-vars: make object! [
; API vars - cient_id and secret are the OAUTH credentials for the Android client, and don't change
grant_type: "password"
client_id: "le37iwi3qctbduh39fvnpevt1m2uuvz"
client_secret: "ignwy9ztnst3azswww66y9vd9zt6qnt"
username: system/script/header/username
password: system/script/header/password
jsdate2reboldate: function [
{convert JS zulu date to rebol local date value}
jsdate [string!]
replace jsdate "T" "/"
replace jsdate "Z" ""
d: now/zone + load jsdate
d/zone: now/zone
blank-flick-map: make map! compose [
access_token _
expires_in (now - 1)
id_token _
token_type "bearer"
flick-map: either exists? %flick-map.reb [
load %flick-map.reb
; probe flick-map
Get-flick-map: func [ /local result err][
if error? err: trap [
result: load-json to string! write get-jwt compose [POST (to-webform form-vars)]
result/expires_in: now + to time! result/expires_in
save/all %flick-map.reb result
print "Error obtaining map"
probe err
return blank-flick-map
; net-trace on
display-current-price: does [
print spaced ["Using electricity supply node of" system/script/header/supply_node]
forever [
next-time: now + 0:10:00 ; default of 10 mins if can't get the flick credentials
if flick-map/expires_in < now [
print "Fetching id_token"
flick-map: Get-flick-map
if flick-map/expires_in > now [
; id_token still valid
if error? err: trap [
price: load-json to string! write get-price compose [
GET [Authorization: (join-of "Bearer " flick-map/id_token)]
if (current-price: price/prices/1/price/value) = infeasible-price [current-price: infeasible]
if (next-price: price/prices/2/price/value) = infeasible-price [next-price: infeasible]
print spaced ["Current price at" jsdate2reboldate price/prices/1/start_at current-price price/prices/1/price/unit_code "per" price/prices/1/price/per]
print spaced ["Next price at" next-time: jsdate2reboldate price/prices/2/start_at next-price price/prices/2/price/unit_code "per" price/prices/2/price/per]
; can't get price
probe err
; next-time is when the price changes, so let's wait until then
print spaced ["... sleeping for about" round divide to-integer (difference next-time now) 60 "mins"]
wait/only difference next-time now ;60 * waitperiod