wmtools/i3bar_tides.nim

119 lines
2.9 KiB
Nim
Raw Normal View History

2022-02-27 15:57:40 +01:00
#curl https://www.tidetimes.org.uk/exmouth-dock-tide-times-20190101 | grep -E -o ">((High|Low)|([0-9]+:[0-9]+)|([0-9]+\.[0-9]+m))"
import i3bar_base
import std/re
import std/httpclient
import std/os
import std/json
import std/times
import std/threadpool
import std/osproc
const url = "https://www.tidetimes.org.uk/%LOC-tide-times"
const loc = "exmouth-dock"
const icon: string = "🌊"
type
Tide = ref object
State: string
Time: string
Height: string
2022-03-07 10:37:31 +01:00
Tomorrow: bool
2022-02-27 15:57:40 +01:00
TideList = ref object
Tides: seq[Tide]
LastUpdated: DateTime
proc sortTides(tides: seq[Tide]): seq[Tide] =
let timenow = now()
var reltides: seq[Tide]
var count = 0
for tide in tides:
2022-03-07 10:37:31 +01:00
if timenow.format("HH:MM") <= tide.Time or tide.Tomorrow:
2022-02-27 15:57:40 +01:00
reltides.add(tide)
count += 1
if count >= 2:
break
return reltides
proc getTideData(gettomorrow: bool = false): seq[Tide] =
2022-03-07 10:37:31 +01:00
var tides: seq[Tide]
2022-02-27 15:57:40 +01:00
let fnd = re">((High|Low)|([0-9]+:[0-9]+)|([0-9]+\.[0-9]+m))"
var client = newHttpClient()
var link = replace(url,re"\%LOC",loc)
if gettomorrow:
let tomdate = now() + initTimeInterval(days = 1)
2022-03-07 10:37:31 +01:00
link &= "-" & tomdate.format("yyyyMMdd")
try:
let data = client.getContent(link)
let times = findAll(data,fnd)
var tide: Tide
var count: int = 0
for time in times:
let l = len(time) - 1
if time == ">High" or time == ">Low":
tide = Tide()
tide.State = time[1..l]
count = 1
continue
else:
count += 1
if count == 2:
tide.Time = time[1..l]
elif count == 3:
tide.Height = time[1..l]
if gettomorrow:
tide.Tomorrow = true
tides.add(tide)
except:
sleep(5000)
return getTideData(false)
2022-02-27 15:57:40 +01:00
if not gettomorrow:
let tomtides = getTideData(true)
for tide in tomtides:
tides.add(tide)
return tides
proc getDesign(tides: seq[Tide]): i3barData =
var size = ""
if len(tides) > 1:
size = "small"
let text = icon & tides[0].State[0] & " " & tides[0].Time & " " & tides[0].Height & "\r" &
icon & tides[1].State[0] & " " & tides[1].Time & " " & tides[1].Height
let t2 = "<span size=\"" & size & "\">" & text & "</span>"
var data = newi3barData()
data.full_text = t2
data.border = black
return data
proc getTides() {.gcsafe.}=
var mytides = TideList()
var last_data = ""
while true:
if len(mytides.Tides) == 0 or mytides.LastUpdated < now() - initTimeInterval(hours = 1):
mytides.Tides = getTideData()
mytides.LastUpdated = now()
let data = getDesign(sortTides(mytides.Tides))
if $data != last_data:
outputJSON(data)
last_data = $data
sleep(10000)
proc await_click_info() =
while true:
let input = parseInput()
case input.button:
of 1:
let state = execCmd("xdg-open " & replace(url,re"\%LOC",loc))
else:
let no = false
proc main() =
spawn getTides()
spawn await_click_info()
sync()
main()