WeatherNews.jl/src/api.jl

42 lines
1.2 KiB
Julia

using JSON3, HTTP
const urls = Dict(
:schedule => "https://smtgvs.weathernews.jp/a/solive_timetable/timetable.json",
:mscale => "https://weathernews.jp/mscale/json/scale.json",
:video_ids => "https://site.weathernews.jp/site/live/json/list.json"
)
"Append a `nocache` query parameter to the URL."
function nocache(url::AbstractString)
return url * "?nocache=" * string(time_ns())
end
"""
Send a GET Request for `url` and parse the response body as JSON.
"""
function get_json(url)
res = HTTP.get(url)
return JSON3.read(res.body)
end
function schedule()
return get_json(urls[:schedule] |> nocache)
end
function mscale()
return get_json(urls[:mscale] |> nocache)
end
function video_ids()
# depends on external program, yt-dlp
# get first 25 items because sometimes staff spams live cams + 地震 streams
cmd = `yt-dlp --flat-playlist
--extractor-args "youtube:player-client=web"
--ignore-no-formats-error
--dump-single-json
--playlist-items 1:25
https://www.youtube.com/weathernews/streams`
playlist = readchomp(cmd) |> JSON3.read
return playlist[:"entries"]
end