Skip to content

Instantly share code, notes, and snippets.

@y4shiro
Forked from ybenjo/README.md
Last active November 30, 2021 06:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save y4shiro/8f3093c7db7b34e0e83b525979fbe0ec to your computer and use it in GitHub Desktop.
Save y4shiro/8f3093c7db7b34e0e83b525979fbe0ec to your computer and use it in GitHub Desktop.
save AGQR radio programs.

agqr.rb

これは何

AGQR の放送を保存するスクリプト.

fork 元 との差分

ybenjo / agqr.rb

  • 各パッケージのパスを自環境に合わせて変更
  • 音声ファイルの保存先を実行ファイルと同じディレクトリに変更
  • /mp4 ディレクトリが作成できるよう修正
  • 配信 URL を現行の物に修正
  • 配信形式が HLS に変更されたので追随した
# -*- coding: utf-8 -*-
# record AGQR
# usage: use with crontab
# 29,59 * * * sleep 55; ruby agqr.rb
# requirements
# crontab, ruby >~ 1.9, ffmpeg,
require 'yaml'
ffmpeg = '/usr/bin/ffmpeg'
agqr_stream_url = 'https://www.uniqueradio.jp/agplayer5/hls/mbr-ff.m3u8'
current = File.dirname(File.expand_path(__FILE__))
save_dir = "#{current}/data/"
Dir.mkdir(save_dir) if !File.exist?(save_dir)
%w(mp4).each do |dir|
path = save_dir + '/' + dir
Dir.mkdir(path) if !File.exist?(path)
end
schedule_yaml = "#{current}/schedule.yaml"
if !File.exist?(schedule_yaml)
puts "Config file (#{schedule_yaml}) is not found!"
puts "Please make #{schedule_yaml}."
exit 1
end
today = Time.now
# ruby 1.9.x でも動くために汚いけど書き換える
# WDAY = %w(日 月 火 水 木 金 土).zip((0..6).to_a).to_h
tmp = { }
%w(日 月 火 水 木 金 土).each_with_index do |v, i|
tmp[v] = i
end
WDAY = tmp
schedule = YAML.load_file(schedule_yaml)
schedule.each do |program|
program_wday = WDAY[program['wday']]
is_next_day_program = false
# appropriate wday
h, m = program['time'].split(':').map(&:to_i)
if h.zero? && m.zero?
# check next day's wday
# if today.wday is 6 (Sat), next_wday is 0 (Sun)
next_wday = (today.wday + 1).modulo(7)
is_appropriate_wday = program_wday == next_wday
is_next_day_program = true
else
# check today's wday
is_appropriate_wday = program_wday == today.wday
end
# appropriate time
if is_next_day_program
# 日付を跨ぐので録音開始の日付が1日ずれる
next_day = today + 60 * 60 * 24
# today.day + 1 してたら 31 を超えるとTimeがエラー吐く
program_start = Time.new(next_day.year, next_day.month, next_day.day, h, m, 0)
else
program_start = Time.new(today.year, today.month, today.day, h, m, 0)
end
is_appropriate_time = (program_start - today).abs < 120
length = program['length'] * 60 + 10
if is_appropriate_wday && is_appropriate_time
title = (program['title'].to_s + '_' + today.strftime('%Y%m%d')).gsub(' ','')
mp4_path = "#{save_dir}/mp4/#{title}.mp4"
# record stream
rec_command = "#{ffmpeg} -protocol_whitelist file,http,https,tcp,tls,crypto -i #{agqr_stream_url} -movflags faststart -t #{length} -c copy #{mp4_path} >/dev/null 2>&1"
system rec_command
end
end
- title: hidakakuma
wday: 火
time: '23:00'
length: 30
- title: toshitai
wday: 火
time: '23:30'
length: 30
- title: lst
wday: 水
time: '21:00'
length: 30
- title: hitokana
wday: 木
time: '23:00'
length: 30
- title: suikoro
wday: 日
time: '17:00'
length: 30
- title: kokoradi
wday: 日
time: '20:00'
length: 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment