Compare commits

...

2 commits

Author SHA1 Message Date
92d1eea678 added tideurrl 2023-11-23 20:10:24 +00:00
7f4bf7bb0f added tideurrl 2023-11-23 20:10:04 +00:00
5 changed files with 97 additions and 44 deletions

View file

@ -15,6 +15,7 @@ import util/temperaturr
import util/screenshurrt import util/screenshurrt
import util/calculaturr import util/calculaturr
import util/brightnurrs import util/brightnurrs
import util/tideurrl
proc dispatch*(cfg: Config) = proc dispatch*(cfg: Config) =
case cfg.run case cfg.run
@ -48,5 +49,7 @@ proc dispatch*(cfg: Config) =
calculaturr.go() calculaturr.go()
of Brightnurrs: of Brightnurrs:
brightnurrs.go() brightnurrs.go()
of Tideurrl:
tideurrl.go()
else: else:
echo "No valid run command given" echo "No valid run command given"

22
src/model/tides.nim Normal file
View file

@ -0,0 +1,22 @@
import times
const TIDE_URL* = "https://www.tidetimes.org.uk/%LOC-tide-times"
const DEFAULT_LOC* = "exmouth-dock"
type
Tide* = ref object
state*: string
time*: string
height*: string
tomorrow*: bool
TideList* = ref object
tides*: seq[Tide]
url*: string
last_updated*: DateTime
location*: string
proc newTideList*(): TideList =
var tl = TideList()
tl.url = TIDE_URL
tl.location = DEFAULT_LOC
return tl

View file

@ -16,4 +16,5 @@ type
Temperaturr, Temperaturr,
Screenshurrt, Screenshurrt,
Calculaturr, Calculaturr,
Brightnurrs Brightnurrs,
Tideurrl

View file

@ -7,6 +7,7 @@ import model/pwgen
import model/volume import model/volume
import model/brightness import model/brightness
import model/screenshot import model/screenshot
import model/tides
proc parseArgs*() = proc parseArgs*() =
let params = commandLineParams() let params = commandLineParams()
@ -47,6 +48,8 @@ proc parseArgs*() =
myConfig.run = Calculaturr myConfig.run = Calculaturr
of "brightnurrs", "brightness", "bright": of "brightnurrs", "brightness", "bright":
myConfig.run = Brightnurrs myConfig.run = Brightnurrs
of "tideurrl", "tides":
myConfig.run = Tideurrl
else: else:
echo p.help echo p.help
quit(1) quit(1)
@ -154,3 +157,23 @@ proc parseScreenshotArgs*(): Screenshot =
stderr.writeLine getCurrentExceptionMsg() stderr.writeLine getCurrentExceptionMsg()
quit(1) quit(1)
return ss return ss
proc parseTideurrlArgs*(): TideList =
var t = newTideList()
let params = commandLineParams()
var p = newParser:
help("Args for tideurrl")
arg("tideurrl",help="can only ever be 'tideurrl' as you won't have gotten this far otherwise")
option("-l","--loc",help="location name")
try:
var opts = p.parse(params)
if opts.loc != "":
t.location = opts.loc
except ShortCircuit as err:
if err.flag == "argparse_help":
echo err.help
quit(1)
except UsageError:
stderr.writeLine getCurrentExceptionMsg()
quit(1)
return t

View file

