| 1 | package App::Pimpd::Playlist::Favorite; | ||
| 2 | use strict; | ||
| 3 | no warnings 'experimental::smartmatch'; | ||
| 4 | #use encoding 'utf8'; | ||
| 5 | use open qw(:utf8 :std); | ||
| 6 | |||
| 7 | BEGIN { | ||
| 8 | use Exporter; | ||
| 9 | use vars qw(@ISA @EXPORT); | ||
| 10 | |||
| 11 | @ISA = qw(Exporter); | ||
| 12 | @EXPORT = qw( | ||
| 13 | add_to_favlist | ||
| 14 | already_loved | ||
| 15 | search_favlist | ||
| 16 | remove_favorite | ||
| 17 | ); | ||
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | use Carp 'confess'; | ||
| 22 | use App::Pimpd; | ||
| 23 | use App::Pimpd::Validate; | ||
| 24 | use Term::ExtendedColor qw(fg bg); | ||
| 25 | use Tie::File; | ||
| 26 | |||
| 27 | sub already_loved { | ||
| 28 | my($file, $playlist) = @_; | ||
| 29 | |||
| 30 | my $fh; | ||
| 31 | # Ok, we have not specified an arbitary playlist name, let's see if this | ||
| 32 | # song have been loved yet! | ||
| 33 | if(!defined($playlist)) { | ||
| 34 | if(! -e $config{loved_database}) { | ||
| 35 | open(my $fh, '>', $config{loved_database}); | ||
| 36 | close $fh; | ||
| 37 | } | ||
| 38 | open($fh, '<', $config{loved_database}) | ||
| 39 | or confess("Cant open '$config{loved_database}': $!"); | ||
| 40 | } | ||
| 41 | |||
| 42 | # See if the song is loved in the arbitary playlist | ||
| 43 | else { | ||
| 44 | if(isa_valid_playlist($playlist)) { | ||
| 45 | open($fh, '<', "$config{playlist_directory}/$playlist.m3u") | ||
| 46 | or confess("Can not open $config{playlist_directory}/$playlist: $!"); | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | chomp(my @songs = <$fh>); | ||
| 54 | close($fh); | ||
| 55 | |||
| 56 | return ($file ~~ @songs) ? 1 : 0; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub remove_favorite { | ||
| 61 | my $query = shift; | ||
| 62 | return if !defined($query); | ||
| 63 | |||
| 64 | tie(my @songs, 'Tie::File', $config{loved_database}) | ||
| 65 | or confess("Cant TIE '$config{loved_database}': $!"); | ||
| 66 | my $i = 0; | ||
| 67 | for my $s(@songs) { | ||
| 68 | if($s =~ m/$query/i) { | ||
| 69 | my $old = $songs[$i]; | ||
| 70 | if(splice(@songs,$i, 1)) { | ||
| 71 | printf("%s removed from favlist\n", $old); | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | die("Cant remove $songs[$i]: $!\n"); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | $i++; | ||
| 78 | } | ||
| 79 | untie(@songs) or confess("Cant close $config{loved_database}: $!"); | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub search_favlist { | ||
| 84 | my $query = shift; | ||
| 85 | |||
| 86 | open(my $fh, '<', $config{loved_database}) | ||
| 87 | or confess("Cant open '$config{loved_database}': $!"); | ||
| 88 | chomp(my @songs = <$fh>); | ||
| 89 | close($fh); | ||
| 90 | |||
| 91 | if(!defined($query)) { | ||
| 92 | print "$_\n" for sort(@songs); | ||
| 93 | return; | ||
| 94 | } | ||
| 95 | |||
| 96 | my @results; | ||
| 97 | for(@songs) { | ||
| 98 | if(invalid_regex($query)) { | ||
| 99 | if($_ =~ m/\Q$query/im) { | ||
| 100 | push(@results, $_); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | if($_ =~ m/$query/im) { | ||
| 105 | push(@results, $_); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | return (wantarray()) ? @results : scalar(@results); | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | sub add_to_favlist { | ||
| 116 | my $favlist_m3u = shift; # arbitary playlist name | ||
| 117 | |||
| 118 | my $artist = $mpd->current->artist // 'undef'; | ||
| 119 | my $album = $mpd->current->album // 'undef'; | ||
| 120 | my $title = $mpd->current->title // 'undef'; | ||
| 121 | my $genre = $mpd->current->genre // 'undef'; | ||
| 122 | my $file = $mpd->current->file; | ||
| 123 | #my $file = $basedir . '/' . $mpd->current->file; | ||
| 124 | |||
| 125 | # Do not complain if an arbitary playlist name is specified. | ||
| 126 | if(!defined($favlist_m3u)) { | ||
| 127 | if(already_loved($file)) { | ||
| 128 | printf("%s by %s is already loved!\n", | ||
| 129 | fg($c[11], $title), fg($c[2], $artist), | ||
| 130 | ); | ||
| 131 | |||
| 132 | return; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | if(already_loved($file, $favlist_m3u)) { | ||
| 137 | printf("%s by %s is already loved in %s\n", | ||
| 138 | fg($c[11], $title), fg($c[2], $artist), fg('bold', $favlist_m3u), | ||
| 139 | ); | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | $genre =~ s/\s+/_/gm; # evil whitespace | ||
| 146 | |||
| 147 | my(undef, undef, undef, undef, $month, $year) = localtime(time); | ||
| 148 | $month += 1; | ||
| 149 | $year += 1900; | ||
| 150 | |||
| 151 | if($favlist_m3u) { | ||
| 152 | $favlist_m3u = "$config{playlist_directory}/" . $favlist_m3u . '.m3u'; | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | # 2010-12-rock.m3u | ||
| 156 | $favlist_m3u = $config{playlist_directory} . "/" | ||
| 157 | . sprintf("%d-%02d-%s.m3u", $year, $month, lc($genre)); | ||
| 158 | } | ||
| 159 | |||
| 160 | # Write the db locally. | ||
| 161 | open(my $fh, '>>', $config{loved_database}) | ||
| 162 | or confess("Cant open '$config{loved_database}': $!"); | ||
| 163 | print $fh "$file\n"; | ||
| 164 | close($fh); | ||
| 165 | |||
| 166 | if(remote_host()) { | ||
| 167 | # Sometimes this yells 'Illegal seek at [ ... ]. | ||
| 168 | # No idea why. It works anyway. Lets fail silently here until | ||
| 169 | # we find a proper solution. | ||
| 170 | |||
| 171 | system('ssh', | ||
| 172 | ('-p', $config{ssh_port}, | ||
| 173 | "$config{ssh_user}\@$config{ssh_host}", "echo '$file' >> $favlist_m3u", | ||
| 174 | ), | ||
| 175 | ) == 0 and do { | ||
| 176 | printf("'%s' >> %s:%s\n", | ||
| 177 | fg($c[3], $title), | ||
| 178 | $config{ssh_host}, | ||
| 179 | fg($c[4], $favlist_m3u), | ||
| 180 | ); | ||
| 181 | return 0; | ||
| 182 | }; | ||
| 183 | return 1; | ||
| 184 | } # Nope, not remote | ||
| 185 | |||
| 186 | open(my $fh, '>>', $favlist_m3u) | ||
| 187 | or die("Could not open '$favlist_m3u' in append mode: $!"); | ||
| 188 | print $fh "$file\n"; | ||
| 189 | close($fh); | ||
| 190 | |||
| 191 | print fg($c[8], fg('bold', $title)), ' => ', fg($c[6], $favlist_m3u), "\n"; | ||
| 192 | |||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | |||
| 197 | 1; | ||
| 198 | |||
| 199 | __END__ |