Compare commits
No commits in common. "92d1eea678f562ada8f89ce11dc34f47f2aef09a" and "e6a72669baaa45ede95e63b082830baac891852c" have entirely different histories.
92d1eea678
...
e6a72669ba
5 changed files with 44 additions and 97 deletions
|
@ -15,7 +15,6 @@ import util/temperaturr
|
|||
import util/screenshurrt
|
||||
import util/calculaturr
|
||||
import util/brightnurrs
|
||||
import util/tideurrl
|
||||
|
||||
proc dispatch*(cfg: Config) =
|
||||
case cfg.run
|
||||
|
@ -49,7 +48,5 @@ proc dispatch*(cfg: Config) =
|
|||
calculaturr.go()
|
||||
of Brightnurrs:
|
||||
brightnurrs.go()
|
||||
of Tideurrl:
|
||||
tideurrl.go()
|
||||
else:
|
||||
echo "No valid run command given"
|
||||
|
|
|
@ -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
|
|
@ -16,5 +16,4 @@ type
|
|||
Temperaturr,
|
||||
Screenshurrt,
|
||||
Calculaturr,
|
||||
Brightnurrs,
|
||||
Tideurrl
|
||||
Brightnurrs
|
||||
|
|
|
@ -7,7 +7,6 @@ import model/pwgen
|
|||
import model/volume
|
||||
import model/brightness
|
||||
import model/screenshot
|
||||
import model/tides
|
||||
|
||||
proc parseArgs*() =
|
||||
let params = commandLineParams()
|
||||
|
@ -48,8 +47,6 @@ proc parseArgs*() =
|
|||
myConfig.run = Calculaturr
|
||||
of "brightnurrs", "brightness", "bright":
|
||||
myConfig.run = Brightnurrs
|
||||
of "tideurrl", "tides":
|
||||
myConfig.run = Tideurrl
|
||||
else:
|
||||
echo p.help
|
||||
quit(1)
|
||||
|
@ -157,23 +154,3 @@ 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
|
||||
|
|
|
@ -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]
|
||||
|
||||
import re
|
||||
import httpclient
|
||||
import times
|
||||
import osproc
|
||||
import sequtils
|
||||
|
||||
import ../common
|
||||
import ../parser
|
||||
import ../output
|
||||
import ../model/tides
|
||||
# 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:
|
||||
if timenow.format("HH:MM") <= tide.Time and not is_tomorrow:
|
||||
reltides.add(tide)
|
||||
elif is_tomorrow:
|
||||
reltides.add(tide)
|
||||
return reltides
|
||||
|
||||
|
||||
proc getTideData(mytides: TideList, get_tomorrow: bool = false): seq[Tide] =
|
||||
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(timeout = 10000)
|
||||
var link = replace(mytides.url,re"\%LOC",mytides.location)
|
||||
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 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 data = client.getContent(link)
|
||||
let times = findAll(data,fnd)
|
||||
var tide: Tide
|
||||
var column = 0
|
||||
|
@ -50,61 +50,57 @@ proc getTideData(mytides: TideList, 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:
|
||||
var data = newInfo("Tideurrl")
|
||||
data.full_text = "Unable to get Tide Data : " & getCurrentExceptionMsg()
|
||||
discard outputData(data)
|
||||
echo "Unable to get Tide Data : " & getCurrentExceptionMsg()
|
||||
return tides
|
||||
|
||||
proc getDesign(tl: TideList): Info =
|
||||
proc getDesign(tides: seq[Tide]): 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*(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:
|
||||
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)
|
||||
return
|
||||
let data = getDesign(mytides)
|
||||
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(mytides,true)
|
||||
getTides(true)
|
||||
elif output == "back":
|
||||
getTides(mytides)
|
||||
elif output == "---" or output == "":
|
||||
getTides()
|
||||
elif output == "---":
|
||||
return
|
||||
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)
|
||||
else:
|
||||
mytides.location = output
|
||||
getTides(mytides)
|
||||
|
||||
proc main() =
|
||||
getTides()
|
||||
|
||||
proc go*() =
|
||||
var mytides = parseTideurrlArgs()
|
||||
getTides(mytides)
|
||||
|
||||
if isMainModule:
|
||||
main()
|
||||
|
|
Loading…
Reference in a new issue