-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
200 lines (170 loc) · 7.18 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require 'erb'
require 'fileutils'
require 'yaml'
require 'net/scp'
require 'rss'
require 'taglib'
DIR_BASE = File.expand_path(File.dirname(__FILE__))
DIR_IMAGES = "#{DIR_BASE}/images"
DIR_RADIO_NEW = "#{DIR_BASE}/radio_new"
DIR_RADIO = "#{DIR_BASE}/radio"
DIR_TMP = "#{DIR_BASE}/tmp"
CONFIG = YAML.load_file("#{DIR_BASE}/config/config.yml")
SSH_HOST = CONFIG['ssh']['host']
SSH_USER = CONFIG['ssh']['user']
SSH_PATH = CONFIG['ssh']['path']
URL_BASE = CONFIG['url']['base']
def files_sorted_by_mtime(pattern)
Dir.glob(pattern).map{|f| [File.mtime(f), f] }.sort{|a, b| a.first <=> b.first }.map(&:last)
end
def embed_cover_image(mp3_file)
image_file = "#{DIR_IMAGES}/#{File.basename(mp3_file, '.mp3').split('_').first}.jpg"
image_file = "#{DIR_IMAGES}/logo_radio.jpg" unless File.exist?(image_file)
TagLib::MPEG::File.open(mp3_file) do |file|
tag = file.id3v2_tag
unless tag.frame_list.any? {|f| f.is_a?(TagLib::ID3v2::AttachedPictureFrame) }
apic = TagLib::ID3v2::AttachedPictureFrame.new
apic.mime_type = 'image/jpeg'
apic.description = 'Front Cover'
apic.type = TagLib::ID3v2::AttachedPictureFrame::FrontCover
apic.picture = File.read(image_file)
tag.add_frame(apic)
file.save
end
end
end
def create_podcast_rss
RSS::Maker.make('2.0') {|maker|
maker.channel.title = 'オレオレラジオ'
maker.channel.link = "#{URL_BASE}/"
maker.channel.language = 'ja'
maker.channel.description = 'オレオレラジオポッドキャストです。'
maker.channel.itunes_subtitle = 'いやぁ、ラジオって本当にいいもんですね。'
maker.channel.itunes_author = 'オレオレ'
maker.channel.itunes_summary = 'オレオレラジオポッドキャストです。'
maker.items.do_sort = true
maker.image.title = 'RADIO'
maker.image.url = "#{URL_BASE}/logo_radio.jpg"
files_sorted_by_mtime("#{DIR_RADIO}/*.mp3").each do |mp3_file|
mp3_file = Pathname.new(mp3_file)
item = maker.items.new_item
item.date = if (matched = mp3_file.basename('.mp3').to_s.match(/(?<=_)\d{14}\z/))
DateTime.strptime("#{matched}+0900", '%Y%m%d%H%M%S%Z').to_time
else
mp3_file.mtime
end
item.enclosure.url = "#{URL_BASE}/mp3/#{ERB::Util.u(mp3_file.basename.to_s)}"
item.enclosure.length = mp3_file.size
item.enclosure.type = 'audio/mpeg'
TagLib::MPEG::File.open(mp3_file.to_s) do |file|
tag = file.id3v2_tag
item.title = tag.title || mp3_file.basename('.mp3')
item.author = tag.artist || mp3_file.basename('.mp3')
item.description = %!<p>#{tag.album} <a href="http://radiko.jp/share/?sid=TBS&t=#{item.date.strftime('%Y%m%d%H%M%S')}">タイムフリー</a></p>!
item.itunes_subtitle = "#{tag.genre} #{tag.album}".strip
end
end
}.to_s
end
namespace :oreore do
desc 'Prepare to podcast.'
task :prepare do
Net::SSH.start(SSH_HOST, SSH_USER) do |ssh|
ssh.exec!("mkdir -p #{SSH_PATH}/mp3/")
end
Net::SCP.upload!(SSH_HOST, SSH_USER, "#{DIR_IMAGES}/logo_radio.jpg", "#{SSH_PATH}/")
end
desc 'Update podcast.'
task :podcast do
# Embed cover image and upload mp3
files_sorted_by_mtime("#{DIR_RADIO_NEW}/*.mp3").each do |mp3_file|
embed_cover_image(mp3_file)
Net::SCP.upload!(SSH_HOST, SSH_USER, mp3_file, "#{SSH_PATH}/mp3/")
FileUtils.move(mp3_file, DIR_RADIO)
end
# Clean expired mp3
Net::SSH.start(SSH_HOST, SSH_USER) do |ssh|
files_sorted_by_mtime("#{DIR_RADIO}/*.mp3").each do |mp3_file|
limit = (Time.now - (86400*365)).strftime('%Y%m%d')
if File.basename(mp3_file, '.mp3').split('_').last < limit
File.delete(mp3_file)
ssh.exec!("rm -f #{SSH_PATH}/mp3/#{File.basename(mp3_file)}")
end
end
end
# Create and upload podcast rss
rss_file = "#{DIR_TMP}/rss.xml"
File.open(rss_file, 'w') {|f| f.puts create_podcast_rss }
Net::SCP.upload!(SSH_HOST, SSH_USER, rss_file, "#{SSH_PATH}/")
end
desc 'Import from ripdiko.'
task :import_from_ripdiko do
indir = Pathname.new(ENV['RIPDIKO_OUTDIR'] || "#{ENV['HOME']}/Music/Radiko")
outdir = Pathname.new(DIR_RADIO_NEW)
# Basename should be like "20141219010000-TBS.mp3"
Dir.glob(indir.join '*-*.mp3').each do |mp3_file|
mp3_file = Pathname.new(mp3_file)
TagLib::MPEG::File.open(mp3_file.to_s) do |file|
name = case file.id3v2_tag.title
when /たまむすび/; 'tama954'
when /深夜の馬鹿力/; 'ijuin'
when /カーボーイ/; 'bakusho'
when /不毛な議論/; 'fumou'
when /メガネびいき/; 'megane'
when /粋な夜電波/; 'denpa'
when /バナナムーンGOLD/; 'banana'
when /コント太郎/; 'elekata'
when /日曜天国/; 'nichiten'
when /日曜サンデー/; 'nichiyou'
when /東京ポッド許可局/; 'tokyopod'
when /らじおと/; 'ij'
when /生活は踊る/; 'so'
when /ターン!/; 'ht'
when /星のギガボディ/; 'giga'
when /D\.C\.GARAGE/; 'dcg'
when /デートの時間でそ?!/; 'deso'
when /アニニャン!/; 'ia'
when /ヘイ!タクシー!/; 'kamataku'
when /好奇心プラス/; 'kaeru'
when /問わず語り/; 'edo'
when /すっぴんしゃん/; 'suppin'
when /Laughter Night/; 'warai954'
when /踊り場/; 'odoriba'
when /週末ノオト/; 'weekend'
when /宮藤さんに言っても/; 'gc'
when /佐久間宣行の/; 'sakuma'
when /ON THE PLANET/; 'otp'
when /ケツビ!/; 'elekata'
when /これが宮治でございます/; 'miyaji905'
when /ここがオズワルドさんち!/; 'ozu'
when /楽屋ぞめき/; 'gakuyazomeki'
when /夢は口に出せば叶う!!早番/; 'kanau'
when /百年ラヂオ/; 'hyakunen'
when /おしんり研究所/; 'oshinri'
when /ザブトン5/; 'miyaji'
when /スタンド・バイ・見取り図/; 'stm'
else 'unknown'
end
started_at = mp3_file.basename('.mp3').to_s.split('-').first
mp3_file.rename outdir.join("#{name}_#{started_at}.mp3")
end
end
end
desc 'Import from ripdiru.'
task :import_from_ripdiru do
indir = Pathname.new(ENV['RIPDIRU_OUTDIR'] || "#{ENV['HOME']}/Music/Radiru")
outdir = Pathname.new(DIR_RADIO_NEW)
# Basename should be like "20150423064402-NHK2.mp3"
Dir.glob(indir.join '*-*.mp3').each do |mp3_file|
mp3_file = Pathname.new(mp3_file)
TagLib::MPEG::File.open(mp3_file.to_s) do |file|
name = case file.id3v2_tag.title
when /ラジオ英会話/; 'kaiwa'
else 'unknown'
end
started_at = mp3_file.basename('.mp3').to_s.split('-').first
mp3_file.rename outdir.join("#{name}_#{started_at}.mp3")
end
end
end
end