»
January 29, 2009
»

HTTParty! Or How I Got That Wishlist Here

I’m spending too much time in the office and the only music I can listen there comes from Last.fm radio. Sometimes that thing plays something good and when it happens I tend to add the link to artist’s Last.fm profile page to my Delicious bookmarks with “music” tag. That was it. Until now.

Now I have Music wishlist page!

The links I’m pulling from Delicious using HTTParty:

require 'rubygems'
require 'open-uri'
require 'httparty'

class Delicious
  include HTTParty
  base_uri 'https://api.del.icio.us/v1'
  
  def initialize(login, password)
    @auth = {:username => login, :password => password}
  end
  
  def recent(options={})
    options.merge!({:basic_auth => @auth})
    self.class.get('/posts/recent', options)
  end
  
  def recent_by_tag(name)
    # limit on 100 -- it's maximum
    recent(:query => {:tag => name, :count => 100})
  end
  
  def last_fm_music_entries
    ret = []
    recent_by_tag("music")['posts']['post'].each do |post|
      href        = post['href']
      description = post['description']
      if href =~ /^http:\/\/www\.last\.fm/
        if description =~ /^(.+) –.*/
          ret << {:name => $1, :href => href}
        end
      end
    end
    ret  
  end
end

And the rake task I use for Webby page creation is:

namespace :wishlist do

  desc "Pulls last 100 from delicious"
  task :pull do
    if Webby.site.args.raw.first
      artists = Delicious.new('ampatspell', Webby.site.args.raw.first).last_fm_music_entries
      artists.each do |artist|
        name     = artist[:name]
        filename = name.gsub(/ /, '-')
        href     = artist[:href]
        
        full_path     = "music/#{filename}"
        absolute_path = "content/#{full_path}.txt"
      
        unless File.exists?(absolute_path)
          Webby::Builder.create(full_path, 
            :from => File.join(Webby.site.template_dir, "music.erb"), 
            :locals => {:artist => name, :href => href})  
        end
      end
    else
      puts "usage: webby wishlist:pull password"
    end
  end

end
 
Internet Explorer 6
Are you serious?