tides changes
This commit is contained in:
parent
ccf397ba00
commit
d7128e0540
3 changed files with 107 additions and 107 deletions
107
WIP/tides.nim
107
WIP/tides.nim
|
@ -1,107 +0,0 @@
|
|||
#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"
|
||||
const icon: string = "🌊"
|
||||
|
||||
type
|
||||
Tide = ref object
|
||||
State: string
|
||||
Time: string
|
||||
Height: string
|
||||
Tomorrow: bool
|
||||
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 or tide.Tomorrow:
|
||||
reltides.add(tide)
|
||||
count += 1
|
||||
if count >= 2:
|
||||
break
|
||||
return reltides
|
||||
|
||||
|
||||
proc getTideData(gettomorrow: bool = false): seq[Tide] =
|
||||
var tides: 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")
|
||||
try:
|
||||
let data = client.getContent(link)
|
||||
echo "Data : " & data
|
||||
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)
|
||||
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 = "-1"
|
||||
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:
|
||||
let args: seq[string] = @[]
|
||||
outputJSON(data,args)
|
||||
last_data = $data
|
||||
if stoploop:
|
||||
break
|
||||
sleep(10000)
|
||||
|
||||
proc main() =
|
||||
getTides()
|
||||
|
||||
if isMainModule:
|
||||
main()
|
1
base.nim
1
base.nim
|
@ -14,6 +14,7 @@ type
|
|||
color*: string
|
||||
html_text*: string
|
||||
short_text*: string
|
||||
args*: seq[string]
|
||||
Dmenu = object
|
||||
command: string
|
||||
bottom: string
|
||||
|
|
106
tides.nim
Normal file
106
tides.nim
Normal file
|
@ -0,0 +1,106 @@
|
|||
#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 base
|
||||
import std/[re,httpclient,os,times,osproc,sequtils]
|
||||
|
||||
# TODO:
|
||||
# Pass location in as variable
|
||||
|
||||
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
|
||||
Tomorrow: bool
|
||||
TideList = ref object
|
||||
Tides: seq[Tide]
|
||||
LastUpdated: DateTime
|
||||
|
||||
proc sortTides(tides: seq[Tide], is_tomorrow: bool = false): seq[Tide] =
|
||||
let timenow = now()
|
||||
var reltides: seq[Tide]
|
||||
for tide in tides:
|
||||
if timenow.format("HH:MM") <= tide.Time and not is_tomorrow:
|
||||
reltides.add(tide)
|
||||
elif is_tomorrow:
|
||||
reltides.add(tide)
|
||||
return reltides
|
||||
|
||||
|
||||
proc getTideData(get_tomorrow: bool = false): seq[Tide] =
|
||||
var tides: 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 get_tomorrow:
|
||||
let tomdate = now() + initTimeInterval(days = 1)
|
||||
link &= "-" & tomdate.format("yyyyMMdd")
|
||||
try:
|
||||
# Remember to compile with -d:ssl else this won't work
|
||||
let data = client.getContent(link)
|
||||
let times = findAll(data,fnd)
|
||||
var tide: Tide
|
||||
var column = 0
|
||||
for idx,time in times:
|
||||
if idx == 12:
|
||||
break
|
||||
let l = len(time) - 1
|
||||
if time == ">High" or time == ">Low":
|
||||
tide = Tide()
|
||||
tide.State = time[1..l]
|
||||
column = 1
|
||||
continue
|
||||
elif column == 1:
|
||||
tide.Time = time[1..l]
|
||||
column = 2
|
||||
continue
|
||||
elif column == 2:
|
||||
tide.Height = time[1..l]
|
||||
tides.add(tide)
|
||||
column = 0
|
||||
continue
|
||||
except:
|
||||
echo "Unable to get Tide Data : " & getCurrentExceptionMsg()
|
||||
return tides
|
||||
|
||||
proc getDesign(tides: seq[Tide]): Info =
|
||||
var my_tides: seq[string] = @[]
|
||||
for tide in tides:
|
||||
let str = icon & tide.State[0] & " " & tide.Time & " " & tide.Height
|
||||
my_tides.add(str)
|
||||
var data = newInfo("Tides")
|
||||
data.border = black
|
||||
data.args = my_tides
|
||||
return data
|
||||
|
||||
proc getTides*(get_tomorrow: bool = false) {.gcsafe.}=
|
||||
var mytides = TideList()
|
||||
mytides.Tides = getTideData(get_tomorrow)
|
||||
mytides.Tides = sortTides(mytides.Tides, get_tomorrow)
|
||||
if len(mytides.Tides) == 0:
|
||||
getTides(true)
|
||||
return
|
||||
let data = getDesign(mytides.Tides)
|
||||
var opt_tomorrow = "tomorrow"
|
||||
if get_tomorrow:
|
||||
opt_tomorrow = "back"
|
||||
let args = concat(data.args,@["---",opt_tomorrow])
|
||||
let output = outputData(data,args)
|
||||
if output == "tomorrow":
|
||||
getTides(true)
|
||||
elif output == "back":
|
||||
getTides()
|
||||
elif output == "---":
|
||||
return
|
||||
elif output in args:
|
||||
let url = replace(url,re"\%LOC",loc)
|
||||
discard execCmd("xdg-open " & url)
|
||||
|
||||
proc main() =
|
||||
getTides()
|
||||
|
||||
if isMainModule:
|
||||
main()
|
Loading…
Reference in a new issue