wmtools/WIP/tides.nim

108 lines
2.7 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/times
const url* = "https://www.tidetimes.org.uk/%LOC-tide-times"
const loc* = "exmouth-dock"
2022-02-27 15:57:40 +01:00
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)
2022-04-28 21:58:40 +02:00
echo "Data : " & data
2022-03-07 10:37:31 +01:00
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.}=
2022-02-27 15:57:40 +01:00
var mytides = TideList()
2022-04-28 21:58:40 +02:00
var last_data = "-1"
2022-02-27 15:57:40 +01:00
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:
2022-04-28 21:58:40 +02:00
let args: seq[string] = @[]
outputJSON(data,args)
2022-02-27 15:57:40 +01:00
last_data = $data
if stoploop:
break
2022-02-27 15:57:40 +01:00
sleep(10000)
proc main() =
2022-04-28 21:58:40 +02:00
getTides()
2022-02-27 15:57:40 +01:00
2022-04-28 21:58:40 +02:00
if isMainModule:
main()