Weather app backend to scrape and parse the Australian BOM xml weather data for Sydney. Output later displayed with Swaybar. Written in Julia ("julialang").
#!/usr/bin/env julia
using FTPClient
ftp = FTP("ftp://ftp.bom.gov.au/")
download(ftp, "anon/gen/fwo/IDN11060.xml", "/home/brett/.IDN11060.xml")
close(ftp)
WEBPAGE = readlines("/home/brett/.IDN11060.xml", keep=true)
# Initialise global variables:
SECTIONLINE = 1
LINE = 1
MAXTEMP = 0
MINTEMP_NEXT = 0
MAXTEMP_NEXT = 0
PRECIP_TODAY = "unknown"
PRECIP_NEXT = "unknown"
PRECIP_RESULT = "unknown"
TEMP_RESULT = "unknown"
# Get to line number where substring "Sydney Metropolitan" is found within a string:
while !occursin("Sydney Metropolitan", WEBPAGE[SECTIONLINE])
global SECTIONLINE = SECTIONLINE + 1
end
println("`Sydney Metropolitan` section begins at line: ", SECTIONLINE)
LINE = SECTIONLINE
while MAXTEMP == 0
global LINE = LINE + 1
if occursin("forecast-period index=\"1\"", WEBPAGE[LINE])
global MAXTEMP = 99
elseif occursin("air_temperature_maximum", WEBPAGE[LINE])
global MAXTEMP = filter(isnumeric, WEBPAGE[LINE])
end
end
println("Today's Max temp: ", MAXTEMP)
LINE = SECTIONLINE
while cmp(PRECIP_TODAY, "unknown") == 0
global LINE = LINE + 1
if occursin("precis", WEBPAGE[LINE])
global PRECIP_TODAY = WEBPAGE[LINE]
end
end
println("Today's precipitation: ", PRECIP_TODAY)
# locate the 'tomorrow' section:
while !occursin("forecast-period index=\"1\"", WEBPAGE[SECTIONLINE])
global SECTIONLINE = SECTIONLINE + 1
end
println("Sydney Metropolitan `tomorrow` section begins at line: ", SECTIONLINE)
# tomorrow's minimum:
LINE = SECTIONLINE
while MINTEMP_NEXT == 0
global LINE = LINE + 1
if occursin("air_temperature_minimum", WEBPAGE[LINE])
global MINTEMP_NEXT = filter(isnumeric, WEBPAGE[LINE])
end
end
println("Tomorrow's Min temp: ", MINTEMP_NEXT)
# tomorrow's maximum
LINE = SECTIONLINE
while MAXTEMP_NEXT == 0
global LINE = LINE + 1
if occursin("air_temperature_maximum", WEBPAGE[LINE])
global MAXTEMP_NEXT = filter(isnumeric, WEBPAGE[LINE])
end
end
println("Tomorrow's Max temp: ", MAXTEMP_NEXT)
# Extract tomorrow's precipitation from "Sydney Metropolitan" section:
LINE = SECTIONLINE
while cmp(PRECIP_NEXT, "unknown") == 0
global LINE = LINE + 1
if occursin("precis", WEBPAGE[LINE])
global PRECIP_NEXT = WEBPAGE[LINE]
end
end
println("Tomorrow's precipitation: ", PRECIP_NEXT)
# descriptions; storm, clear, cloudy, overcast, sunny
if occursin("torm", PRECIP_TODAY)
global PRECIP_RESULT = "Storm"
elseif occursin("ain", PRECIP_TODAY) || occursin("hower", PRECIP_TODAY) || occursin("rizzle", PRECIP_TODAY)
global PRECIP_RESULT = "Rain"
elseif occursin("loud", PRECIP_TODAY) || occursin("vercast", PRECIP_TODAY)
global PRECIP_RESULT = "Cloudy"
elseif occursin("unny", PRECIP_TODAY) || occursin("lear", PRECIP_TODAY)
global PRECIP_RESULT = "Sunny"
else
global PRECIP_RESULT = "-"
end
println("Today's precipitation: ", PRECIP_RESULT)
if occursin("torm", PRECIP_NEXT)
global PRECIP_RESULT = PRECIP_RESULT * " Storm"
elseif occursin("ain", PRECIP_NEXT) || occursin("hower", PRECIP_NEXT) || occursin("rizzle", PRECIP_NEXT)
global PRECIP_RESULT = PRECIP_RESULT * " Rain"
elseif occursin("loud", PRECIP_NEXT) || occursin("vercast", PRECIP_NEXT)
global PRECIP_RESULT = PRECIP_RESULT * " Cloudy"
elseif occursin("unny", PRECIP_NEXT) || occursin("lear", PRECIP_NEXT)
global PRECIP_RESULT = PRECIP_RESULT * " Sunny "
else
global PRECIP_RESULT = PRECIP_RESULT * "-"
end
println("Today and tomorrow's precipitation: ", PRECIP_RESULT)
if MAXTEMP == 99
MAXTEMP_FINAL = ""
else
MAXTEMP_FINAL = string(MAXTEMP)
end
TEMP_RESULT = MAXTEMP_FINAL * "° " * MINTEMP_NEXT * "° " * MAXTEMP_NEXT * "°"
println("Today and tomorrow's temp: ", TEMP_RESULT)
open("/home/brett/.weather_text_temp_syd", "w") do f
write(f, TEMP_RESULT)
end
open("/home/brett/.weather_text_syd", "w") do f
write(f, PRECIP_RESULT) end
© Brett Mahar
site nginx on openbsd