2021/5/9 RSS
onk.icon
知っていること
標準ライブラリ RSS がある
Rails 製の RSS リーダがある
fastladder
feedjira
feedbin
feedjira (fork)
意外と標準ライブラリ使われていない……?
RSS
code:ruby
require 'rss'
rss = RSS::Parser.parse(rss_source, true)
あれ、Doorkeeper は RSS 出してないんだ
code:ruby
code:ruby
rss = RSS::Parser.parse(xml, true)
code:log
tag <description> is missing in tag <channel> (RSS::MissingTagError)
世の中の RSS はまぁ valid じゃないと思っておいた方がよさそう
code:ruby
rss = RSS::Parser.parse(xml, false)
# =>
# <RSS::Rss:0x00000001060b4468
# ...
どういうメソッドがあるかというと
code:ruby
rss.methods - "".methods
# =>
# [:channel,
# :rss_version,
# :feed_version=,
# :image,
# :setup_maker_elements,
# :items,
# :textinput,
# :rss_version=,
# :channel=,
# :version=,
# :output_encoding,
# :feed_type,
# :feed_subtype,
# :feed_info,
# :output_encoding=,
# :standalone=,
# :to_feed,
# :to_rss,
# :to_atom,
# :to_xml,
# :encoding=,
# :feed_version,
# :standalone,
# :version,
# :setup_maker,
# :xml_stylesheets,
# :xml_stylesheets=,
# :have_required_elements?,
# :tag_name,
# :do_validate,
# :do_validate=,
# :valid?,
# :full_name,
# :parent,
# :validate_for_stream,
# :converter=,
# :convert,
# :set_next_element,
# :parent=,
# :have_xml_content?,
# :validate,
# :need_base64_encode?]
code:ruby
item = rss.items.first
# =>
# @author=nil,
# @category=[],
# @comments=nil,
# @content_encoded=nil,
# @converter=nil,
# @dc_contributor=[],
# @dc_coverage=[],
# @dc_creator=[],
# @dc_date=[],
# @dc_description=[],
# @dc_format=[],
# @dc_identifier=[],
# @dc_language=[],
# @dc_publisher=[],
# @dc_relation=[],
# @dc_rights=[],
# @dc_source=[],
# @dc_subject=[],
# @dc_title=[],
# @dc_type=[],
# @description=
# "(snip...)",
# @do_validate=false,
# @enclosure=nil,
# @itunes_author=nil,
# @itunes_block=nil,
# @itunes_duration=nil,
# @itunes_email=nil,
# @itunes_explicit=nil,
# @itunes_image=nil,
# @itunes_keywords=nil,
# @itunes_name=nil,
# @itunes_subtitle=nil,
# @itunes_summary=nil,
# @parent=nil,
# @pubDate=2020-09-14 00:00:00 +0900,
# @source=nil,
# @title="Kaigi on Rails 特集号",
# @trackback_about=[],
# @trackback_ping=nil>
code:ruby
item.title
# => "Kaigi on Rails 特集号"
item.categories
# => []
item.link
item.date
# => 2020-09-14 00:00:00 +0900
item.date.class
# => Time
item.description
# => "\n<p>『るびま』は、(ry"
item.pubDate
# => 2020-09-14 00:00:00 +0900
Atom フィードでも試す
code:ruby
rss = RSS::Parser.parse(xml, true)
# =>
# ...
item = rss.items.first
# =>
# ...
code:ruby
item.title
なんかズラズラ出てきた :sweat_drops:
code:ruby
item.title.class
# => RSS::Atom::Feed::Title
item.title.to_s
# => "<title lang=\"en\">株式会社はてなに入社しました</title>"
item.title.content
# => "株式会社はてなに入社しました"
Feedjira を使う気持ちが分かってきたな。RSS と Atom とを共通で扱いたいのだろう
feedjira
下に Projects that use Feedjira 書いてあるな
huginn は知ってる
code:ruby
feed = Feedjira.parse(xml)
entry = feed.entries.first
code:ruby
entry.methods - {}.methods
# => :add_categories, :author=, :categories=, :updated, :dc_identifier, :summary=, :title, :title=, :categories, :author, :content=, :summary, :content, :image, :url=, :entry_id=, :dc_identifier=, :image=, :id, :url, :entry_id, :updated=, :published=, :parse_datetime, :published, :sanitize!, :last_modified, :parse code:ruby
entry.updated
# => nil
entry.title
# => "Kaigi on Rails 特集号"
entry.categories
# => []
entry.author
# => nil
entry.summary
# => "\n<p>『るびま』は、Ruby に関する技術記事はもちろんのこと、(snip)"
entry.content
# => nil
entry.url
entry.published
# => 2020-09-13 15:00:00 UTC
Atom は
code:ruby
feed = Feedjira.parse(xml)
feed.entries.first
code:ruby
entry.methods - {}.methods
# => :add_categories, :categories=, :updated, :entry_id, :links, :content, :raw_title=, :title_type=, :add_links, :links=, :author=, :summary=, :categories, :title=, :raw_title, :title_type, :author, :content=, :summary, :image, :url=, :entry_id=, :image=, :url, :title, :id, :updated=, :published=, :parse_datetime, :published, :sanitize!, :last_modified, :parse なんか増えてんな
code:ruby
rss_entry.methods - atom_entry.methods
atom_entry.methods - rss_entry.methods
:hee:
code:ruby
entry.updated
# => 2021-03-31 17:39:26 UTC
entry.title
# => "株式会社はてなに入社しました"
entry.categories
# => []
entry.author
# => "onk"
entry.summary
# => "株式会社はてなに入社しました (snip...)
entry.content
entry.url
entry.published
# => 2021-03-31 17:39:26 UTC
時間 (updated, published) は Time class (UTC)
entry.content は nil の場合がある
Atom は content が本文 HTMLっぽい、summary はタグを除去した文字列っぽい?
はてなブログは本文全文配信しているので、続きを読む記法を使っている場合も entry.content には全文が入る
entry.summary は HTML を含んでいる場合がある
RSS だと content が nil で summary に本文がある?
「続きを読む」をどう扱うか悩むな。気にしなければ entry.content || entry.summary || "" で良さそう
HTML 除去
なんかいいのないかな
Sanitize 使うの賢い
code:ruby
entry.summary
# => "\n<p>『るびま』は、Ruby に関する技術記事はもちろんのこと、Rubyist へのインタビューやエッセイ、その他をお届けするウェブ雑誌です。</p>\n\n<p>このレポートは、2020 年 10 月 3 日(土)に開催される <a href=\"https://kaigionrails.org/\">Kaigi on Rails</a> の基調講演や Sanitize.clean(entry.summary, whitespace_elements: :p, :div) # => "\n『るびま』は、Ruby に関する技術記事はもちろんのこと、Rubyist へのインタビューやエッセイ、その他をお届けするウェブ雑誌です。\n\nこのレポートは、2020 年 10 月 3 日(土)に開催される Kaigi on Rails の基調講演や
ヨサソウ