| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use utf8; | ||
| 7 | |||
| 8 | our $VERSION = '0.001'; # 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 Data::Dumper; | ||
| 76 | use JSON::MaybeUTF8 qw(:v1); | ||
| 77 | use Net::Async::HTTP::Server; | ||
| 78 | |||
| 79 | GetOptions( | ||
| 80 | 'c|client-id=s' => \(my $client_id = $ENV{client_id}), | ||
| 81 | 's|client-secret=s' => \(my $client_secret = $ENV{client_secret}), | ||
| 82 | 't|access-token=s' => \(my $access_token = $ENV{access_token}), | ||
| 83 | 'i|interactive' => \my $interactive, | ||
| 84 | 'w|web-server' => \my $webserver, | ||
| 85 | 'l|log-level=s' => \(my $log_level = 'info'), | ||
| 86 | 'h|help' => \my $help, | ||
| 87 | ); | ||
| 88 | |||
| 89 | require Log::Any::Adapter; | ||
| 90 | Log::Any::Adapter->set( qw(Stdout), log_level => $log_level ); | ||
| 91 | |||
| 92 | pod2usage( | ||
| 93 | { | ||
| 94 | -verbose => 99, | ||
| 95 | -sections => "NAME|SYNOPSIS|DESCRIPTION|OPTIONS|COMMANDS", | ||
| 96 | } | ||
| 97 | ) if $help; | ||
| 98 | |||
| 99 | die 'Need Spotify Client ID and Client Secret' unless ( $client_id and $client_secret ); | ||
| 100 | |||
| 101 | my $loop = IO::Async::Loop->new; | ||
| 102 | $loop->add( my $spotify = Net::Async::Spotify->new( | ||
| 103 | client_id => $client_id, | ||
| 104 | client_secret => $client_secret, | ||
| 105 | access_token => $access_token, | ||
| 106 | ) | ||
| 107 | ); | ||
| 108 | |||
| 109 | my $stream = IO::Async::Stream->new( | ||
| 110 | read_handle => \*STDIN, | ||
| 111 | write_handle => \*STDOUT, | ||
| 112 | on_read => sub { | ||
| 113 | my ( $self, $buffref, $eof ) = @_; | ||
| 114 | |||
| 115 | while( $$buffref =~ s/^(.*\n)// ) { | ||
| 116 | my $line = $1; | ||
| 117 | my @cmd_array = split ' ', $line; | ||
| 118 | my $command = $cmd_array[0]; | ||
| 119 | unless (defined $command) { | ||
| 120 | print "\n"; | ||
| 121 | pod2usage( | ||
| 122 | { | ||
| 123 | -verbose => 99, | ||
| 124 | -sections => "COMMANDS", | ||
| 125 | -exitval => 'NOEXIT', | ||
| 126 | } | ||
| 127 | ); | ||
| 128 | print "Waiting for your Command!...\nCMD: "; | ||
| 129 | return 0; | ||
| 130 | } | ||
| 131 | |||
| 132 | if (exists &{$command}) { | ||
| 133 | # for a happy strict pragma | ||
| 134 | my $method = \&{$command}; | ||
| 135 | $method->(@cmd_array)->retain; | ||
| 136 | } else { | ||
| 137 | generic->(@cmd_array)->retain; | ||
| 138 | } | ||
| 139 | |||
| 140 | } | ||
| 141 | return 0; | ||
| 142 | } | ||
| 143 | ); | ||
| 144 | $loop->add( $stream ); | ||
| 145 | |||
| 146 | my $callback_f = $loop->new_future(label => 'CallbackFuture'); | ||
| 147 | # Web server for incoming callback request | ||
| 148 | my $server = Net::Async::HTTP::Server->new( | ||
| 149 | on_request => sub { | ||
| 150 | my ( $self, $req ) = @_; | ||
| 151 | $log->tracef('Webserver receives %s %s: %s | %s', $req->method, $req->path, {$req->query_form}, $req->body); | ||
| 152 | if ( $req->method eq 'GET' and $req->path eq '/callback' ) { | ||
| 153 | # That's what we are waiting for. | ||
| 154 | my %params = $req->query_form; | ||
| 155 | $callback_f->done(%params) unless $callback_f->is_done; | ||
| 156 | |||
| 157 | my $response = HTTP::Response->new(200); | ||
| 158 | $response->add_content(encode_json_utf8({response => 'Got it! From spotify-cli.pl ;)'})); | ||
| 159 | $response->content_type("application/json"); | ||
| 160 | $response->content_length(length $response->content); | ||
| 161 | |||
| 162 | $req->respond($response); | ||
| 163 | } | ||
| 164 | }, | ||
| 165 | ); | ||
| 166 | $loop->add( $server ); | ||
| 167 | |||
| 168 | my $listner = await $server->listen( | ||
| 169 | addr => { | ||
| 170 | family => 'inet', | ||
| 171 | socktype => 'stream', | ||
| 172 | port => 80, | ||
| 173 | }, | ||
| 174 | ) if $webserver; | ||
| 175 | |||
| 176 | my %authorize = $spotify->authorize(scope => ['scopes'], show_dialog => 'false'); | ||
| 177 | |||
| 178 | $stream->write(sprintf("Your Authorize URI is (state: %s) :\n\n%s\n\n", $authorize{state}, $authorize{uri})); | ||
| 179 | |||
| 180 | if ( $interactive ) { | ||
| 181 | $stream->write("Please insert your `code` response in callback URL...\n"); | ||
| 182 | my ( $auth_code ) = await $stream->read_until( "\n" ); | ||
| 183 | chomp $auth_code; | ||
| 184 | |||
| 185 | $stream->write("Please insert your `state` response in callback URL...\n"); | ||
| 186 | my ( $auth_state ) = await $stream->read_until( "\n" ); | ||
| 187 | chomp $auth_state; | ||
| 188 | |||
| 189 | await $spotify->obtain_token(code => $auth_code, auto_refresh => 1); | ||
| 190 | $stream->write("Waiting for your Command!...\nCMD: "); | ||
| 191 | $loop->run; | ||
| 192 | } elsif ( $webserver ) { | ||
| 193 | my %auth_res = await $callback_f; | ||
| 194 | if ( $auth_res{state} eq $authorize{state} ) { | ||
| 195 | await $spotify->obtain_token(code => $auth_res{code}, auto_refresh => 1); | ||
| 196 | $stream->write("Waiting for your Command!...\nCMD: "); | ||
| 197 | $loop->run; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | sub write_to_stream { | ||
| 202 | my $content = shift; | ||
| 203 | my $to_write = ''; | ||
| 204 | try { | ||
| 205 | # since we are Getting Net::Async::Spotify::Object here. | ||
| 206 | # need to implement to_hash there in order to be able to do this | ||
| 207 | #$to_write = encode_json_utf8($content); | ||
| 208 | $to_write = Dumper($content); | ||
| 209 | } catch ($e) { | ||
| 210 | $stream->write("Could not parse response. Error: $e\n"); | ||
| 211 | } | ||
| 212 | $stream->write($to_write."\nWaiting for your Command!...\nCMD: "); | ||
| 213 | } | ||
| 214 | |||
| 215 | =head1 COMMANDS | ||
| 216 | |||
| 217 | Available Commands: | ||
| 218 | |||
| 219 | =head2 CMD => p | ||
| 220 | |||
| 221 | Play - Player -> start_a_users_playback | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | async sub p { | ||
| 226 | my $r = await $spotify->api->player->start_a_users_playback(); | ||
| 227 | write_to_stream($r); | ||
| 228 | } | ||
| 229 | |||
| 230 | =head2 CMD => pu | ||
| 231 | |||
| 232 | Pause - Player -> pause_a_users_playback | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | async sub pu { | ||
| 237 | $stream->write("Pausing Player...\n"); | ||
| 238 | my $r = await $spotify->api->player->pause_a_users_playback(); | ||
| 239 | write_to_stream($r); | ||
| 240 | } | ||
| 241 | |||
| 242 | =head2 CMD => n | ||
| 243 | |||
| 244 | Next - Player -> skip_users_playback_to_next_track | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | async sub n { | ||
| 249 | $stream->write("NEXT! :D\n"); | ||
| 250 | my $r = await $spotify->api->player->skip_users_playback_to_next_track(); | ||
| 251 | write_to_stream($r); | ||
| 252 | } | ||
| 253 | |||
| 254 | =head2 CMD => b | ||
| 255 | |||
| 256 | Previous - Player -> skip_users_playback_to_previous_track | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | async sub b { | ||
| 261 | my $r = await $spotify->api->player->skip_users_playback_to_previous_track(); | ||
| 262 | write_to_stream($r); | ||
| 263 | } | ||
| 264 | |||
| 265 | =head2 CMD => c | ||
| 266 | |||
| 267 | Current Track - Player -> get_information_about_the_users_current_playback | ||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | async sub c { | ||
| 272 | my $r = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 273 | write_to_stream($r); | ||
| 274 | } | ||
| 275 | |||
| 276 | =head2 CMD => d | ||
| 277 | |||
| 278 | Available Devices - Player -> get_a_users_available_devices | ||
| 279 | |||
| 280 | =cut | ||
| 281 | |||
| 282 | async sub d { | ||
| 283 | my $r = await $spotify->api->player->get_a_users_available_devices(); | ||
| 284 | write_to_stream($r); | ||
| 285 | } | ||
| 286 | |||
| 287 | =head2 CMD => t I<device_id> | ||
| 288 | |||
| 289 | Transfer playback to device and start playing. | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | async sub t { | ||
| 294 | my @cmd_array = @_; | ||
| 295 | my $r = await $spotify->api->player->transfer_a_users_playback(device_ids => $cmd_array[1], play => 'true'); | ||
| 296 | write_to_stream($r); | ||
| 297 | } | ||
| 298 | |||
| 299 | =head2 CMD => v I<volume_percent> | ||
| 300 | |||
| 301 | Sets the current active device's volume. | ||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | async sub v { | ||
| 306 | my @cmd_array = @_; | ||
| 307 | my $devices = await $spotify->api->player->get_a_users_available_devices(); | ||
| 308 | my $device_id; | ||
| 309 | for my $device ($devices->{content}{devices}->@*) { | ||
| 310 | if($device->{is_active}) { | ||
| 311 | $device_id = $device->{id}; | ||
| 312 | last; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | my $r = await $spotify->api->player->set_volume_for_users_playback(volume_percent => $cmd_array[1], device_id => $device_id); | ||
| 316 | write_to_stream($r); | ||
| 317 | } | ||
| 318 | |||
| 319 | =head2 CMD => l | ||
| 320 | |||
| 321 | Like the current playing song. | ||
| 322 | |||
| 323 | =cut | ||
| 324 | |||
| 325 | async sub l { | ||
| 326 | my @cmd_array = @_; | ||
| 327 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 328 | my $id = $current->{content}->data->{item}{id}; | ||
| 329 | if ( $id ) { | ||
| 330 | my $r = await $spotify->api->library->save_tracks_user(ids => $id); | ||
| 331 | write_to_stream($r); | ||
| 332 | } else { | ||
| 333 | write_to_stream('Could not get Track ID'); | ||
| 334 | } | ||
| 335 | |||
| 336 | } | ||
| 337 | |||
| 338 | =head2 CMD => ul | ||
| 339 | |||
| 340 | Remove the current playing song from Liked. | ||
| 341 | |||
| 342 | =cut | ||
| 343 | |||
| 344 | async sub ul { | ||
| 345 | my @cmd_array = @_; | ||
| 346 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 347 | my $id = $current->{content}->data->{item}{id}; | ||
| 348 | if ( $id ) { | ||
| 349 | my $r = await $spotify->api->library->remove_tracks_user(ids => $id); | ||
| 350 | write_to_stream($r); | ||
| 351 | } else { | ||
| 352 | write_to_stream('Could not get Track ID'); | ||
| 353 | } | ||
| 354 | |||
| 355 | } | ||
| 356 | |||
| 357 | =head2 CMD => f | ||
| 358 | |||
| 359 | Current track Audio Features. | ||
| 360 | |||
| 361 | =cut | ||
| 362 | |||
| 363 | async sub f { | ||
| 364 | my @cmd_array = @_; | ||
| 365 | my $current = await $spotify->api->player->get_information_about_the_users_current_playback(); | ||
| 366 | my $id = $current->{content}->data->{item}{id}; | ||
| 367 | if ( $id ) { | ||
| 368 | my $r = await $spotify->api->tracks->get_audio_features(id => $id); | ||
| 369 | write_to_stream($r); | ||
| 370 | } else { | ||
| 371 | write_to_stream('Could not get Track ID'); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | |||
| 375 | =head2 CMD => I<api_name> I<method_name> I<%args> | ||
| 376 | |||
| 377 | Generic - Where it will take first argument as API name, second would be the method name. And whaterve comes after that would be | ||
| 378 | a key value arguments. | ||
| 379 | |||
| 380 | =cut | ||
| 381 | |||
| 382 | async sub generic { | ||
| 383 | my @cmd_array = @_; | ||
| 384 | my $r; | ||
| 385 | try{ | ||
| 386 | my $api_name = $cmd_array[0]; | ||
| 387 | my $api_cmd = $cmd_array[1]; | ||
| 388 | $r = await $spotify->api->$api_name->$api_cmd(splice @cmd_array, 2); | ||
| 389 | } catch ($e) { | ||
| 390 | $r = {fail => $e}; | ||
| 391 | } | ||
| 392 | write_to_stream($r); | ||
| 393 | } |