Last active
December 14, 2024 19:13
-
-
Save okuramasafumi/bc875362317415f6ad71932b271fc5d9 to your computer and use it in GitHub Desktop.
Rubyists OPMLの作り方
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'builder' | |
require 'json' | |
list = JSON.load_file('list.json') | |
builder = Builder::XmlMarkup.new | |
builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" | |
xml = builder.opml(version: '1.0') do |opml| | |
opml.head do |head| | |
head.title("This is the title") | |
head.dateCreated(Time.now) | |
head.dateModified(Time.now) | |
head.ownerName("OKURA Masafumi") | |
end | |
opml.body do |body| | |
body.outline(text: 'Rubyists Feed', title: 'Rubyists Feed') do |rubyists_feed| | |
list.each do |name, data| | |
title = data['title'] | |
rubyists_feed.outline(text: title, description: title, title: title, type: 'rss', xmlUrl: data['url'], htmlUrl: data['html_link']) | |
end | |
end | |
end | |
end | |
File.open('rubyists-feed-opml.xml', 'w') do |f| | |
f.puts xml | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'rss' | |
require 'json' | |
def from_rss(parsed) | |
{ | |
title: parsed.channel.title, | |
updated: nil, | |
feed_type: parsed.feed_type, | |
html_link: parsed.channel.link, | |
} | |
end | |
def from_atom(parsed) | |
author = parsed.author | |
unless author | |
author = parsed.entry.author | |
end | |
title = parsed.title.content | |
{ | |
name: author.name.content, | |
email: author.email&.content, | |
title: title, | |
updated: parsed.updated.content, | |
feed_type: parsed.feed_type, | |
html_link: parsed.link.href | |
} | |
end | |
list = File.readlines('list.txt').map do |line| | |
line.chomp! | |
url, *name = line.split(" ") | |
[url, name.join(" ")] | |
end | |
# list = [["https://okuramasafumi.hatenablog.jp/rss", "@okuramasafumi"]] | |
json = {} | |
list.each do |url, name| | |
parsed = RSS::Parser.parse(URI(url)) | |
data = case parsed | |
when RSS::Rss then from_rss(parsed).merge(name: name) | |
when RSS::Atom::Feed then from_atom(parsed) | |
when nil | |
puts "#{url} is probably RDF" | |
else | |
puts "Unknown feed type #{data.class} from #{url}" | |
end | |
if data | |
data.update(url: url) | |
json[name] = data | |
end | |
rescue RSS::NotWellFormedError | |
puts "Not valid feed: #{url}" | |
rescue OpenURI::HTTPError | |
puts "Invalid URL: #{url}" | |
end | |
File.open('list.json', 'w') do |f| | |
JSON.dump(json, f) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment