GeoRSS aggregator and Layar augmented reality server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

78 lines
2.1 KiB

  1. class Feed
  2. include MongoMapper::Document
  3. key :title, String, :default => "[New feed - hasn't been fetched yet]"
  4. key :feed_url, String # The URL of the RSS feed, not the website that owns it
  5. key :url, String # The URL of website. Called "link" in RSS 2.0
  6. key :description, String
  7. key :guid, String # Atom id or RSS guid
  8. key :generator, String
  9. key :last_fetched, Time, :default => nil
  10. timestamps!
  11. many :posts, :dependent => :destroy
  12. validates :title, :presence => true
  13. validates_format_of :feed_url, :with => URI::regexp(%w(http https)), :message => "must be a valid URL"
  14. after_create :get
  15. # Fetch and parse feed contents from web
  16. def self.get_all
  17. Feed.all.each { |f| f.get }
  18. end
  19. def get
  20. puts "Fetching feed: #{@url}"
  21. Feedzirra::Feed.add_common_feed_entry_element('georss:point', :as => :point)
  22. Feedzirra::Feed.add_common_feed_entry_element('geo:lat', :as => :geo_lat)
  23. Feedzirra::Feed.add_common_feed_entry_element('geo:long', :as => :geo_long)
  24. Feedzirra::Feed.add_common_feed_element('generator', :as => :generator)
  25. feed = Feedzirra::Feed.fetch_and_parse(@feed_url)
  26. self.set(
  27. :title => feed.title,
  28. :url => feed.url,
  29. :description => feed.description,
  30. :generator => feed.generator,
  31. :last_fetched => Time.now
  32. )
  33. feed.entries.each do |e|
  34. if e.geo_lat && e.geo_long
  35. latlng = [e.geo_lat, e.geo_long]
  36. elsif e.point
  37. latlng = e.point.split(' ')
  38. else
  39. next
  40. end
  41. attrs = {
  42. :title => e.title,
  43. :url => e.url,
  44. :author => e.author,
  45. :summary => e.summary,
  46. :content => e.content,
  47. :published => e.published,
  48. :guid => e.id,
  49. :loc => {
  50. :lng => latlng[1].to_f,
  51. :lat => latlng[0].to_f
  52. }
  53. }
  54. if Post.where(:url => e.url).size == 0
  55. self.posts << Post.create(attrs)
  56. else
  57. Post.set({:url => e.url}, attrs)
  58. end
  59. end
  60. end
  61. end