我使用以下從this dashing widget獲取RSS提要並解析它的ruby腳本,並將解析後的標題和描述發送到小部件。如何以xml格式爲ruby腳本獲取RSS提要
require 'net/http'
require 'uri'
require 'nokogiri'
require 'htmlentities'
news_feeds = {
"seattle-times" => "http://seattletimes.com/rss/home.xml",
}
Decoder = HTMLEntities.new
class News
def initialize(widget_id, feed)
@widget_id = widget_id
# pick apart feed into domain and path
uri = URI.parse(feed)
@path = uri.path
@http = Net::HTTP.new(uri.host)
end
def widget_id()
@widget_id
end
def latest_headlines()
response = @http.request(Net::HTTP::Get.new(@path))
doc = Nokogiri::XML(response.body)
news_headlines = [];
doc.xpath('//channel/item').each do |news_item|
title = clean_html(news_item.xpath('title').text)
summary = clean_html(news_item.xpath('description').text)
news_headlines.push({ title: title, description: summary })
end
news_headlines
end
def clean_html(html)
html = html.gsub(/<\/?[^>]*>/, "")
html = Decoder.decode(html)
return html
end
end
@News = []
news_feeds.each do |widget_id, feed|
begin
@News.push(News.new(widget_id, feed))
rescue Exception => e
puts e.to_s
end
end
SCHEDULER.every '60m', :first_in => 0 do |job|
@News.each do |news|
headlines = news.latest_headlines()
send_event(news.widget_id, { :headlines => headlines })
end
end
示例rss供稿正常工作,因爲該URL是針對xml文件的。不過,我想用這個不同的RSS提要,不提供實際的XML文件。此rss飼料我想要的是在http://www.ttc.ca/RSS/Service_Alerts/index.rss 這似乎不顯示任何東西在小部件上。我沒有使用「http://www.ttc.ca/RSS/Service_Alerts/index.rss」,而是嘗試了「http://www.ttc.ca/RSS/Service_Alerts/index.rss?format=xml」和「查看源代碼:http://www.ttc.ca/RSS/Service_Alerts/index.rss」,但沒有運氣。有誰知道我可以如何獲得與此rss提要相關的實際xml數據,以便我可以將其與此ruby腳本一起使用?
你應該接受diego.greyrobot的回答,因爲它是正確的,所以他可以獲得他的得分積分 –