Skip to content

Commit c02755a

Browse files
authored
Merge pull request #387 from coderdojo-japan/rss_feeds
RSSフィード作成
2 parents 0ddcfa7 + a378348 commit c02755a

File tree

8 files changed

+71
-0
lines changed

8 files changed

+71
-0
lines changed

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ gem 'rack-attack'
3939
gem 'google_drive'
4040
gem 'lazy_high_charts'
4141

42+
# For RSS feed
43+
gem 'ruby-mp3info', :require => 'mp3info'
44+
4245
group :development do
4346
gem 'web-console'
4447
gem 'spring'

Gemfile.lock

+2
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)
@@ -429,6 +430,7 @@ DEPENDENCIES
429430
rake
430431
rspec-rails (~> 3.5)
431432
rspec-retry
433+
ruby-mp3info
432434
sass-rails
433435
scrivito (~> 1.15.0)
434436
scrivito_section_widgets

app/controllers/podcasts_controller.rb

+11
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,15 @@ def show
1515
@content = Kramdown::Document.new(@episode.content, input: 'GFM').to_html
1616
@url = request.url
1717
end
18+
19+
def feed
20+
@episodes = Podcast.all.sort_by{|episode| episode.published_at}
21+
@base_url = request.base_url
22+
@author = "一般社団法人 CoderDojo Japan"
23+
@art_work_url = "https://coderdojo.jp/podcasts/cover.jpg"
24+
@current_year = Time.current.year
25+
respond_to do |format|
26+
format.rss { render :layout => false }
27+
end
28+
end
1829
end

app/models/podcast.rb

+18
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
@@ -36,6 +50,10 @@ def description
3650
@desc ||= exists? ? self.content.lines.reject{|l| l =~ /^(\n|<)/ }.second.delete('<br>').strip : ''
3751
end
3852

53+
def published_at
54+
@published_at ||= exists? ? Time.parse(self.content.lines.second.gsub(/<.+?>/, '').delete('収録日: ')) : ''
55+
end
56+
3957
def content
4058
@content ||= exists? ? File.read(path) : ''
4159
end

app/views/podcasts/feed.rss.builder

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
xml.instruct! :xml, :version => "1.0"
2+
xml.rss :version => "2.0", "xmlns:itunes" => "http://www.itunes.com/dtds/Podcast-1.0.dtd" do
3+
xml.channel do
4+
xml.title full_title ""
5+
xml.description full_description ""
6+
xml.link root_url
7+
xml.image @art_work_url
8+
xml.author @author
9+
xml.copyright "Copyright © 2012-#{@current_year} #{@author}"
10+
xml.language "ja"
11+
xml.itunes :category, :text => "Technology" do
12+
xml.itunes :category, :text => "Software How-To"
13+
xml.itunes :category, :text => "Podcasting"
14+
end
15+
xml.itunes :type, "serial"
16+
xml.itunes :explicit, "clean"
17+
18+
@episodes.each do |episode|
19+
xml.item do
20+
xml.title episode.title
21+
xml.author @author
22+
xml.description episode.description
23+
xml.link "#{@base_url}#{episode.url}"
24+
xml.guid({:isPermaLink => "false"}, "#{@base_url}#{episode.url}")
25+
xml.itunes :explicit, "clean"
26+
xml.published_at episode.published_at.rfc2822
27+
xml.enclosure({
28+
:url => "#{@base_url}#{episode.url}.mp3",
29+
:length => episode.filesize,
30+
:type => "audio/mpeg"}
31+
)
32+
xml.itunes :duration, episode.duration
33+
end
34+
end
35+
end
36+
end

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
get "/docs/charter_en", to: redirect('/charter_en')
1212
get "/docs/financial-report", to: redirect('/financial-report')
1313
get "/login", to: redirect('/login-8717e64efaf19d7d')
14+
get "/podcasts/feed" => "podcasts#feed"
1415
get "/charter" => "docs#show", id: 'charter'
1516
get "/charter_en" => "docs#show", id: 'charter_en'
1617
get "/financial-report" => "docs#show", id: 'financial-report'

public/podcasts/apple-rss-cover.jpg

142 KB
Loading

public/podcasts/apple-rss-cover.pxm

8.04 MB
Binary file not shown.

0 commit comments

Comments
 (0)