WeatherNews.jl/src/WeatherNews.jl

84 lines
2.2 KiB
Julia

module WeatherNews
using JSON3, HTTP, Dates, TimeZones, DataStructures
SCHEDULE_URL = "https://smtgvs.weathernews.jp/a/solive_timetable/timetable.json"
TITLES = Dict(
"ウェザーニュースLiVE・モーニング" => "Morning",
"ウェザーニュースLiVE・サンシャイン" => "Sunshine",
"ウェザーニュースLiVE・コーヒータイム" => "Coffee Time",
"ウェザーニュースLiVE・アフタヌーン" => "Afternoon",
"ウェザーニュースLiVE・イブニング" => "Evening",
"ウェザーニュースLiVE・ムーン " => "Moon",
"ウェザーニュースLiVE" => "_",
)
function dl(url)
res = HTTP.get(url)
return JSON3.read(res.body)
end
function shortperiod(p)
abbrev = Dict{DataType,String}(
Day => "d",
Hour => "h",
Minute => "m",
Second => "s",
Millisecond => "ms"
)
return "$(p.value)$(abbrev[typeof(p)])"
end
function shortduration(d)
return d.periods .|> shortperiod |> p -> join(p, " ")
end
function fmt(v, _, j)
if j == 5
return canonicalize(v) |> shortduration
else
return v
end
end
function massage_fn(zone, leading_zero)
td = Date(now(tz"Asia/Tokyo"))
zero_adjustment = if leading_zero 0 else 1 end
ymd = yearmonthday(td)
zn = ZonedDateTime(now(), localzone())
return function massage(item)
if (item[:hour] == "00:00")
# The closed variable ymd may be mutated.
ymd = yearmonthday(td + Dates.Day(zero_adjustment))
zero_adjustment += 1
end
hms = (map(i -> replace(i, r"^0" => "") |> n -> parse(Int64, n), split(item[:hour], ":"))..., 0)
t = ZonedDateTime(ymd..., hms..., tz"Asia/Tokyo")
n = DataStructures.OrderedDict{Symbol,Any}()
n[:caster] = item[:caster]
n[:title] = TITLES[item[:title]]
n[:t] = t
n[:t2] = astimezone(t, zone)
n[:diff] = n[:t] - zn
return n
end
end
function get_schedule(zone)
s = dl(SCHEDULE_URL)
return map(massage_fn(zone, s[1][:hour] == "00:00"), s)
end
function get_schedule()
get_schedule(localzone())
end
module DB
include("./db.jl")
end
export DB
end