@ -1,46 +1,46 @@
#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))" #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 ../../globurrl
import std/[re,httpclient,times,osproc,sequtils]
# TODO: import re
# Pass location in as variable import httpclient
import times
import osproc
import sequtils
import ../common
import ../parser
import ../output
import ../model/tides
const url* = "https://www.tidetimes.org.uk/%LOC-tide-times"
const loc* = "exmouth-dock"
const icon: string = "🌊 " 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] = proc sortTides(tides: seq[Tide], is_tomorrow: bool = false): seq[Tide] =
let timenow = now() let timenow = now()
var reltides: seq[Tide] var reltides: seq[Tide]
for tide in tides: for tide in tides:
if timenow.format("HH:MM") <= tide.Time and not is_tomorrow: if timenow.format("HH:MM") <= tide.time and not is_tomorrow:
reltides.add(tide) reltides.add(tide)
elif is_tomorrow: elif is_tomorrow:
reltides.add(tide) reltides.add(tide)
return reltides return reltides
proc getTideData(get_tomorrow: bool = false): seq[Tide] = proc getTideData(mytides: TideList, get_tomorrow: bool = false): seq[Tide] =
var tides: seq[Tide] var tides: seq[Tide]
let fnd = re">((High|Low)|([0-9]+:[0-9]+)|([0-9]+\.[0-9]+m))" let fnd = re">((High|Low)|([0-9]+:[0-9]+)|([0-9]+\.[0-9]+m))"
var client = newHttpClient() var client = newHttpClient(timeout = 10000)
var link = replace(url,re"\%LOC",loc) var link = replace(mytides.url,re"\%LOC",mytides.location)
if get_tomorrow: if get_tomorrow:
let tomdate = now() + initTimeInterval(days = 1) let tomdate = now() + initTimeInterval(days = 1)
link &= "-" & tomdate.format("yyyyMMdd") link &= "-" & tomdate.format("yyyyMMdd")
try: try:
# Remember to compile with -d:ssl else this won't work # Remember to compile with -d:ssl else this won't work
let data = client.getContent(link) let resp = client.request(link)
if resp.status != $Http200 or resp.body == "":
var data = newInfo("Tideurrl")
data.full_text = "Error Response: " & resp.status & ":\nBody:" & resp.body
discard outputData(data)
return @[]
let data = resp.body
let times = findAll(data,fnd) let times = findAll(data,fnd)
var tide: Tide var tide: Tide
var column = 0 var column = 0
@ -50,57 +50,61 @@ proc getTideData(get_tomorrow: bool = false): seq[Tide] =
let l = len(time) - 1 let l = len(time) - 1
if time == ">High" or time == ">Low": if time == ">High" or time == ">Low":
tide = Tide() tide = Tide()
tide.State = time[1..l] tide.state = time[1..l]
column = 1 column = 1
continue continue
elif column == 1: elif column == 1:
tide.Time = time[1..l] tide.time = time[1..l]
column = 2 column = 2
continue continue
elif column == 2: elif column == 2:
tide.Height = time[1..l] tide.height = time[1..l]
tides.add(tide) tides.add(tide)
column = 0 column = 0
continue continue
except: except:
echo "Unable to get Tide Data : " & getCurrentExceptionMsg() var data = newInfo("Tideurrl")
data.full_text = "Unable to get Tide Data : " & getCurrentExceptionMsg()
discard outputData(data)
return tides return tides
proc getDesign(tides: seq[Tide]): Info = proc getDesign(tl: TideList): Info =
var my_tides: seq[string] = @[] var my_tides: seq[string] = @[]
my_tides.add(tl.location)
let tides = tl.tides
for tide in tides: for tide in tides:
let str = icon & tide.State[0] & " " & tide.Time & " " & tide.Height let str = icon & tide.state[0] & " " & tide.time & " " & tide.height
my_tides.add(str) my_tides.add(str)
var data = newInfo("Tideurrl") var data = newInfo("Tideurrl")
data.border = black
data.args = my_tides data.args = my_tides
return data return data
proc getTides*(get_tomorrow: bool = false) = proc getTides*(mytides: var TideList, get_tomorrow: bool = false) =
var mytides = TideList() mytides.tides = mytides.getTideData(get_tomorrow)
mytides.Tides = getTideData(get_tomorrow) mytides.tides = sortTides(mytides.tides, get_tomorrow)
mytides.Tides = sortTides(mytides.Tides, get_tomorrow) if len(mytides.tides) == 0:
if len(mytides.Tides) == 0:
getTides(true)
return return
let data = getDesign(mytides.Tides) let data = getDesign(mytides)
var opt_tomorrow = "tomorrow" var opt_tomorrow = "tomorrow"
if get_tomorrow: if get_tomorrow:
opt_tomorrow = "back" opt_tomorrow = "back"
let args = concat(data.args,@["---",opt_tomorrow]) let args = concat(data.args,@["---",opt_tomorrow])
let output = outputData(data,args) let output = outputData(data,args)
if output == "tomorrow": if output == "tomorrow":
getTides(true) getTides(mytides,true)
elif output == "back": elif output == "back":
getTides() getTides(mytides)
elif output == "---": elif output == "---" or output == "":
return return
elif output in args: elif output in args:
let url = replace(url,re"\%LOC",loc) let url = replace(mytides.url,re"\%LOC",mytides.location)
discard execCmd("xdg-open " & url) discard execCmd("xdg-open " & url)
else:
mytides.location = output
getTides(mytides)
proc main() =
getTides()
if isMainModule: proc go*() =
main() var mytides = parseTideurrlArgs()
getTides(mytides)