Skip navigation

Converting FOAF to OPML

Soon my first professional contract comes to an end, and I will have time again to develop my newsfeed addiction.

Previously I used Bloglines, which is certainly one of the better on-line feed readers. I’m switching to a desktop feed reader however, I’m trying out Liferea.

I’d like to add all the Planet Ugent blogs to my list of feeds, but the available OPML contains no URL’s. Pretty useless this way. Nothing is lost however, since they still have a FOAF export. The FOAF however contains only the blog URLs, not the actual feeds.

Ruby to the rescue!

require 'rubygems'
require 'open-uri'
require 'hpricot'

doc = Hpricot open('http://planet-ugent.be/foafroll.xml')

def extract_feed(url)
  return 'error://no url' unless url && url != ''
  begin
    page = Hpricot(open(url))
    %w(application/atom+xml application/rss+xml).each do |t|
      link = page.at("link[@type=#{t}]")
      if link
        link = link.attributes['href']
        if link =~ /^\//
          link = url[/^[w]+:\/\/[^\/]+/] + link
        elsif link !~ /^[^\/]+:\/\//
          link = url[/^.*\//] + link
        end
        return link
      end
    end
    'error://not found'
  rescue Timeout::Error
    'error://timeout'
  rescue SocketError
    'error://socket error'
  end
end

feeds = doc.search('foaf:member').map do |m|
  name = m.at('foaf:name').inner_html
  url = m.at('foaf:document').attributes['rdf:about']
  [name, extract_feed(url)]
end

puts %(<opml version="1.1">
  <head>
    <title>Planet UGent
    <dateCreated>#{Time.now.rfc822}
    <dateModified>#{Time.now.rfc822}
    <ownerName>Ikke
    <ownerEmail>eikke at eikke dot commercial
  </head>
  <body>
)

feeds.each do |name, url|
  puts %(    <outline text=”#{name}” xmlUrl=”#{url}”/>\n)
end

puts ”  </body>\n</opml>”

This script will extract the FOAF names and urls, load each page and extract the feed. Atom feeds get precedence over RSS feeds. It should be able to handle relative URLs, but this is not thoroughly tested. The OPML is written on standard out.

Find the result here.

Pinyin for Ruby 0.0.1 has been released

Today I released my very first public Ruby project!

Pinyin is a pure Ruby library with a specific focus : converting between various transliteration systems for Mandarin Chinese.

Over time various systems have been devised to capture the phonetics of the Chinese language, often as a teaching aid.

This is the first public release. Pinyin for Ruby can currently convert between five different systems, with more to come.

- Hanyu Pinyin
- Wade-Giles
- Tongyong Pinyin
- Zhuyin Fuhao (BoPoMoFo)
- IPA (International Phonetic Alphabet)

It does this by decomposing each syllable into its three components (initial, final and tone) and then recomposing the syllable in a different system. This approach was chosen over a table based approach because it better captures the way each system works.

Conversion tables are included to test the package.

There’s still plenty to be done, but since it already has some nice capabilities I’m putting this out in the open, to see if there is any interest. Next step is to move from my private SVN repository to the one on Rubyforge. The project also needs a homepage.

Give it a whirl! Simply type

gem install pinyin

(Adding ’sudo’ where necessary.)

The daily Nitro

Important 2007-12-01 : This article is deprecated. The location of the repository changed and I haven’t had the time to update my scripts to reflect this. With version 0.50 approaching an easier way of trying out Nitro is on the horizon. I’ll write a new article soon that will supersede this one. If you need help getting Nitro up and running please ask advice on the Nitro mailing list.

Update: Changed the RUBYOPT line to load rubygems and to be compatible with the RSpec specs.

As I’ve mentioned I’m becoming involved in the Nitro community, a web framework for Ruby.

The latest release is 0.42, but it’s an old, outdated release and is no longer recommended. In stead we urge everyone to use the repository version, wich in time will become 0.50. The version control system used is Darcs, a powerful distributed VCS written in Haskell. However since darcs is less popular than say Subversion or CVS, I decided to make a daily tarball. This way you can try the repo version without having darcs installed.

It can be found at http://arnebrasseur.net/glycerin/nitro-latest.tar.gz. This is a symlink to a timestamped file in the same directory. A new one should be created daily around 9.30 CET (8.30 GMT).

To get started with it you need to set up your ruby load path. The easiest way to do this is to load the glycerin script, which will set up the load path to include the various Nitro components (Nitro, Raw, Og and Glue).

I have this in my ~/.bashrc to do this behind the scenes.

export NITRO_REPO=~/work/nitro
export RUBYOPT="-r$NITRO_REPO/script/lib/glycerin -rubygems"

You will also need Facets, Xmlsimple and RedCloth. These can be installed as gems. To run the blog example you also need the uuidtools gem.

There’s also a nitro command to start a Nitro app with various options. It can be found under nitro/nitro/bin, so I added this to .bashrc:

export PATH=~/work/nitro/nitro/bin:"${PATH}"

An overview of the command line options (and other assorted Nitro articles) are available on my wiki.

To get a feel for a typical nitro application, take a look at examples/blog.

Please report any difficulties you had with getting the repo version up and running.

Nitro update

Nitro is seeing some action again recently, which is mighty good, obviously. Nitro is a free software framework (under the liberal BSD licence) for building wep applications quickly, utilizing the lightweight Ruby language.

The 0.42 release is very outdated compared to the repo version, so we’re all preparing for a dazzling 0.50.

The team has decided to switch from Test/Unit to RSpec, so we’re busy doing the conversion and making sure all specs go green. There’s also an updated example blog application bundled with the source, that demonstrates the new API. Be sure to check it out and report any problems!

So why Nitro, and not Rails for instance? To put it in the words of lead developer George Moschivitis, “Nitro is to Rails what Ruby is to Python”.

I’ll leave it to your curiosity to find out what that really means. Suffice to say that after six months of trying to master Rails, Nitro was a breath of fresh air for me. I’m very happy to help the developers to move this project forward. If you have any questions, head over to #Nitro on freenode IRC.