代码之家  ›  专栏  ›  技术社区  ›  Marcos Placona

Ruby-Feedzirra和更新

  •  1
  • Marcos Placona  · 技术社区  · 15 年前

    想让我的头转过来 Feedzirra公司 在这里。

    我有它的所有设置和一切,甚至可以得到结果和更新,但奇怪的事情正在发生。

    我想出了以下代码:

      def initialize(feed_url)
        @feed_url = feed_url
        @rssObject =  Feedzirra::Feed.fetch_and_parse(@feed_url)
      end
    
      def update_from_feed_continuously()    
        @rssObject = Feedzirra::Feed.update(@rssObject)
        if @rssObject.updated?
          puts @rssObject.new_entries.count
        else
          puts "nil"
        end
      end
    

    好的,我在上面所做的,是从大提要开始,然后只获取更新。我确信我一定在做一些愚蠢的事情,即使我能够获取更新,并将它们存储在同一个实例变量中,第一次之后,我再也无法获得这些更新。

    显然,这是因为我正在用只更新的内容覆盖我的实例变量,并丢失完整的feed对象。

    然后我想把我的代码改成:

      def update_from_feed_continuously()    
        feed = Feedzirra::Feed.update(@rssObject)
        if feed.updated?
          puts feed.new_entries.count
        else
          puts "nil"
        end
      end
    

    好吧,我没有重写任何东西,这应该是正确的方法?

    错误的 ,这意味着我注定要总是尝试更新同一个静态提要对象,就像我在一个变量上获得更新一样,我从来没有真正更新过我的“静态提要对象”,并且新添加的项将被附加到我的“提要.新条目”中,因为它们在理论上是新的。

    我肯定我错过了一步,但我真的很感激有人能为我开灯。我已经花了好几个小时的时间来研究这个代码,但是我无法理解它。

    很明显,如果我做了如下的事情,它会很好地工作:

    if feed.updated?
      puts feed.new_entries.count
      @rssObject = initialize(@feed_url)
    else
    

    因为这会用一个全新的feed对象重新初始化我的实例变量,更新会再次出现。

    但这也意味着任何新的更新都将丢失,以及 大规模杀伤 因为我得再装一次。

    事先谢谢!

    3 回复  |  直到 14 年前
        1
  •  6
  •   Rich Apodaca    15 年前

    如何进行更新有点违反当前API的直觉。此示例显示了实现此操作的最佳方法:

    # I'm using Atom here, but it could be anything. You don't need to know ahead of time.
    # It will parse out to the correct format when it updates.
    feed_to_update = Feedzirra::Parser::Atom.new
    feed_to_update.feed_url = some_stored_feed_url
    feed_to_update.etag = some_stored_feed_etag
    feed_to_update.last_modified = some_stored_feed_last_modified
    
    last_entry = Feedzirra::Parser::AtomEntry.new
    last_entry.url = the_url_of_the_last_entry_for_a_feed
    
    feed_to_update.entries = [last_entry]
    
    updated_feed = Feedzirra::Feed.update(feed_to_update)
    
    updated_feed.updated? # => nil if there is nothing new
    updated_feed.new_entries # => [] if nothing new otherwise a collection of feedzirra entries
    updated_feed.etag # => same as before if nothing new. although could change with comments added to entries.
    updated_feed.last_modified # => same as before if nothing new. although could change with comments added to entries.
    

    基本上,您必须保存四个数据块(feed_url, 最后修改的、etag和最新条目的URL)。然后当你 要进行更新,请构造一个新的源对象并调用更新 那。

        2
  •  4
  •   badawym    14 年前

    我认为一个更明显的解决办法是 :if_modified_since 选择 fetch_and_parse 分类方法 Feed https://github.com/pauldix/feedzirra/blob/master/lib/feedzirra/feed.rb#L116 https://github.com/pauldix/feedzirra/blob/master/lib/feedzirra/feed.rb#L206

        3
  •  0
  •   benm    15 年前

    你可以重置 @rssObject 到更新的源。

    feed = Feedzirra::Feed.update(@rssObject)
    if feed.updated?
      puts feed.new_entries.count
      @rssObject = feed
    else
      puts 'nil'
    end
    

    @rssobject中的条目数将随着找到新条目而不断增加。因此,如果第一次提取找到10个条目,然后下一次查找10个新条目, @rssObject.entries.size 将是20岁。

    请注意,无论是否 update 查找新条目。如果 feed.updated? 是假的, feed 将是原始源对象, @rssobject项目 .