Create a gist now

Instantly share code, notes, and snippets.

@s4kr4 /README.md forked from ybenjo/README.md
Last active Oct 3, 2016

Embed
What would you like to do?
save AGQR radio programs.

agqr.rb

これは何

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

fork元 ybenjo / README.md

fork 元との違いは

  • 26:00等の表記に対応

使い方

fork元 参照

# -*- coding: utf-8 -*-
# record AGQR
# usage: use with crontab
# 29,59 * * * sleep 55; ruby agqr.rb
# requirements
# crontab, ruby >~ 1.9, ffmpeg, rtmpdump
require 'yaml'
rtmpdump = '/usr/bin/rtmpdump'
ffmpeg = '/usr/bin/ffmpeg'
agqr_stream_url = 'rtmp://fms-base2.mitene.ad.jp/agqr/aandg22'
current = File.dirname(File.expand_path(__FILE__))
save_dir = "#{current}/"
Dir.mkdir(save_dir) if !File.exist?(save_dir)
%w(mp3 flv).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']]
# appropriate wday
h, m = program['time'].split(':').map(&:to_i)
# check whether the hour is next day
if h >= 24
# check next day's wday
# if today.wday is 6 (Sat), next_wday is 0 (Sun)
if h == 24 && m.zero?
next_wday = (today.wday + 1).modulo(7)
next_day = today + 24 * 60 * 60
is_appropriate_wday = program_wday == next_wday
else
# check today's wday
next_wday = today.wday
next_day = today
is_appropriate_wday = (program_wday + 1).modulo(7) == today.wday
end
h -= 24
is_next_day_program = true
else
is_next_day_program = false
if program_wday == today.wday
is_appropriate_wday = true
else
is_appropriate_wday = false
end
end
if is_next_day_program
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(' ','')
flv_path = "#{save_dir}/flv/#{title}.flv"
# record stream
rec_command = "#{rtmpdump} -r #{agqr_stream_url} --live -B #{length} -o #{flv_path} >/dev/null 2>&1"
system rec_command
# encode flv -> m4a
m4a_path = "#{save_dir}/mp3/#{title}.m4a"
m4a_encode_command = "#{ffmpeg} -y -i #{flv_path} -vn -acodec copy #{m4a_path} >/dev/null 2>&1"
system m4a_encode_command
# encode m4a -> mp3
mp3_path = "#{save_dir}/mp3/#{title}.mp3"
mp3_encode_command = "#{ffmpeg} -i #{m4a_path} #{mp3_path} >/dev/null 2>&1"
system mp3_encode_command
# delete m4a
system "rm -rf #{m4a_path}"
end
end
- title: mu_n
wday:
time: '22:00'
length: 60
- title: it_kakumei
wday:
time: '21:00'
length: 30
- title: free_style
wday:
time: '21:30'
length: 30
- title: suzakinishi
wday:
time: '25:00'
length: 30
- title: kokoro
wday:
time: '0:00'
length: 30
- title: alamode
wday:
time: '21:30'
length: 30
- title: seisyun
wday:
time: '25:30'
length: 30
- title: igaitai
wday:
time: '23:30'
length: 30
- title: dream_theater
wday:
time: '21:30'
length: 30
- title: daaaa
wday:
time: '25:00'
length: 30
- title: tls
wday:
time: '25:30'
length: 30
- title: ageage
wday:
time: '19:30'
length: 30
- title: dotai
wday:
time: '27:00'
length: 30
- title: tryangle
wday:
time: '19:30'
length: 30
- title: hitokana
wday:
time: '23:00'
length: 30
- title: asumin
wday:
time: '23:00'
length: 60
- title: okaeri
wday:
time: '22:00'
length: 60
- title: ayahi
wday:
time: '26:00'
length: 30
- title: yumeiro
wday:
time: '20:00'
length: 30
- title: adlib
wday:
time: '25:00'
length: 30
- title: remake
time: '1:30'
wday:
length: 30
- title: uchiasa
time: '21:00'
wday:
length: 30
- title: note
wday:
time: '26:30'
length: 30
- title: you_know_me
wday:
time: '21:00'
length: 30
- title: stay_gold
wday:
time: '26:30'
length: 30
- title: doudemo_ii
wday:
time: '19:30'
length: 30
- title: five_stars_mon
wday:
time: '20:00'
length: 60
- title: five_stars_tue
wday:
time: '20:00'
length: 60
- title: five_stars_wed
wday:
time: '20:00'
length: 60
- title: five_stars_thu
wday:
time: '20:00'
length: 60
- title: five_stars_fri
wday:
time: '20:00'
length: 60
- title: sachika
wday:
time: '26:00'
length: 30
- title: ozanari
wday:
time: '19:00'
length: 30
- title: a_rie
wday:
time: '23:00'
length: 30
- title: agson
wday:
time: '21:00'
length: 120
- title: homerun
wday:
time: '22:00'
length: 60
- title: ohanashi
wday:
time: '20:30'
length: 30
- title: animage
wday:
time: '26:30'
length: 30
- title: delicate_zone
wday:
time: '25:00'
length: 30
- title: ss_ch
wday:
time: '25:30'
length: 30
- title: mikassho
wday:
time: '19:30'
length: 30
- title: pit_inn
wday:
time: '18:30'
length: 30
- title: himitsu_kichi
wday:
time: '26:00'
length: 30
- title: queenty_sat
wday:
time: '10:00'
length: 60
- title: queenty_sun
wday:
time: '10:00'
length: 60
- title: kuroshiro
wday:
time: '17:00'
length: 30
- title: toshitai
wday:
time: '19:30'
length: 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment