开发者

Optimizing Ruby RSS

开发者 https://www.devze.com 2023-03-10 11:35 出处:网络
I\'m writing a very simple Ruby script to parse tweets out of a twitter RSS feed. Here\'s the code I have:

I'm writing a very simple Ruby script to parse tweets out of a twitter RSS feed. Here's the code I have:

require 'rss'
@rss = RSS::Parser.parse(开发者_如何学C'statuses.xml', false)
outputfile = open("output.txt", "w")
@rss.items.each do |i|
  pubdate = i.published.to_s
  if pubdate.include? '2011-05'
    tweet = i.title.to_s
    tweet = tweet.gsub(/<title>SlyFlourish: /, "")
    tweet = tweet.gsub(/<\/title>/, "\n\n")
    outputfile << tweet
  end
end

I think I'm missing something about dealing with the objects coming out of the RSS parser. Can someone tell me how I can better pull out the title and date entries from the object returned by the parser?


Is there a reason you chose RSS? Parsing XML is expensive.

I'd consider using JSON instead.

There's also a twitter Ruby gem that makes this really easy:

require "twitter"
Twitter.user_timeline("gavin_morrice").each do |tweet|
  puts tweet.text
  puts tweet.created_at
end
0

精彩评论

暂无评论...
验证码 换一张
取 消