Merge pull request 'Get video ids from youtube with yt-dlp' (#8) from pta/WeatherNews.jl:yt-dlp into main

Reviewed-on: #8
This commit is contained in:
gg 2024-04-17 23:18:48 +00:00
commit b4fbc705ed
2 changed files with 51 additions and 11 deletions

View File

@ -24,6 +24,15 @@ TITLES = Dict(
"ウェザーニュースLiVE" => "_",
)
HOURS = Dict(
"モーニング" => 5,
"サンシャイン" => 8,
"コーヒータイム" => 11,
"アフタヌーン" => 14,
"イブニング" => 17,
"ムーン" => 20
)
function massage_fn(zone, leading_zero; t=now())
zn = ZonedDateTime(t, localzone())
td = Date(astimezone(zn, tz"Asia/Tokyo"))
@ -78,6 +87,26 @@ function get_schedule()
get_schedule(localzone())
end
"Derive full jst from stream title's date string and WNL segment title"
function wnl_title_to_jst(title)
date_re = r"(?<year>[1-9][0-9]{3})[年.](?<month>[0-1]?[0-9])[月.](?<day>[0-3]?[0-9])日?"
d = match(date_re, title)
segment_re = r"モーニング|サンシャイン|コーヒータイム|アフタヌーン|イブニング|ムーン"
s = match(segment_re, title)
h = isnothing(s) ? 23 : HOURS[s.match]
return ZonedDateTime(DateTime(parse(Int64, d[:year]),
parse(Int64, d[:month]),
parse(Int64, d[:day]),
h),
tz"Asia/Tokyo")
end
"Assume titles with ウェザーニュースLiVE and a full date string belong to regular WNL streams"
function iswnl(title)
return occursin(r"ウェザーニュース[Ll][Ii][Vv][Ee]", title) &&
occursin(r"[1-9][0-9]{3}[年.][0-1]?[0-9][月.][0-3]?[0-9]日?", title)
end
"""
Find the current live video's id and update the schedule table if a corresponding row can be found.
@ -87,16 +116,18 @@ julia> WeatherNews.update_schedule_with_live_video(db)
```
"""
function update_schedule_with_live_video(db)
v = WeatherNews.API.video_ids()
zn = ZonedDateTime(now(), localzone())
jn = astimezone(zn, tz"Asia/Tokyo")
jst_1h = floor(jn, Dates.Hour)
@debug jst_1h, v[:live][:id]
return DB.update_schedule_with_video(
db,
jst_1h |> string,
v[:live][:id]
)
vids = WeatherNews.API.video_ids()
for v in vids
if iswnl(v[:title])
jst = wnl_title_to_jst(v[:title])
DB.update_schedule_with_video(db, string(jst), v[:id])
if hour(jst) == 23
DB.update_schedule_with_video(db, string(jst + Hour(1)), v[:id])
end
else
continue
end
end
end
"""

View File

@ -28,5 +28,14 @@ function mscale()
end
function video_ids()
return get_json(urls[:video_ids] |> nocache)
# 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