Fandom

Music Player Daemon Community Wiki

Client:Playlistbuilder

525pages on
this wiki
Add New Page
Talk2 Share
  • Download: playlistbuilder
  • Description: A little ruby script that uses Audioscrobbler to append songs from artists similar to the one you're currently listening to the current playlist.
  • Installation: After downloading just copy it to a place of your choice, i.e. (as root) /usr/local/bin
# on many unix platforms (including Mac OS X) the following should do the trick
sudo wget --output-document=/usr/local/bin/playlistbuilder --no-check-certificate https://trac.fs.ei.tum.de/musik/browser/mpd/playlistbuilder/playlistbuilder?format=raw
sudo chmod a+x /usr/local/bin/playlistbuilder
sudo gem install scrobbler librmpd
  • Notes: beside ruby (and ruby-dev package if you get LoadErrors when doing 'gem install') you need the following gems.
  • Usage:
$ playlistbuilder --help
Builds a MPD playlist based on similar artists.
Every parameter is an artist.
If no artist is given, MPD is queued for the current track.
For more infos: $ view /usr/local/bin/playlistbuilder
    -s, --silent                     Don't write status messages
    -n, --no-mpd                     Don't interact with MPD
    -p, --pretend                    Just pretend every acton
    -d, --allow-duplicates           Don't check current playlist to avoied  duplicates
    -o, --only-similar               Don't allow tracks by the given artists themselves
    -t, --tags                       Treat parameters as tags instead of artists
    -m, --match MATCH                Percental limit of what a 'similar' artist is
                                     Detault 90
    -i, --items ITEMS                Max items to enqueue
                                     Detault all
    -e, --exclude REGEX              Regular expression to exclude specific artists

Get the artist of the current song, look up some similar artists, search your mpd-music for these artists and add all songs of them to the playlist

 $ playlistbuilder

Queue songs from artists tagged as 'rock' or 'jazz'. The artists have to be tagged at least 60% as often as the one with the most tags

 $ playlistbuilder -m 60 --tags rock jazz

Look up your music for artists similar to the Arctic Monkeys and the Kaiser Chiefs and add 5 similar songs randomly

 $ playlistbuilder --items 5 "arctic monkeys" "kaiser chiefs"   # you may also use -i

Use the -p (--pretend) switch to do all but the actual appending. Use the -m (--match) to give an other match-level (default 90, lower means the artist is less similar but you may got more matches within your music). Use -s (--silent) to supress non-error output. With -n (--no-mpd) you can disable the MPD-interaction and only see the list of similar artists. You can exclude artists via -e (--exclude). This can be handy if one artist is a substring of an other.

 $ playlistbuilder -p -e fatboy atb

playlistbuilder uses the same env-vars as mpc, so you can use MPD_HOST and MPD_PORT to configure the connection.

  • Script
#! /usr/bin/env ruby
# required gems: scrobbler, librmpd
# Licence: GNU GPLv2
# Author: jonas@fs.ei.tum.de
## ABOUT PLAYLISTBUILDER
# http://mpd.wikia.com/wiki/Client:Playlistbuilder
#
# Get the artist of the current song, look up some similar artists,random songs of them to the playlist
# 
#  playlistbuilder
#
# Look up your music for artists similar to the Arctic Monkeys and the Kaiser Chiefs
# and add 5 similar songs each
# 
#  playlistbuilder -i 5 "arctic monkeys" "kaiser chiefs"
#
# Use the -p (--pretend) switch to do all but the actual appending.
# Use the -m (--match) to give an other match-level lower means the artist is less similar but you may got more matches within your music).
# Use -s (--silent) to supress non-error output.
# With -n (--no-mpd) you can disable the MPD-interaction and only see the list of similar artists.

## INSTALLATION
# wget https://trac.fs.ei.tum.de/musik/browser/mpd/playlistbuilder/playlistbuilder?format=raw
#
# wget http://rubyforge.org/frs/download.php/15722/librmpd-0.1.1.gem
# sudo gem install librmpd-0.1.1.gm
#
# sudo gem install scrobbler
#

require "rubygems"
require "scrobbler"
require "librmpd"

class Array
  def random
    # implement a sort of random-choice
    self[rand(self.length)]
  end
end

class MPD
  def self.new_connection
    host = ( ENV['MPD_HOST'] or 'localhost' )
)
    mpd = self.new host, port
    try = 0
    begin
      puts "connecting to #{host}:#{port}..." if $options.verbose
      mpd.connect
    rescue RuntimeError
      try += 1
      unless try > 3
         $stderr.puts "connection failed on #{try}. attempt, retrying..."
         retry
      else
         $stderr.puts "unable to connect"
         exit 1
      end
    end
    return mpd
  end
end #class MPD

class Options
  attr_reader :verbose, :use_mpd, :pretend, :match, :items, :artists
  def initialize args
    @verbose = true
    @use_mpd = true
    @pretend = false
    @match = 50
    @items = 20
    @artists = []
    until args.empty?
      t = args.shift
      @verbose = false if t == "-s" or t == "--silent"
      @use_mpd = false if t == "-n" or t == "--no-mpd"
      @pretend = true if t == "-p" or t == "--pretend"
      @match = args.shift.to_i if t == "-m" or t == "--match"
      @items = args.shift.to_i if t == "-i" or t == "--items"
      @artists.push t unless t[0].chr == "-"
      if t == "-h" or t == "--help" or t == "-?"
        puts "some help would be realy nice..."
        exit 0
      end #help
    end
  end
end #class Options

$options = Options.new ARGV
#puts $options.to_yaml

mpd = MPD.new_connection if $options.use_mpd

if $options.artists.empty? and mpd
  song = mpd.current_song

  unless song
    $stderr.puts "currently not playing, please give an artist as argument"
    exit 1
  end

  #puts "#{song.artist} - #{song.title}"
  $options.artists.push song.artist
end

$options.artists.each do |artist_name | # quick and dirty :-/
artist = Scrobbler::Artist.new artist_name

# getting similar artists can take a little time
similar = artist.similar.select {|a| a.match.to_f > $options.match}
similar_artists = similar.collect {|a| a.name}

if $options.verbose
  puts "Similar Artists for #{artist_name}:"
  puts similar_artists
end

# continues with the next artist if no mpd invocation is planed
next unless $options.use_mpd

# reconnect if we lost meanwhile the connection
mpd.connect unless mpd.connected?

songs_by_artist = Hash.new

similar_artists.each do |a|
  songs = mpd.find "artist", a
  songs_by_artist[a] = songs unless songs.empty?
end

#puts songs_by_artist.to_yaml

if songs_by_artist.empty?
  puts "no similar artists found in your music-db"
  exit 0
end

songs_to_add = Array.new
# we have to get a new artist array because it can happen
# that no songs a found for a similar_artist
artists = songs_by_artist.keys

$options.items.times do
  artist = artists.random
  song = songs_by_artist[artist].random
  songs_to_add.push song
end

#puts songs_to_add.to_yaml

# reconnect if we lost meanwhile the connection
mpd.connect unless mpd.connected?

songs_to_add.each do |song|
  puts "adding #{song.artist} - #{song.title}" if $options.verbose
  mpd.add song.file unless $options.pretend
end
end # each artist

see also: lastfmpdqc

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki