App-pimpd

 view release on metacpan or  search on metacpan

lib/App/Pimpd/Collection/Album.pm  view on Meta::CPAN

package App::Pimpd::Collection::Album;
use strict;

BEGIN {
  use Exporter;
  use vars qw(@ISA @EXPORT);
  @ISA = qw(Exporter);
  @EXPORT = qw(
    songs_on_album
    albums_by_artist
    delete_album
  );
}

use App::Pimpd;
use App::Pimpd::Validate;
use Term::ExtendedColor qw(fg);

sub delete_album {
  my $file = $mpd->current->file;

  my($path) = $file =~ m|(.+)/.+$|m;
  my @songs = $mpd->collection->all_items_simple($path);


  printf("Remove %s ? [y/N] ", fg('bold', $path));

  chomp(my $answer = <STDIN>);
  if(lc($answer) ne 'y') {
    return 1;
  }

  $path = "$config{music_directory}/$path";

  if(remote_host()) {
    open(OLD_STDOUT, '>&', STDOUT) or die("Cant dupe STDOUT: $!");
    close(STDOUT);
    system(
      'ssh', "-p $config{ssh_port}",
      "$config{ssh_user}\@$config{ssh_host}",
      "rm -rv '$path'",
    ) == 0 and do {
      open(STDOUT, '>&', OLD_STDOUT) or die("Cant reopen STDOUT: $!");
      printf("Removed %s successfully\n", fg('bold', $path));
      return;
    };
    open(STDOUT, '>&', OLD_STDOUT) or die("Cant reopen STDOUT: $!");

  }
  else {
    if(remove_path($path)) {
      printf("removed '%s'\n", fg('bold', $path));
      return;
    }
    print STDERR "remove_path($path): $!\n";
    return;
  }
  return;
}


sub albums_by_artist {
  my $artist = shift;
  if( (!defined($artist)) or ($artist eq '') ) {
    $artist = $mpd->current->artist;
  }

  return wantarray()
    ? sort($mpd->collection->albums_by_artist($artist))
    : scalar($mpd->collection->albums_by_artist($artist))
    ;
}

sub songs_on_album {
  my($album, $artist) = @_;
  #my $album  = shift // $mpd->current->album;
  #my $artist = shift // $mpd->current->artist;

  $album or $album = $mpd->current->album;
  if(!defined($album) or $album eq '') {
    print STDERR "Album tag missing!\n";
    return 1;
  }

  my @tracks;
  if($artist) {
  # We dont want _all_ albums named 'Best Of'.
  @tracks = grep { $_->artist eq $artist }
    $mpd->collection->songs_from_album($album);
  }
  else {
    @tracks = $mpd->collection->songs_from_album($album);
  }

  return (wantarray()) ? @tracks : scalar(@tracks);
}


1;

__END__

=pod

=head1 NAME

App::Pimpd::Collection::Album - Album functions

=head1 SYNOPSIS

    use App::Pimpd;
    use App::Pimpd::Collection::Album;



( run in 0.366 second using v1.01-cache-2.11-cpan-63c85eba8c4 )