Compare commits

..

No commits in common. "92d1eea678f562ada8f89ce11dc34f47f2aef09a" and "e6a72669baaa45ede95e63b082830baac891852c" have entirely different histories.

5 changed files with 44 additions and 97 deletions

View file

@ -15,7 +15,6 @@ 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
@ -49,7 +48,5 @@ 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"

View file

@ -1,22 +0,0 @@
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,5 +16,4 @@ type
Temperaturr, Temperaturr,
Screenshurrt, Screenshurrt,
Calculaturr, Calculaturr,
Brightnurrs, Brightnurrs
Tideurrl

View file

@ -7,7 +7,6 @@ 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()
@ -48,8 +47,6 @@ 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)
@ -157,23 +154,3 @@ 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]
import re # TODO:
import httpclient # Pass location in as variable
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(mytides: TideList, get_tomorrow: bool = false): seq[Tide] = proc getTideData(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(timeout = 10000) var client = newHttpClient()
var link = replace(mytides.url,re"\%LOC",mytides.location) var link = replace(url,re"\%LOC",loc)
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 resp = client.request(link) let data = client.getContent(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,61 +50,57 @@ proc getTideData(mytides: TideList, 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:
var data = newInfo("Tideurrl") echo "Unable to get Tide Data : " & getCurrentExceptionMsg()
data.full_text = "Unable to get Tide Data : " & getCurrentExceptionMsg()
discard outputData(data)
return tides return tides
proc getDesign(tl: TideList): Info = proc getDesign(tides: seq[Tide]): 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*(mytides: var TideList, get_tomorrow: bool = false) = proc getTides*(get_tomorrow: bool = false) =
mytides.tides = mytides.getTideData(get_tomorrow) var mytides = TideList()
mytides.tides = sortTides(mytides.tides, get_tomorrow) mytides.Tides = getTideData(get_tomorrow)
if len(mytides.tides) == 0: mytides.Tides = sortTides(mytides.Tides, get_tomorrow)
if len(mytides.Tides) == 0:
getTides(true)
return return
let data = getDesign(mytides) let data = getDesign(mytides.Tides)
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(mytides,true) getTides(true)
elif output == "back": elif output == "back":
getTides(mytides) getTides()
elif output == "---" or output == "": elif output == "---":
return return
elif output in args: elif output in args:
let url = replace(mytides.url,re"\%LOC",mytides.location) let url = replace(url,re"\%LOC",loc)
discard execCmd("xdg-open " & url) discard execCmd("xdg-open " & url)
else:
mytides.location = output
getTides(mytides)
proc main() =
getTides()
proc go*() = if isMainModule:
var mytides = parseTideurrlArgs() main()
getTides(mytides)