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/calculaturr
import util/brightnurrs
import util/tideurrl
proc dispatch*(cfg: Config) =
case cfg.run
@ -48,5 +49,7 @@ proc dispatch*(cfg: Config) =
calculaturr.go()
of Brightnurrs:
brightnurrs.go()
of Tideurrl:
tideurrl.go()
else:
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,
Screenshurrt,
Calculaturr,
Brightnurrs
Brightnurrs,
Tideurrl

View file

@ -7,6 +7,7 @@ import model/pwgen
import model/volume
import model/brightness
import model/screenshot
import model/tides
proc parseArgs*() =
let params = commandLineParams()
@ -47,6 +48,8 @@ proc parseArgs*() =
myConfig.run = Calculaturr
of "brightnurrs", "brightness", "bright":
myConfig.run = Brightnurrs
of "tideurrl", "tides":
myConfig.run = Tideurrl
else:
echo p.help
quit(1)
@ -154,3 +157,23 @@ proc parseScreenshotArgs*(): Screenshot =
stderr.writeLine getCurrentExceptionMsg()
quit(1)
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))"
import ../../globurrl
import std/[re,httpclient,times,osproc,sequtils]
# TODO:
# Pass location in as variable
import re
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 = "🌊 "
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:
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] =
proc getTideData(mytides: TideList, 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)
var client = newHttpClient(timeout = 10000)
var link = replace(mytides.url,re"\%LOC",mytides.location)
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 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)
var tide: Tide
var column = 0
@ -50,57 +50,61 @@ proc getTideData(get_tomorrow: bool = false): seq[Tide] =
let l = len(time) - 1
if time == ">High" or time == ">Low":
tide = Tide()
tide.State = time[1..l]
tide.state = time[1..l]
column = 1
continue
elif column == 1:
tide.Time = time[1..l]
tide.time = time[1..l]
column = 2
continue
elif column == 2:
tide.Height = time[1..l]
tide.height = time[1..l]
tides.add(tide)
column = 0
continue
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
proc getDesign(tides: seq[Tide]): Info =
proc getDesign(tl: TideList): Info =
var my_tides: seq[string] = @[]
my_tides.add(tl.location)
let tides = tl.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)
var data = newInfo("Tideurrl")
data.border = black
data.args = my_tides
return data
proc getTides*(get_tomorrow: bool = false) =
var mytides = TideList()
mytides.Tides = getTideData(get_tomorrow)
mytides.Tides = sortTides(mytides.Tides, get_tomorrow)
if len(mytides.Tides) == 0:
getTides(true)
proc getTides*(mytides: var TideList, get_tomorrow: bool = false) =
mytides.tides = mytides.getTideData(get_tomorrow)
mytides.tides = sortTides(mytides.tides, get_tomorrow)
if len(mytides.tides) == 0:
return
let data = getDesign(mytides.Tides)
let data = getDesign(mytides)
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)
getTides(mytides,true)
elif output == "back":
getTides()
elif output == "---":
getTides(mytides)
elif output == "---" or output == "":
return
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)
else:
mytides.location = output
getTides(mytides)
proc main() =
getTides()
if isMainModule:
main()
proc go*() =
var mytides = parseTideurrlArgs()
getTides(mytides)