GeoRSS aggregator and Layar augmented reality server
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

83 lignes
2.2 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. before_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. # We fetched the feed OK but couldn't parse it. HTTP 200 OK
  27. if feed.is_a? Fixnum
  28. raise
  29. end
  30. self.set(
  31. :title => feed.title,
  32. :url => feed.url,
  33. :description => feed.description,
  34. :generator => feed.generator,
  35. :last_fetched => Time.now
  36. )
  37. feed.entries.each do |e|
  38. if e.geo_lat && e.geo_long
  39. latlng = [e.geo_lat, e.geo_long]
  40. elsif e.point
  41. latlng = e.point.split(' ')
  42. else
  43. next
  44. end
  45. attrs = {
  46. :title => e.title,
  47. :url => e.url,
  48. :author => e.author,
  49. :summary => e.summary,
  50. :content => e.content,
  51. :published => e.published,
  52. :guid => e.id,
  53. :loc => {
  54. :lng => latlng[1].to_f,
  55. :lat => latlng[0].to_f
  56. }
  57. }
  58. if Post.where(:url => e.url).size == 0
  59. self.posts << Post.create(attrs)
  60. else
  61. Post.set({:url => e.url}, attrs)
  62. end
  63. end
  64. end
  65. end