require 'pp' require 'rubygems' require 'json' require 'httpclient' username = ARGV[0] password = ARGV[1] api_key = ARGV[2] list_id = ARGV[3] if ! username || ! password || ! api_key || ! list_id print " Usage: ruby clear_list.rb username password api_key list_id " exit 1 end client = HTTPClient.new user = username password = password client.set_auth(nil, user, password) print "Cleaning out list id #{list_id}; this might take a while.\n" res = client.get_content( "http://api.smart.fm/lists/#{list_id}.json", { 'include_items' => 1 } ) sleep 1 while JSON.parse(res)["items_count"] > 0 JSON.parse(res)["items"].each { |item| begin print "Removing item #{item['id']} from the list\n" res = client.delete( "http://api.smart.fm/lists/#{list_id}/items/#{item["id"]}?api_key=#{api_key}" ) if res.body.content != "" || res.header.status_code.to_i >= 300 || res.header.status_code.to_i < 200 print "Whoops, seems to have not worked; error: #{res.body.content}\n" raise Exception.new(true) end #pp res # the res is not json rescue Exception => problem #pp problem print "Had an error removing item #{item['id']}: #{problem}\n" sleep 5 end sleep 1 } res = client.get_content( "http://api.smart.fm/lists/#{list_id}.json", { 'include_items' => 1 } ) end print "Done cleaning out the list.\n"