| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use utf8; | ||
| 7 | |||
| 8 | our $VERSION = '0.002'; # VERSION | ||
| 9 | our $AUTHORITY = 'cpan:VNEALV'; # AUTHORITY | ||
| 10 | |||
| 11 | =encoding UTF8 | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | C<spotify-cli.pl> - Script to interact with Spotify API in various ways. | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | perl bin/spotify-cli.pl -i | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This script allows you to interact with Spotify API in multiple different ways. | ||
| 24 | Providing you with a convinent way to query Spotify API by either the set of predefined commands or inline requests. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | =head1 OPTIONS | ||
| 29 | |||
| 30 | =over 4 | ||
| 31 | |||
| 32 | =item B<-c>, B<--client-id>=I<Spotify App Client ID> | ||
| 33 | |||
| 34 | Your registered Spotify Application Client ID | ||
| 35 | Can be set as environment variable I<client_id> | ||
| 36 | |||
| 37 | =item B<-s>, B<--client-secret>=I<Spotify APP Client Secret> | ||
| 38 | |||
| 39 | Your registered Spotify Application Client Secret | ||
| 40 | Can be set as environment variable I<client_secret> | ||
| 41 | |||
| 42 | =item B<-t>, B<--access-token>=I<Spotify Client Access Token> | ||
| 43 | |||
| 44 | Optional, if passed there will be no need to obtain token and requests can be directly called using it. | ||
| 45 | Can be set as environment variable I<access_token> | ||
| 46 | |||
| 47 | =item B<-i>, B<--interactive> | ||
| 48 | |||
| 49 | If exists it will run this script in interactive mode, where it will continuously wait for a command to perform. | ||
| 50 | However manual handling for Authentication process. | ||
| 51 | |||
| 52 | =item B<-w>, B<--web-server> | ||
| 53 | |||
| 54 | If exists it will make the script run in interactive mode, with an HTTP Webserver running in background listening to port 80 on localhost. | ||
| 55 | So it can process Spotify C<callback> GET requests automatically. and can be extended to receive commands. | ||
| 56 | Note: define L<http://localhost/callback> as a B<Redirect URI> in your Spotify App settings. | ||
| 57 | Also if running in Docker, then run container with I<-p 127.0.0.1:80:80/tcp> option. | ||
| 58 | |||
| 59 | =item B<-l> I<debug>, B<--log-level>=I<info> | ||
| 60 | |||
| 61 | Log level used. with default being I<Info>. | ||
| 62 | |||
| 63 | =back | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | use Pod::Usage; | ||
| 68 | use Getopt::Long; | ||
| 69 | use IO::Async::Loop; | ||
| 70 | use IO::Async::Stream; | ||
| 71 | use Future::AsyncAwait; | ||
| 72 | use Syntax::Keyword::Try; | ||
| 73 | use Log::Any qw($log); | ||
| 74 | use Net::Async::Spotify; | ||
| 75 | use Net::Async::Spotify::Util qw(hash_to_string); | ||
| 76 | use JSON::MaybeUTF8 qw(:v1); | ||
| 77 | use Net::Async::HTTP::Server; | ||
| 78 | use Future::Utils qw(fmap_concat); | ||
| 79 | use Unicode::UTF8 qw(encode_utf8); | ||
| 80 | use Scalar::Util qw(blessed); | ||
| 81 | use Encode qw(encode); | ||
| 82 | |||
| 83 | GetOptions( | ||
| 84 | 'c|client-id=s' => \(my $client_id = $ENV{client_id}), | ||
| 85 | 's|client-secret=s' => \(my $client_secret = $ENV{client_secret}), | ||
| 86 | 't|access-token=s' => \(my $access_token = $ENV{access_token}), | ||
| 87 | 'i|interactive' => \my $interactive, | ||
| 88 | 'w|web-server' => \my $webserver, | ||
| 89 | 'l|log-level=s' => \(my $log_level = 'info'), | ||
| 90 | 'h|help' => \my $help, | ||
| 91 | ); | ||
| 92 | |||
| 93 | require Log::Any::Adapter; | ||
| 94 | Log::Any::Adapter->set( qw(Stdout), log_level => $log_level ); | ||
| 95 | |||
| 96 | pod2usage( | ||
| 97 | { | ||
| 98 | -verbose => 99, | ||
| 99 | -sections => "NAME|SYNOPSIS|DESCRIPTION|OPTIONS|COMMANDS", | ||
| 100 | } | ||
| 101 | ) if $help; | ||
| 102 | |||
| 103 | die 'Need Spotify Client ID and Client Secret' unless ( $client_id and $client_secret ); | ||
| 104 | |||
| 105 | my $loop = IO::Async::Loop->new; | ||
| 106 | $loop->add( my $spotify = Net::Async::Spotify->new( | ||
| 107 | client_id => $client_id, | ||
| 108 | client_secret => $client_secret, | ||
| 109 | access_token => $access_token, | ||
| 110 | ) | ||
| 111 | ); | ||
| 112 | |||
| 113 | my $stream = IO::Async::Stream->new( | ||
| 114 | read_handle => \*STDIN, | ||
| 115 | write_handle => \*STDOUT, | ||
| 116 | on_read => sub { | ||
| 117 | my ( $self, $buffref, $eof ) = @_; | ||
| 118 | |||
| 119 | while( $$buffref =~ s/^(.*\n)// ) { | ||
| 120 | my $line = $1; | ||
| 121 | my @cmd_array = split ' ', $line; | ||
| 122 | my $command = $cmd_array[0]; | ||
| 123 | unless (defined $command) { | ||
| 124 | print "\n"; | ||
| 125 | pod2usage( | ||
| 126 | { | ||
| 127 | -verbose => 99, | ||
| 128 | -sections => "COMMANDS", | ||
| 129 | -exitval => 'NOEXIT', | ||
| 130 | } | ||
| 131 | ); | ||
| 132 | print "Waiting for your Command!...\nCMD: "; | ||
| 133 | return 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | if (exists &{$command}) { | ||
| 137 | # for a happy strict pragma | ||
| 138 | my $method = \&{$command}; | ||
| 139 | $method->(@cmd_array)->retain; | ||
| 140 | } else { | ||
| 141 | generic->(@cmd_array)->retain; | ||
| 142 | } | ||
| 143 | |||
| 144 | } | ||
| 145 | return 0; | ||
| 146 | } | ||
| 147 | ); | ||
| 148 | $loop->add( $stream ); | ||
| 149 | |||
| 150 | my $callback_f = $loop->new_future(label => 'CallbackFuture'); | ||
| 151 | # Web server for incoming callback request | ||
| 152 | my $server = Net::Async::HTTP::Server->new( | ||
| 153 | on_request => sub { | ||
| 154 | my ( $self, $req ) = @_; | ||
| 155 | $log->tracef('Webserver receives %s %s: %s | %s', $req->method, $req->path, {$req->query_form}, $req->body); | ||
| 156 | if ( $req->method eq 'GET' and $req->path eq '/callback' ) { | ||
| 157 | # That's what we are waiting for. | ||
| 158 | my %params = $req->query_form; | ||
| 159 | $callback_f->done(%params) unless $callback_f->is_done; | ||
| 160 | |||
| 161 | my $response = HTTP::Response->new(200); | ||
| 162 | $response->add_content(encode_json_utf8({response => 'Got it! From spotify-cli.pl ;)'})); | ||
| 163 | $response->content_type("application/json"); | ||
| 164 | $response->content_length(length $response->content); | ||
| 165 | |||
| 166 | $req->respond($response); | ||
| 167 | } | ||
| 168 | }, | ||
| 169 | ); | ||
| 170 | $loop->add( $server ); | ||
| 171 | |||
| 172 | my $listner = await $server->listen( | ||
| 173 | addr => { | ||
| 174 | family => 'inet', | ||
| 175 | socktype => 'stream', | ||
| 176 | port => 80, | ||
| 177 | }, | ||
| 178 | ) if $webserver; | ||
| 179 | |||
| 180 | my %authorize = $spotify->authorize(scope => ['scopes'], show_dialog => 'false'); | ||
| 181 | |||
| 182 | $stream->write(sprintf("Your Authorize URI is (state: %s) :\n\n%s\n\n", $authorize{state}, $authorize{uri})); | ||
| 183 | |||
| 184 | if ( $interactive ) { | ||
| 185 | $stream->write("Please insert your `code` response in callback URL...\n"); | ||
| 186 | my ( $auth_code ) = await $stream->read_until( "\n" ); | ||
| 187 | chomp $auth_code; | ||
| 188 | |||
| 189 | $stream->write("Please insert your `state` response in callback URL...\n"); | ||
| 190 | my ( $auth_state ) = await $stream->read_until( "\n" ); | ||
| 191 | chomp $auth_state; | ||
| 192 | |||
| 193 | await $spotify->obtain_token(code => $auth_code, auto_refresh => 1); | ||
| 194 | $stream->write("Waiting for your Command!...\nCMD: "); | ||
| 195 | $loop->run; | ||
| 196 | } elsif ( $webserver ) { | ||
| 197 | my %auth_res = await $callback_f; | ||
| 198 | if ( $auth_res{state} eq $authorize{state} ) { | ||
| 199 | await $spotify->obtain_token(code => $auth_res{code}, auto_refresh => 1); | ||
| 200 | $stream->write("Waiting for your Command!...\nCMD: "); | ||
| 201 | $loop->run; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | sub write_to_stream { | ||
| 206 | my $content = shift; | ||
| 207 | my $to_write = ''; | ||
| 208 | try { | ||
| 209 | # since we are Getting Net::Async::Spotify::Object here. | ||
| 210 | # need to implement to_hash there in order to be able to do this | ||
| 211 | #$to_write = encode_json_utf8($content); | ||
| 212 | if ( ref $content eq 'HASH' ) { | ||
| 213 | if ( exists $content->{content} and blessed $content->{content} and $content->{content}->can('to_hash') ) { | ||
| 214 | $to_write = $content->{content}->to_human; | ||
| 215 | } else { | ||
| 216 | $to_write = hash_to_string( $content ) . "\n"; | ||
| 217 | } | ||
| 218 | } else { | ||
| 219 | $to_write = $content if defined $content; | ||
| 220 | $to_write .= "\n"; | ||
| 221 | } | ||
| 222 | } catch ($e) { | ||
| 223 | $stream->write("Could not parse response. Error: $e\n"); | ||
| 224 | } | ||
| 225 | $stream->write(encode_utf8($to_write)."\nWaiting for your Command!...\nCMD: "); | ||
| 226 | } | ||
| 227 | |||
| 228 | =head1 COMMANDS | ||
| 229 | |||
| 230 | Available Commands: | ||
| 231 | |||
| 232 | =head2 CMD => p I<uri(optional)> I<device_id(optional)> | ||
| 233 | |||
| 234 | Play - Player -> start_a_users_playback | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | async sub p { | ||
| 239 | my @cmd_array = @_; | ||
| 240 | my $uri = $cmd_array[1]; | ||
| 241 | my $device_id = $cmd_array[2] || $spotify->api->player->{selected_device}->id; | ||
| 242 | my $r = await $spotify->api->player->start_a_users_playback( | ||
| 243 | $uri ? (uris => $uri) : (), | ||
| 244 | $device_id ? (device_id => $device_id) : () | ||
| 245 | ); | ||
| 246 | write_to_stream($r); | ||
| 247 | } | ||
| 248 | |||
| 249 | =head2 CMD => pu | ||
| 250 | |||
| 251 | Pause - Player -> pause_a_users_playback | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | async sub pu { | ||
| 256 | my $r = await $spotify->api->player->pause_a_users_playback(); | ||
| 257 | write_to_stream($r); | ||
| 258 | } | ||
| 259 | |||
| 260 | =head2 CMD => n | ||
| 261 | |||
| 262 | Next - Player -> skip_users_playback_to_next_track | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | async sub n { | ||
| 267 | await $spotify->api->player->skip_users_playback_to_next_track(); | ||
| 268 | my $r = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 269 | write_to_stream($r->{content}->to_human); | ||
| 270 | } | ||
| 271 | |||
| 272 | =head2 CMD => b | ||
| 273 | |||
| 274 | Previous - Player -> skip_users_playback_to_previous_track | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | async sub b { | ||
| 279 | my $r = await $spotify->api->player->skip_users_playback_to_previous_track(); | ||
| 280 | write_to_stream($r); | ||
| 281 | } | ||
| 282 | |||
| 283 | =head2 CMD => ff I<seconds> | ||
| 284 | |||
| 285 | FastForward the current playing track with the passed. | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | async sub ff { | ||
| 290 | my @cmd_array = @_; | ||
| 291 | |||
| 292 | my $seconds = 1000 * $cmd_array[1]; | ||
| 293 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 294 | my $r = await $spotify->api->player->seek_to_position_in_currently_playing_track( | ||
| 295 | position_ms => $seconds + $current->{content}->progress_ms | ||
| 296 | ); | ||
| 297 | write_to_stream($r); | ||
| 298 | } | ||
| 299 | |||
| 300 | =head2 CMD => set_s I<second> | ||
| 301 | |||
| 302 | Set the current playing track position to the passed second. | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | async sub set_s { | ||
| 307 | my @cmd_array = @_; | ||
| 308 | |||
| 309 | my $seconds = 1000 * $cmd_array[1]; | ||
| 310 | my $r = await $spotify->api->player->seek_to_position_in_currently_playing_track(position_ms => $seconds); | ||
| 311 | write_to_stream($r); | ||
| 312 | } | ||
| 313 | |||
| 314 | =head2 CMD => sh I<(true)|false> | ||
| 315 | |||
| 316 | Toggle Shuffle for current Playback context. | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | async sub sh { | ||
| 321 | my @cmd_array = @_; | ||
| 322 | |||
| 323 | my $state = $cmd_array[1] || 'true'; | ||
| 324 | my $r = await $spotify->api->player->toggle_shuffle_for_users_playback(state => $state); | ||
| 325 | write_to_stream($r); | ||
| 326 | } | ||
| 327 | |||
| 328 | =head2 CMD => c | ||
| 329 | |||
| 330 | Current Track - Player -> get_information_about_the_users_current_playback | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | async sub c { | ||
| 335 | my $r; | ||
| 336 | try { | ||
| 337 | $r = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 338 | write_to_stream($r->{content}->to_human); | ||
| 339 | } catch ($e) { | ||
| 340 | warn "ERROR " . hash_to_string($e); | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | =head2 CMD => d | ||
| 345 | |||
| 346 | Available Devices - Player -> get_a_users_available_devices | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | async sub d { | ||
| 351 | my $r = await $spotify->api->player->get_a_users_available_devices(); | ||
| 352 | write_to_stream($r->{content}->to_human); | ||
| 353 | } | ||
| 354 | |||
| 355 | =head2 CMD => d_select I<name|ID> I<1|(0)/play> | ||
| 356 | |||
| 357 | Selects a device, That will be used for Player API during this session. | ||
| 358 | |||
| 359 | =cut | ||
| 360 | |||
| 361 | async sub d_select { | ||
| 362 | my @cmd_array = @_; | ||
| 363 | try { | ||
| 364 | my $dvcs = await $spotify->api->player->get_a_users_available_devices(); | ||
| 365 | my @sel_d = grep { $_->name eq $cmd_array[1] or $_->id eq $cmd_array[1] } $dvcs->{content}->devices->@*; | ||
| 366 | $spotify->api->player->{selected_device} = $sel_d[0]; | ||
| 367 | await $spotify->api->player->start_a_users_playback(device_id => $sel_d[0]->id) if $cmd_array[2]; | ||
| 368 | } catch ($e) { | ||
| 369 | $log->warnf('error : %s', $e); | ||
| 370 | } | ||
| 371 | write_to_stream('OK'); | ||
| 372 | } | ||
| 373 | |||
| 374 | =head2 CMD => t I<device_id> | ||
| 375 | |||
| 376 | Transfer playback to device and start playing. | ||
| 377 | |||
| 378 | =cut | ||
| 379 | |||
| 380 | async sub t { | ||
| 381 | my @cmd_array = @_; | ||
| 382 | my $device_id = $cmd_array[1] || $spotify->api->player->{selected_device}->id; | ||
| 383 | my $r = await $spotify->api->player->transfer_a_users_playback(device_ids => $device_id, play => 'true'); | ||
| 384 | write_to_stream($r); | ||
| 385 | } | ||
| 386 | |||
| 387 | =head2 CMD => v I<volume_percent> | ||
| 388 | |||
| 389 | Sets the current active device's volume. | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | async sub v { | ||
| 394 | my @cmd_array = @_; | ||
| 395 | my $devices = await $spotify->api->player->get_a_users_available_devices(); | ||
| 396 | my $device_id; | ||
| 397 | for my $device ($devices->{content}{devices}->@*) { | ||
| 398 | if($device->{is_active}) { | ||
| 399 | $device_id = $device->{id}; | ||
| 400 | last; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | my $r = await $spotify->api->player->set_volume_for_users_playback(volume_percent => $cmd_array[1], device_id => $device_id); | ||
| 404 | write_to_stream($r); | ||
| 405 | } | ||
| 406 | |||
| 407 | =head2 CMD => l | ||
| 408 | |||
| 409 | Like the current playing song. | ||
| 410 | |||
| 411 | =cut | ||
| 412 | |||
| 413 | async sub l { | ||
| 414 | my @cmd_array = @_; | ||
| 415 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 416 | my $id = $current->{content}->item->id; | ||
| 417 | if ( $id ) { | ||
| 418 | my $r = await $spotify->api->library->save_tracks_user(ids => $id); | ||
| 419 | write_to_stream($r); | ||
| 420 | } else { | ||
| 421 | write_to_stream('Could not get Track ID'); | ||
| 422 | } | ||
| 423 | |||
| 424 | } | ||
| 425 | |||
| 426 | =head2 CMD => ul | ||
| 427 | |||
| 428 | Remove the current playing song from Liked. | ||
| 429 | |||
| 430 | =cut | ||
| 431 | |||
| 432 | async sub ul { | ||
| 433 | my @cmd_array = @_; | ||
| 434 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 435 | my $id = $current->{content}->item->id; | ||
| 436 | if ( $id ) { | ||
| 437 | my $r = await $spotify->api->library->remove_tracks_user(ids => $id); | ||
| 438 | write_to_stream($r); | ||
| 439 | } else { | ||
| 440 | write_to_stream('Could not get Track ID'); | ||
| 441 | } | ||
| 442 | |||
| 443 | } | ||
| 444 | |||
| 445 | =head2 CMD => f | ||
| 446 | |||
| 447 | Current track Audio Features. | ||
| 448 | |||
| 449 | =cut | ||
| 450 | |||
| 451 | async sub f { | ||
| 452 | my @cmd_array = @_; | ||
| 453 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 454 | my $id = $current->{content}->item->id; | ||
| 455 | if ( $id ) { | ||
| 456 | my $r = await $spotify->api->tracks->get_audio_features(id => $id); | ||
| 457 | write_to_stream($r->{content}->to_human); | ||
| 458 | } else { | ||
| 459 | write_to_stream('Could not get Track ID'); | ||
| 460 | } | ||
| 461 | } | ||
| 462 | |||
| 463 | =head2 CMD => fc_dance I<dance_level(0.6)> | ||
| 464 | |||
| 465 | Find in Current Contenxt Track with more than the passed danceablitiy leve. | ||
| 466 | |||
| 467 | =cut | ||
| 468 | |||
| 469 | async sub fc_dance { | ||
| 470 | my @cmd_array = @_; | ||
| 471 | |||
| 472 | my $current_features = async sub { | ||
| 473 | |||
| 474 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 475 | my $id = $current->{content}->item->id; | ||
| 476 | if ( $id ) { | ||
| 477 | my $r = await $spotify->api->tracks->get_audio_features(id => $id); | ||
| 478 | return $r; | ||
| 479 | } else { | ||
| 480 | return {error => 'Could not get Track ID'}; | ||
| 481 | } | ||
| 482 | }; | ||
| 483 | |||
| 484 | my $f = await $current_features->(); | ||
| 485 | my $c = 0; | ||
| 486 | my $danceability = $cmd_array[1] || 0.6; | ||
| 487 | while ( $f->{content}->danceability < $danceability ) { | ||
| 488 | $log->warnf('%d | Still less %s Next!', $c, $f->{content}->danceability); | ||
| 489 | await $spotify->api->player->skip_users_playback_to_next_track(); | ||
| 490 | $f = await $current_features->(); | ||
| 491 | } | ||
| 492 | $f->{count} = $c; | ||
| 493 | write_to_stream($f); | ||
| 494 | } | ||
| 495 | |||
| 496 | =head2 CMD => g | ||
| 497 | |||
| 498 | Get a list of available recommendation genres | ||
| 499 | |||
| 500 | =cut | ||
| 501 | |||
| 502 | async sub g { | ||
| 503 | my @cmd_array = @_; | ||
| 504 | |||
| 505 | my $r = await $spotify->api->browse->get_recommendation_genres(); | ||
| 506 | write_to_stream($r); | ||
| 507 | |||
| 508 | } | ||
| 509 | |||
| 510 | =head2 CMD => i I<(tracks)|artists> I<s|(m)|l> I<count(2)> I<offset(0)> | ||
| 511 | |||
| 512 | Get your top tracks | artists. where time_range => s:short_term, m:medium_term, l:long_term | ||
| 513 | |||
| 514 | =cut | ||
| 515 | |||
| 516 | async sub i { | ||
| 517 | my @cmd_array = @_; | ||
| 518 | |||
| 519 | my $type = $cmd_array[1] || 'tracks'; | ||
| 520 | my $time_range = $cmd_array[2] || 's'; | ||
| 521 | my $limit = $cmd_array[3] || 2; | ||
| 522 | my $level = $cmd_array[4] || 0; | ||
| 523 | my $range = 'medium_term'; | ||
| 524 | if ( $time_range eq 's' ) { | ||
| 525 | $range = 'short_term'; | ||
| 526 | } elsif ( $time_range eq 'l' ) { | ||
| 527 | $range = 'long_term'; | ||
| 528 | } | ||
| 529 | |||
| 530 | try { | ||
| 531 | my $r = await $spotify->api->personalization->get_users_top_artists_and_tracks( | ||
| 532 | type => $type, | ||
| 533 | time_range => $range, | ||
| 534 | limit => $limit, | ||
| 535 | offset => $level | ||
| 536 | ); | ||
| 537 | my $output; | ||
| 538 | $output .= $_->to_human ."\n" for $r->{content}->items->@*; | ||
| 539 | write_to_stream($output); | ||
| 540 | } catch ($e) { | ||
| 541 | $log->warnf('ddd %s', hash_to_string($e)) | ||
| 542 | } | ||
| 543 | } | ||
| 544 | |||
| 545 | =head2 CMD => seed_t I<ID|(current)> | ||
| 546 | |||
| 547 | add the track as a seed track (will keep latest selected 2 only) | ||
| 548 | |||
| 549 | =cut | ||
| 550 | |||
| 551 | async sub seed_t { | ||
| 552 | my @cmd_array = @_; | ||
| 553 | my $id = $cmd_array[1]; | ||
| 554 | unless ( defined $id ) { | ||
| 555 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 556 | $id = $current->{content}->item->id; | ||
| 557 | return {error => 'Could not get Track ID'} unless defined $id; | ||
| 558 | } | ||
| 559 | unshift $spotify->api->personalization->{seed_tracks}->@*, $id; | ||
| 560 | pop $spotify->api->personalization->{seed_tracks}->@* if scalar($spotify->api->personalization->{seed_tracks}->@*) > 2; | ||
| 561 | write_to_stream('OK '.hash_to_string($spotify->api->personalization->{seed_tracks})); | ||
| 562 | } | ||
| 563 | |||
| 564 | =head2 CMD => seed_a I<ID|(current)> | ||
| 565 | |||
| 566 | add the artist as a seed track (will keep latest selected 2 only) | ||
| 567 | |||
| 568 | =cut | ||
| 569 | |||
| 570 | async sub seed_a { | ||
| 571 | my @cmd_array = @_; | ||
| 572 | my $id = $cmd_array[1]; | ||
| 573 | unless ( defined $id ) { | ||
| 574 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 575 | $id = $current->{content}->item->artists->[0]->id; | ||
| 576 | return {error => 'Could not get Track ID'} unless defined $id; | ||
| 577 | } | ||
| 578 | unshift $spotify->api->personalization->{seed_artists}->@*, $id; | ||
| 579 | pop $spotify->api->personalization->{seed_artists}->@* if scalar($spotify->api->personalization->{seed_artists}->@*) > 2; | ||
| 580 | write_to_stream('OK'.hash_to_string($spotify->api->personalization->{seed_artists})); | ||
| 581 | } | ||
| 582 | |||
| 583 | =head2 CMD => rng I<genre(reggae)> I<(auto)|manual> I<acousticness> I<danceability> I<energy> I<instrumentalness> I<liveness> I<loudness> I<speechiness> I<tempo> I<valence> | ||
| 584 | |||
| 585 | Get you a random track based on passed Genre, Tracks and Artists used as seed, Auto for them to be your top tracks and artists. | ||
| 586 | and manual for selected seeded ones. Also accepts constrains on track features. | ||
| 587 | |||
| 588 | =cut | ||
| 589 | |||
| 590 | async sub rng { | ||
| 591 | my @cmd_array = @_; | ||
| 592 | |||
| 593 | my $genre = $cmd_array[1] || 'reggae'; | ||
| 594 | my $seed_source = $cmd_array[2] || 'auto'; | ||
| 595 | my ($tracks_ids, $artists_ids); | ||
| 596 | if ( $seed_source eq 'auto' ) { | ||
| 597 | my $top_tracks = await $spotify->api->personalization->get_users_top_artists_and_tracks(type => 'tracks', limit => 2, time_range => 'short_term'); | ||
| 598 | $tracks_ids = [ map { $_->id } $top_tracks->{content}->items->@*]; | ||
| 599 | my $top_artists = await $spotify->api->personalization->get_users_top_artists_and_tracks(type => 'artists', limit => 2, time_range => 'short_term'); | ||
| 600 | $artists_ids = [ map { $_->id } $top_artists->{content}->items->@*]; | ||
| 601 | } | ||
| 602 | $tracks_ids = $spotify->api->personalization->{seed_tracks} unless defined $tracks_ids; | ||
| 603 | $artists_ids = $spotify->api->personalization->{seed_artists} unless defined $artists_ids; | ||
| 604 | my %rec_args = ( | ||
| 605 | seed_tracks => $tracks_ids || [], | ||
| 606 | seed_artists => $artists_ids || [], | ||
| 607 | seed_genres => [$genre], | ||
| 608 | limit => 100, | ||
| 609 | ); | ||
| 610 | my %h; | ||
| 611 | @h{qw(acousticness danceability energy instrumentalness liveness loudness speechiness tempo valence)} = @cmd_array[3 .. 12]; | ||
| 612 | for my $k (keys %h) { | ||
| 613 | # zero value is skipped. | ||
| 614 | if (defined $h{$k} and $h{$k}) { | ||
| 615 | $rec_args{"target_$k"} = $h{$k}; | ||
| 616 | $rec_args{"max_$k"} = $h{$k} + 0.1; | ||
| 617 | $rec_args{"min_$k"} = $h{$k} - 0.1; | ||
| 618 | } | ||
| 619 | } | ||
| 620 | my $rng_rec = await $spotify->api->browse->get_recommendations(%rec_args); | ||
| 621 | my $suggested_ids = join ',', map { $_->id } $rng_rec->{content}->tracks->@*; | ||
| 622 | my $suggested_uris = join ',', map { $_->uri } $rng_rec->{content}->tracks->@*; | ||
| 623 | |||
| 624 | # Spotify API seems to be good enough with the accuracy. so do not re-check. | ||
| 625 | # my $playlist_id = $spotify->api->playlists->{selected_playlist}->id; | ||
| 626 | # await $spotify->api->playlists->add_tracks_to_playlist(playlist_id => $playlist_id, uris => $suggested_uris); | ||
| 627 | # my $t_af = await $spotify->api->tracks->get_several_audio_features(ids => $suggested_ids); | ||
| 628 | |||
| 629 | await $spotify->api->player->start_a_users_playback(uris => $suggested_uris); | ||
| 630 | |||
| 631 | my $r = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 632 | write_to_stream("Tracks found and currently in queue: " . scalar @$suggested_ids . " Tracks (max. 100)"); | ||
| 633 | write_to_stream($r->{content}->to_human); | ||
| 634 | } | ||
| 635 | |||
| 636 | =head2 CMD => me | ||
| 637 | |||
| 638 | Current user Info. | ||
| 639 | |||
| 640 | =cut | ||
| 641 | |||
| 642 | async sub me { | ||
| 643 | my @cmd_array = @_; | ||
| 644 | my $me = await $spotify->api->users->get_current_users_profile(); | ||
| 645 | write_to_stream($me); | ||
| 646 | } | ||
| 647 | |||
| 648 | =head2 CMD => s I<string> | ||
| 649 | |||
| 650 | Search for a track | ||
| 651 | |||
| 652 | =cut | ||
| 653 | |||
| 654 | async sub s { | ||
| 655 | my @cmd_array = @_; | ||
| 656 | shift @cmd_array; | ||
| 657 | |||
| 658 | try { | ||
| 659 | my $r = await $spotify->api->search->search(type => 'track', q => join(' ', @cmd_array)); | ||
| 660 | $log->warnf('dddddd %s', hash_to_string($r)); | ||
| 661 | } catch ($e) { | ||
| 662 | warn hash_to_string($e); | ||
| 663 | } | ||
| 664 | } | ||
| 665 | |||
| 666 | # Playlists | ||
| 667 | =head2 CMD => pl_ls | ||
| 668 | |||
| 669 | List user's playlists. | ||
| 670 | |||
| 671 | =cut | ||
| 672 | |||
| 673 | async sub pl_ls { | ||
| 674 | my @cmd_array = @_; | ||
| 675 | try { | ||
| 676 | my $pls = await $spotify->api->playlists->get_a_list_of_current_users_playlists(limit => 50); | ||
| 677 | my %r = map { $_->name => { id => $_->id, description => $_->description } } $pls->{content}->items->@*; | ||
| 678 | write_to_stream(hash_to_string(\%r)); | ||
| 679 | } catch ($e) { | ||
| 680 | warn hash_to_string($e); | ||
| 681 | } | ||
| 682 | } | ||
| 683 | |||
| 684 | =head2 CMD => pl_add I<playlist_name> I<public:(true)|false> I<"Description"> | ||
| 685 | |||
| 686 | Creates a new playlist for user. | ||
| 687 | |||
| 688 | =cut | ||
| 689 | |||
| 690 | async sub pl_add { | ||
| 691 | my @cmd_array = @_; | ||
| 692 | my $user_id = $cmd_array[4]; | ||
| 693 | unless ( defined $user_id ) { | ||
| 694 | my $me = await $spotify->api->users->get_current_users_profile(); | ||
| 695 | $user_id = $me->{content}->id; | ||
| 696 | } | ||
| 697 | my $r = await $spotify->api->playlists->create_playlist( | ||
| 698 | user_id => $user_id, | ||
| 699 | name => $cmd_array[1] || 'NoName', | ||
| 700 | public => $cmd_array[2] || 'true', | ||
| 701 | $cmd_array[3] ? (description => $cmd_array[3]) : () | ||
| 702 | ); | ||
| 703 | write_to_stream($r); | ||
| 704 | } | ||
| 705 | |||
| 706 | =head2 CMD => pl_select I<1|0(playit?)> I<name|ID> | ||
| 707 | |||
| 708 | Selects a playlist, passing a second parameter will play it too. | ||
| 709 | |||
| 710 | =cut | ||
| 711 | |||
| 712 | async sub pl_select { | ||
| 713 | my @cmd_array = @_; | ||
| 714 | shift @cmd_array; | ||
| 715 | my $play_it = shift @cmd_array; | ||
| 716 | my $name = join ' ', @cmd_array; | ||
| 717 | my $pl; | ||
| 718 | try { | ||
| 719 | my $pls = await $spotify->api->playlists->get_a_list_of_current_users_playlists(limit => 50); | ||
| 720 | my @sel_pl = grep { encode('UTF-8', $_->name) =~ m/$name/ or $_->id eq $name } $pls->{content}->items->@*; | ||
| 721 | $log->warnf('NAME: %s | %s', $name, \@sel_pl); | ||
| 722 | $pl = $spotify->api->playlists->{selected_playlist} = $sel_pl[0]; | ||
| 723 | await $spotify->api->player->start_a_users_playback(context_uri => $sel_pl[0]->uri) if $play_it; | ||
| 724 | } catch ($e) { | ||
| 725 | $log->warnf('error : %s', $e); | ||
| 726 | } | ||
| 727 | write_to_stream('OK : ' . $pl->name); | ||
| 728 | } | ||
| 729 | |||
| 730 | =head2 CMD => atp I<playlistID(default selected)> | ||
| 731 | |||
| 732 | Adds the current playing track to a playlist. | ||
| 733 | If no PlaylistID passed it will use selected. | ||
| 734 | |||
| 735 | =cut | ||
| 736 | |||
| 737 | async sub atp { | ||
| 738 | my @cmd_array = @_; | ||
| 739 | |||
| 740 | try { | ||
| 741 | my $playlist_id = $cmd_array[1] || $spotify->api->playlists->{selected_playlist}->id; | ||
| 742 | my $c = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 743 | my $d = $c->{content}->item->uri; | ||
| 744 | |||
| 745 | await $spotify->api->playlists->add_tracks_to_playlist(playlist_id => $playlist_id, uris => $d); | ||
| 746 | } catch ($e) { | ||
| 747 | $log->warnf('error %s', $e); | ||
| 748 | } | ||
| 749 | write_to_stream('OK'); | ||
| 750 | } | ||
| 751 | |||
| 752 | =head2 CMD => rfp I<playlistID(default selected)> | ||
| 753 | |||
| 754 | Removes the current playing track from a playlist. | ||
| 755 | If no PlaylistID passed it will use selected. | ||
| 756 | |||
| 757 | =cut | ||
| 758 | |||
| 759 | async sub rfp { | ||
| 760 | my @cmd_array = @_; | ||
| 761 | |||
| 762 | try { | ||
| 763 | my $playlist_id = $cmd_array[1] || $spotify->api->playlists->{selected_playlist}->id; | ||
| 764 | my $c = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 765 | my $d = $c->{content}->item->uri; | ||
| 766 | |||
| 767 | await $spotify->api->playlists->remove_tracks_playlist(playlist_id => $playlist_id, tracks => [{ uri => $d}] ); | ||
| 768 | } catch ($e) { | ||
| 769 | $log->warnf('error %s', $e); | ||
| 770 | } | ||
| 771 | write_to_stream('OK'); | ||
| 772 | } | ||
| 773 | |||
| 774 | =head2 CMD => I<api_name> I<method_name> I<%args> | ||
| 775 | |||
| 776 | Generic - Where it will take first argument as API name, second would be the method name. And whaterve comes after that would be | ||
| 777 | a key value arguments. | ||
| 778 | |||
| 779 | =cut | ||
| 780 | |||
| 781 | async sub generic { | ||
| 782 | my @cmd_array = @_; | ||
| 783 | my $r; | ||
| 784 | try{ | ||
| 785 | my $api_name = $cmd_array[0]; | ||
| 786 | my $api_cmd = $cmd_array[1]; | ||
| 787 | $r = await $spotify->api->$api_name->$api_cmd(splice @cmd_array, 2); | ||
| 788 | } catch ($e) { | ||
| 789 | $r = {fail => $e}; | ||
| 790 | } | ||
| 791 | write_to_stream($r); | ||
| 792 | } |