GeoRSS aggregator and Layar augmented reality server
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12 年前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. class Feed < ActiveRecord::Base
  2. has_many :posts, :dependent => :destroy
  3. has_and_belongs_to_many :layers
  4. attr_accessible :title, :url, :description, :generator, :last_fetched, :feed_url
  5. validates_format_of :feed_url, :with => URI::regexp(%w(http https)), :message => "must be a valid URL"
  6. after_create :fetch
  7. def self.fetch_all
  8. Feed.all.each { |f| f.fetch }
  9. end
  10. # Fetch and parse feed contents from web
  11. def fetch
  12. puts "Fetching feed: #{self.feed_url}"
  13. Feedzirra::Feed.add_common_feed_entry_element('georss:point', :as => :point)
  14. Feedzirra::Feed.add_common_feed_entry_element('geo:lat', :as => :geo_lat)
  15. Feedzirra::Feed.add_common_feed_entry_element('geo:long', :as => :geo_long)
  16. Feedzirra::Feed.add_common_feed_element('generator', :as => :generator)
  17. feed = Feedzirra::Feed.fetch_and_parse(self.feed_url)
  18. self.update_attributes(
  19. :title => feed.title,
  20. :url => feed.url,
  21. :description => feed.description,
  22. :generator => feed.generator,
  23. :last_fetched => DateTime.now
  24. )
  25. feed.entries.each do |e|
  26. if e.geo_lat && e.geo_long
  27. latlon = [e.geo_lat, e.geo_long]
  28. elsif e.point
  29. latlon = e.point.split(' ')
  30. else
  31. next
  32. end
  33. attrs = {
  34. :title => e.title,
  35. :url => e.url,
  36. :author => e.author,
  37. :summary => e.summary,
  38. :content => e.content,
  39. :published => e.published,
  40. :guid => e.id,
  41. :lon => latlon[1].to_f,
  42. :lat => latlon[0].to_f
  43. }
  44. # Create a new post or update an existing one
  45. post = Post.find_or_initialize_by_url(e.url)
  46. post.feed = self
  47. post.assign_attributes(attrs)
  48. post.save
  49. end
  50. end
  51. end