Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSSフィード作成 #387

Merged
merged 6 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ gem 'rack-attack'
gem 'google_drive'
gem 'lazy_high_charts'

# For RSS feed
gem 'ruby-mp3info', :require => 'mp3info'

group :development do
gem 'web-console'
gem 'spring'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ GEM
rspec-retry (0.6.1)
rspec-core (> 3.3)
rspec-support (3.8.0)
ruby-mp3info (0.8.10)
ruby_dep (1.5.0)
ruby_parser (3.12.0)
sexp_processor (~> 4.9)
Expand Down Expand Up @@ -428,6 +429,7 @@ DEPENDENCIES
rake
rspec-rails (~> 3.5)
rspec-retry
ruby-mp3info
sass-rails
scrivito (~> 1.15.0)
scrivito_section_widgets
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/podcasts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ def show
@content = Kramdown::Document.new(@episode.content, input: 'GFM').to_html
@url = request.url
end

def feed
@episodes = Podcast.all.sort_by{|episode| episode.published_at}
@base_url = request.base_url
@author = "一般社団法人 CoderDojo Japan"
@art_work_url = "https://coderdojo.jp/podcasts/cover.jpg"
@current_year = Time.current.year
respond_to do |format|
format.rss { render :layout => false }
end
end
end
18 changes: 18 additions & 0 deletions app/models/podcast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ def exists?(offset: 0)
File.exists?("#{DIR_PATH}/#{self.filename.to_i + offset}.md")
end

def filesize
@size ||= File.size("#{DIR_PATH}/#{self.filename}.mp3")
end

def duration
return @duration if @duration

path = "#{DIR_PATH}/#{self.filename}.mp3"
open_opts = { :encoding => 'utf-8' }
Mp3Info.open(path, open_opts) do |mp3info|
@duration = Time.at(mp3info.length).utc.strftime('%H:%M:%S')
end
end

def title
@title ||= exists? ? self.content.lines.first[2..-1].strip.gsub('<br>', '') : ''
end
Expand All @@ -36,6 +50,10 @@ def description
@desc ||= exists? ? self.content.lines.reject{|l| l =~ /^(\n|<)/ }.second.delete('<br>').strip : ''
end

def published_at
@published_at ||= exists? ? Time.parse(self.content.lines.second.gsub(/<.+?>/, '').delete('収録日: ')) : ''
end

def content
@content ||= exists? ? File.read(path) : ''
end
Expand Down
36 changes: 36 additions & 0 deletions app/views/podcasts/feed.rss.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0", "xmlns:itunes" => "http://www.itunes.com/dtds/Podcast-1.0.dtd" do
xml.channel do
xml.title full_title ""
xml.description full_description ""
xml.link root_url
xml.image @art_work_url
xml.author @author
xml.copyright "Copyright © 2012-#{@current_year} #{@author}"
xml.language "ja"
xml.itunes :category, :text => "Technology" do
xml.itunes :category, :text => "Software How-To"
xml.itunes :category, :text => "Podcasting"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

サブカテゴリの追加の仕方ってこれであってるのかな 🤔💭 ちょっと確認してみよ 👀 💨

カテゴリ: Technology
サブカテゴリ: Software How-To, Podcasting

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

まぁひとまず検証してどんなデータが出てくるか試してみるのが早そうかな ✅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

問題なさそう 😊 ✅

image

end
xml.itunes :type, "serial"
xml.itunes :explicit, "clean"

@episodes.each do |episode|
xml.item do
xml.title episode.title
xml.author @author
xml.description episode.description
xml.link "#{@base_url}#{episode.url}"
xml.guid({:isPermaLink => "false"}, "#{@base_url}#{episode.url}")
xml.itunes :explicit, "clean"
xml.published_at episode.published_at.rfc2822
xml.enclosure({
:url => "#{@base_url}#{episode.url}.mp3",
:length => episode.filesize,
:type => "audio/mpeg"}
)
xml.itunes :duration, episode.duration
end
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
get "/docs/charter_en", to: redirect('/charter_en')
get "/docs/financial-report", to: redirect('/financial-report')
get "/login", to: redirect('/login-8717e64efaf19d7d')
get "/podcasts/feed" => "podcasts#feed"
get "/charter" => "docs#show", id: 'charter'
get "/charter_en" => "docs#show", id: 'charter_en'
get "/financial-report" => "docs#show", id: 'financial-report'
Expand Down
Binary file added public/podcasts/apple-rss-cover.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/podcasts/apple-rss-cover.pxm
Binary file not shown.