App-MPDJ

 view release on metacpan or  search on metacpan

lib/App/MPDJ.pm  view on Meta::CPAN

  my $song = $self->mpd->song || 0;
  my $count =
    $self->config->get('after') + $song - $self->mpd->playlist_length + 1;
  if ($count > 0) {
    $self->log->info("Adding $count new songs");
    $self->add_song for 1 .. $count;
  }
}

sub add_song {
  my ($self) = @_;

  $self->add_random_item_from_category('music');
}

sub add_call {
  my ($self) = @_;

  $self->log->info('Injecting call');

  $self->add_random_item_from_category('calls', 'immediate');

  my $now = time;
  $self->{last_call} = $now - $now % $self->config->get('calls-freq');
  $self->log->info('Set last call to ' . $self->{last_call});
}

sub add_random_item_from_category {
  my ($self, $category, $next) = @_;

  my @items = @{ $self->{$category} };

  my $index = int rand scalar @items;
  my $item  = $items[$index];

  my $uri  = $item->{uri};
  my $song = $self->mpd->song || 0;
  my $pos  = $next ? $song + 1 : $self->mpd->playlist_length;
  $self->log->info('Adding ' . $uri . ' at position ' . $pos);

  $self->mpd->add_id($uri, $pos);
}

sub time_for_call {
  my ($self) = @_;

  return unless $self->config->get('calls-freq');
  return time - $self->{last_call} > $self->config->get('calls-freq');
}

sub version {
  say "mpdj (App::MPDJ) version $VERSION";
  exit;
}

sub help {
  print <<'HELP';
Usage: mpdj [options]

Options:
  --mpd             MPD connection string (password@host:port)
  -s,--syslog       Turns on syslog output (debug, info, notice, warn[ing], error, etc)
  -l,--conlog       Turns on console output (same choices as --syslog)
  --no-daemon       Turn off daemonizing
  -b,--before       Number of songs to keep in playlist before current song
  -a,--after        Number of songs to keep in playlist after current song
  -c,--calls-freq   Frequency to inject call signs in seconds
  --calls-path      Path to call sign files
  --music-path      Path to music files
  -f,--conf         Config file to use
  -V,--version      Show version information and exit
  -h,--help         Show this help and exit
HELP

  exit;
}

sub database_changed {
  my ($self) = @_;

  $self->update_cache;
}

sub player_changed {
  my ($self) = @_;

  $self->add_call() if $self->time_for_call();
  $self->add_new_songs();
  $self->remove_old_songs();
}

sub playlist_changed {
  my ($self) = @_;

  $self->player_changed();
}

sub message_changed {
  my $self = shift;

  my @messages = $self->mpd->read_messages();

  foreach my $message (@messages) {
    my $function = 'handle_message_' . $message->{channel};
    $self->$function($message->{message});
  }
}

sub options_changed {
  my $self = shift;

  $self->log->notice('Resetting configuration');

  $self->mpd->repeat(0);
  $self->mpd->random(0);
}

sub handle_message_mpdj {
  my ($self, $message) = @_;

  my ($option, $value) = split /\s+/, $message, 2;

  if ($option eq 'before' or $option eq 'after' or $option eq 'calls-freq') {
    return unless $value =~ /^\d+$/;
    $self->log->info(sprintf 'Setting %s to %s (was %s)',
      $option, $value, $self->config->get($option));
    $self->config->set($option, $value);
    $self->player_changed();
  }
}

1;

__END__

=encoding utf-8

=head1 NAME

App::MPDJ - MPD DJ.

=head1 SYNOPSIS

  > mpdj
  > mpdj --before 2 --after 6
  > mpdj --no-daemon --conlog info

=head1 DESCRIPTION

C<App::MPDJ> is an automatic DJ for your C<MPD> server.  It will manage a queue
of random songs for you just like a real DJ.

=head1 OPTIONS

=over 4

=item --mpd

Sets the MPD connection details.  Should be a string like password@host:port.
The password and port are both optional.

=item -s, --syslog

Turns on sending of log information to syslog at specified level.  Level is a
required parameter can be one of debug, info, notice, warn[ing], err[or],
crit[ical], alert or emerg[ency].

=item -l, --conlog

Turns on sending of log information to console at specified level.  Level is a
required parameter can be one of debug, info, notice, warn[ing], err[or],
crit[ical], alert or emerg[ency].

=item --no-daemon

Run in the foreground instead of trying to fork and exit.

=item -b, --before

Number of songs to keep in the playlist before the current song.  The default
is 2.

=item -a, --after

Number of songs to queue up in the playlist after the current song.  The
default is 2.

=item -c, --calls-freq

Frequency in seconds for call signs to be injected.  The default is 3600 (one
hour).  A value of 0 will disable call sign injection.

=item --calls-path

Path to call sign files.  The default is 'calls'.

=item --music-path

Path to music files.  The default is 'music'.

=item -f --conf

Config file to use

=item -V, --version

Show the current version of the script installed and exit.

=item -h, --help

Show this help and exit.

=back

=head1 CONFIGURATION FILES

The configuration file is formatted as an INI file.  See L<AppConfig> for
details.  If no configuration file is given, the file C</etc/mpdj.conf> will be
read (if it exists) followed by the file C<~/.mpdjrc> (if it exists).  The
values in the latter file will override anything in the first file.  Command



( run in 0.634 second using v1.01-cache-2.11-cpan-d8267643d1d )