Skip to content

Commit 0e54ed1

Browse files
committed
add #387 rssフィードにmp3のファイル情報を追加
podcastsに推奨されていたので、ファイルの情報を追加しました。
1 parent eb00c31 commit 0e54ed1

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ gem 'rack-attack'
3737
gem 'google_drive'
3838
gem 'lazy_high_charts'
3939

40+
# For RSS feed
41+
gem 'ruby-mp3info', :require => 'mp3info'
42+
4043
group :development do
4144
gem 'web-console'
4245
gem 'spring'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ GEM
285285
rspec-retry (0.6.1)
286286
rspec-core (> 3.3)
287287
rspec-support (3.8.0)
288+
ruby-mp3info (0.8.10)
288289
ruby_dep (1.5.0)
289290
ruby_parser (3.12.0)
290291
sexp_processor (~> 4.9)
@@ -428,6 +429,7 @@ DEPENDENCIES
428429
rake
429430
rspec-rails (~> 3.5)
430431
rspec-retry
432+
ruby-mp3info
431433
sass-rails
432434
scrivito (~> 1.15.0)
433435
scrivito_section_widgets

app/models/podcast.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ def exists?(offset: 0)
2828
File.exists?("#{DIR_PATH}/#{self.filename.to_i + offset}.md")
2929
end
3030

31+
def filesize
32+
@size ||= File.size("#{DIR_PATH}/#{self.filename}.mp3")
33+
end
34+
35+
def duration
36+
return @duration if @duration
37+
38+
path = "#{DIR_PATH}/#{self.filename}.mp3"
39+
open_opts = { :encoding => 'utf-8' }
40+
Mp3Info.open(path, open_opts) do |mp3info|
41+
@duration = Time.at(mp3info.length).utc.strftime('%H:%M:%S')
42+
end
43+
end
44+
3145
def title
3246
@title ||= exists? ? self.content.lines.first[2..-1].strip.gsub('<br>', '') : ''
3347
end

app/views/podcasts/feed.rss.builder

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ xml.rss :version => "2.0", "xmlns:itunes" => "http://www.itunes.com/dtds/Podcast
2323
xml.guid({:isPermaLink => "false"}, @domainname + episode.url)
2424
xml.itunes :explicit, "clean"
2525
xml.pubDate episode.published_at.rfc2822
26-
xml.enclosure({:url => @domainname + episode.url + ".mp3", :type => "audio/mpeg"})
26+
xml.enclosure({:url => @domainname + episode.url + ".mp3", :length => episode.filesize, :type => "audio/mpeg"})
27+
xml.itunes :duration, episode.duration
2728
end
2829
end
2930
end

0 commit comments

Comments
 (0)