added tide times

This commit is contained in:
Paul Wilde 2022-02-27 14:57:40 +00:00
parent e69e813328
commit 976f106d43
3 changed files with 123 additions and 5 deletions

View file

@ -24,6 +24,7 @@ const foreground* = "#dfdfdf"
const foregroundalt* = "#777" const foregroundalt* = "#777"
const foregroundalt2* = "#ccc" const foregroundalt2* = "#ccc"
const black* = "#000000" const black* = "#000000"
const white* = "#FFFFFF"
const yellow* = "#ffb52a" const yellow* = "#ffb52a"
const red* = "#e60053" const red* = "#e60053"
const purple* = "#9f78e1" const purple* = "#9f78e1"
@ -38,6 +39,14 @@ const primary* = yellow
const secondary* = red const secondary* = red
const alert* = "#bd2c40" const alert* = "#bd2c40"
proc newi3BarData*(): i3BarData =
return i3BarData(
full_text: "",
short_text: "",
color: foreground,
border: white,
background: black
)
proc debugLog*(str: string) = proc debugLog*(str: string) =
let f = open("/tmp/debug.txt",fmAppend) let f = open("/tmp/debug.txt",fmAppend)
defer: f.close() defer: f.close()

View file

@ -38,11 +38,9 @@ proc get_brightness(run_once: bool = false) =
let pcnt = (current/limit)*100 let pcnt = (current/limit)*100
if pcnt != last_pcnt: if pcnt != last_pcnt:
let text = getDesign(pcnt) let text = getDesign(pcnt)
let data = i3barData( var data = newi3BarData()
full_text: text, data.full_text = text
color: foreground, data.border = yellow
border: yellow
)
outputJSON(data) outputJSON(data)
if run_once: if run_once:
break break

111
i3bar_tides.nim Normal file
View file

@ -0,0 +1,111 @@
#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
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:
if timenow.format("HH:MM") <= tide.Time:
reltides.add(tide)
count += 1
if count >= 2:
break
return reltides
proc getTideData(gettomorrow: bool = false): seq[Tide] =
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)
link &= "-" & tomdate.format("yyyymmdd")
let data = client.getContent(link)
let times = findAll(data,fnd)
var tides: seq[Tide]
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]
tides.add(tide)
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()