Feedzirra

http://github.com/pauldix/feedzirra/tree/master

I’d like feedback on the api and any bugs encountered on feeds in the wild. I’ve set up a google group here.

Summary

A feed fetching and parsing library that treats the internet like Godzilla treats Japan: it dominates and eats all.

Description

Feedzirra is a feed library that is designed to get and update many feeds as quickly as possible. This includes using libcurl-multi through the taf2-curb gem for faster http gets, and libxml through nokogiri and sax-machine for faster parsing.

Once you have fetched feeds using Feedzirra, they can be updated using the feed objects. Feedzirra automatically inserts etag and last-modified information from the http response headers to lower bandwidth usage, eliminate unnecessary parsing, and make things speedier in general.

Another feature present in Feedzirra is the ability to create callback functions that get called “on success” and “on failure” when getting a feed. This makes it easy to do things like log errors or update data stores.

The fetching and parsing logic have been decoupled so that either of them can be used in isolation if you’d prefer not to use everything that Feedzirra offers. However, the code examples below use helper methods in the Feed class that put everything together to make things as simple as possible.

The final feature of Feedzirra is the ability to define custom parsing classes. In truth, Feedzirra could be used to parse much more than feeds. Microformats, page scraping, and almost anything else are fair game.

Installation

For now Feedzirra exists only on github. It also has a few gem requirements that are only on github. Before you start you need to have libcurl and libxml installed. If you’re on Leopard you have both. Otherwise, you’ll need to grab them. Once you’ve got those libraries, these are the gems that get used: nokogiri, pauldix-sax-machine, taf2-curb (note that this is a fork that lives on github and not the Ruby Forge version of curb), and pauldix-feedzirra. The feedzirra gemspec has all the dependencies so you should be able to get up and running with the standard github gem install routine:


gem sources -a http://gems.github.com # if you haven't already
gem install pauldix-feedzirra

NOTE:Some people have been reporting a few issues related to installation. First, the Ruby Forge version of curb is not what you want. It will not work. Nor will the curl-multi gem that lives on Ruby Forge. You have to get the taf2-curb fork installed.

If you see this error when doing a require:


/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require’: no such file to load — curb_core (LoadError)
It means that the taf2-curb gem didn’t build correctly. To resolve this you can do a git clone git://github.com/taf2/curb.git then run rake gem in the curb directory, then sudo gem install pkg/curb-0.2.4.0.gem. After that you should be good.

If you see something like this when trying to run it:


NoMethodError: undefined method `on_success’ for #<Curl::Easy:0x1182724>
  from ./lib/feedzirra/feed.rb:88:in `add_url_to_multi
This means that you are requiring curl-multi or the Ruby Forge version of Curb somewhere. You can’t use those and need to get the taf2 version up and running.

If you’re on Debian or Ubuntu and getting errors while trying to install the taf2-curb gem, it could be because you don’t have the latest version of libcurl installed. Do this to fix:


sudo apt-get install libcurl4-gnutls-dev

Another problem could be if you are running Mac Ports and you have libcurl installed through there. You need to uninstall it for curb to work! The version in Mac Ports is old and doesn’t play nice with curb. If you’re running Leopard, you can just uninstall and you should be golden. If you’re on an older version of OS X, you’ll then need to download curl and build from source. Then you’ll have to install the taf2-curb gem again. You might have to perform the step above.

If you’re still having issues, please let me know on the mailing list. Also, Todd Fisher is working on fixing the gem install. Please send him a full error report.

Usage

A gist of the following code


require 

Benchmarks

One of the goals of Feedzirra is speed. This includes not only parsing, but fetching multiple feeds as quickly as possible. I ran a benchmark getting 20 feeds 10 times using Feedzirra, rFeedParser, and FeedNormalizer. For more details the benchmark code can be found in the project in spec/benchmarks/feedzirra_benchmarks.rb


feedzirra          5.170000   1.290000   6.460000 ( 18.917796)
rfeedparser      104.260000  12.220000 116.480000 (244.799063)
feed-normalizer   66.250000   4.010000  70.260000 (191.589862)
The result of that benchmark is a bit sketchy because of the network variability. Running 10 times against the same 20 feeds was meant to smooth some of that out. However, there is also a benchmark comparing parsing speed in spec/benchmarks/parsing_benchmark.rb on an atom feed.

feedzirra        0.500000   0.030000   0.530000 (  0.658744)
rfeedparser      8.400000   1.110000   9.510000 ( 11.839827)
feed-normalizer  5.980000   0.160000   6.140000 (  7.576140)
There’s also a benchmark that shows the results of using Feedzirra to perform updates on feeds you’ve already pulled in. I tested against 179 feeds. The first is the initial pull and the second is an update 65 seconds later. I’m not sure how many of them support etag and last-modified, so performance may be better or worse depending on what feeds you’re requesting.

feedzirra fetch and parse  4.010000   0.710000   4.720000 ( 15.110101)
feedzirra update           0.660000   0.280000   0.940000 (  5.152709)

Next Steps

This thing needs to hammer on many different feeds in the wild. I’m sure there will be bugs. I want to find them and crush them. I didn’t bother using the test suite for feedparser. i wanted to start fresh.

Here are some more specific TODOs.

  • Fix the iTunes parser so things are normalized again
  • Fix the Zlib deflate error
  • Fix this error: http://github.com/inbox/70508
  • Convert to use Typhoeus instead of taf2-curb
  • Make the entries parse all link fields
  • Make a feedzirra-rails gem to integrate feedzirra seamlessly with Rails and ActiveRecord.
  • Create a super sweet DSL for defining new parsers.
  • Test against Ruby 1.9.1 and fix any bugs.
  • Clean up the fetching code inside feed.rb so it doesn’t suck so hard.
  • Readdress how feeds determine if they can parse a document. Maybe I should use namespaces instead?

LICENSE

(The MIT License)

Copyright © 2009:

Paul Dix

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.