rss.rb 1016 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. require 'simple-rss'
  2. module Rss
  3. def self.fetch(url, limit = 10)
  4. cache_key = 'rss::' + url
  5. items = Cache.get( cache_key )
  6. return items if items
  7. begin
  8. puts 'fetch rss...'
  9. response = UserAgent.request(url)
  10. if !response.success?
  11. raise "Can't fetch '#{url}', http code: #{response.code.to_s}"
  12. return
  13. end
  14. rss = SimpleRSS.parse response.body
  15. items = []
  16. fetched = 0
  17. rss.items.each { |item|
  18. record = {
  19. :id => item.id,
  20. :title => Encode.conv( 'utf8', item.title ),
  21. :summary => Encode.conv( 'utf8', item.summary ),
  22. :link => item.link,
  23. :published => item.published
  24. }
  25. items.push record
  26. fetched += 1
  27. break item if fetched == limit.to_i
  28. }
  29. Cache.write( cache_key, items, :expires_in => 4.hours )
  30. rescue Exception => e
  31. puts "can't fetch #{url}"
  32. puts e.inspect
  33. return
  34. end
  35. return items
  36. end
  37. end