0

RSS フィードを取得して解析し、解析されたタイトルと説明をウィジェットに送信する、この威勢のいいウィジェットから次の Ruby スクリプトを使用しています。

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

URL が xml ファイル用であるため、RSS フィードの例は正しく機能します。ただし、実際の xml ファイルを提供しない別の rss フィードにこれを使用したいと考えています。私が欲しいこの 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」も試しました"view-source: http://www.ttc.ca/RSS/Service_Alerts/index.rss " しかし運が悪い。この RSS フィードに関連する実際の xml データを取得して、この ruby​​ スクリプトで使用できるようにする方法を知っている人はいますか?

4

1 に答える 1