Skip to content

Commit

Permalink
better log and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio Poli committed Sep 5, 2009
1 parent e17a7fa commit 3b5b1bc
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions loved2itunes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@
PVERSION = "Version 1.1, 09/05/2009"
$KCODE = "u"

require 'rubygems'
begin
require 'rubygems'

gem 'nokogiri', '>= 1.3.3'
gem 'rb-appscript', '>= 0.5.3'
gem 'nokogiri', '>= 1.3.3'
gem 'rb-appscript', '>= 0.5.3'

require 'nokogiri'
require 'appscript'
require 'open-uri'
require 'nokogiri'
require 'appscript'
require 'open-uri'

username = ARGV[0]
playlist_name = ARGV[1] || 'Loved'
api_key = ARGV[2] || API_KEY
username = ARGV[0]
playlist_name = ARGV[1] || 'Loved'
api_key = ARGV[2] || API_KEY

begin
raise("Please specify a username") if username.nil?
p "lastfm2itunes #{PVERSION} running on Ruby #{RUBY_VERSION} (#{RUBY_PLATFORM}), initializing..."
raise("please specify a username") if username.nil?
p "loved2itunes: #{PVERSION} running on Ruby #{RUBY_VERSION} (#{RUBY_PLATFORM}), initializing..."
url = "http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user=#{URI.escape(username.downcase)}&api_key=#{api_key}&limit=0"
doc = Nokogiri::XML(open(url))
# XPath selection.
Expand All @@ -58,22 +58,24 @@
# Reset playlist.
playlist.tracks.get.each{ |tr| tr.delete } if playlist.tracks.get.size.to_i > 0

p "Found #{loved_tracks.size} loved tracks, importing..."
p "loved2itunes: found #{loved_tracks.size} loved tracks, importing..."
counter = 0
loved_tracks.each do |loved_track|
# Grab the name of the loved track.
title = loved_track.search('name')[0].inner_html.to_s
# Get a reference to the existing track, it may be more robust in future.
track_ref = iTunes.library_playlists[1].tracks[title]
# Finally add the track to our playlist.
if track_ref.exists
iTunes.add(track_ref.location.get, :to => playlist)
counter += 1
else
p "Track not found, skipped: #{title}"
end
if track_ref.exists
iTunes.add(track_ref.location.get, :to => playlist)
counter += 1
else
p "loved2itunes: track not found, skipping #{title}"
end
end
p "#{counter}/#{loved_tracks.size} tracks imported into '#{playlist_name}' playlist."
rescue Exception => e
puts "Something was wrong: #{e.message}"
puts "loved2itunes: something went wrong, the error message is: #{e.message}"
ensure
puts "loved2itunes: execution finished."
end

0 comments on commit 3b5b1bc

Please sign in to comment.