From 976f106d43964d092804be931cede888f7861093 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Sun, 27 Feb 2022 14:57:40 +0000 Subject: [PATCH] added tide times --- i3bar_base.nim | 9 ++++ i3bar_brightness.nim | 8 ++-- i3bar_tides.nim | 111 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 i3bar_tides.nim diff --git a/i3bar_base.nim b/i3bar_base.nim index 4715e47..d7e5a22 100644 --- a/i3bar_base.nim +++ b/i3bar_base.nim @@ -24,6 +24,7 @@ const foreground* = "#dfdfdf" const foregroundalt* = "#777" const foregroundalt2* = "#ccc" const black* = "#000000" +const white* = "#FFFFFF" const yellow* = "#ffb52a" const red* = "#e60053" const purple* = "#9f78e1" @@ -38,6 +39,14 @@ const primary* = yellow const secondary* = red const alert* = "#bd2c40" +proc newi3BarData*(): i3BarData = + return i3BarData( + full_text: "", + short_text: "", + color: foreground, + border: white, + background: black + ) proc debugLog*(str: string) = let f = open("/tmp/debug.txt",fmAppend) defer: f.close() diff --git a/i3bar_brightness.nim b/i3bar_brightness.nim index 936d3b7..ba0d8cb 100644 --- a/i3bar_brightness.nim +++ b/i3bar_brightness.nim @@ -38,11 +38,9 @@ proc get_brightness(run_once: bool = false) = let pcnt = (current/limit)*100 if pcnt != last_pcnt: let text = getDesign(pcnt) - let data = i3barData( - full_text: text, - color: foreground, - border: yellow - ) + var data = newi3BarData() + data.full_text = text + data.border = yellow outputJSON(data) if run_once: break diff --git a/i3bar_tides.nim b/i3bar_tides.nim new file mode 100644 index 0000000..f258ba8 --- /dev/null +++ b/i3bar_tides.nim @@ -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 = "" & text & "" + 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()