Sélectionner une révision Git
spotify.pm 2,02 Kio
package PlayBot::sites::spotify;
use strict;
use warnings;
use utf8;
use LWP::UserAgent;
use JSON;
use FindBin;
my $conf;
my $endpoint = "https://api.spotify.com/v1";
sub regex_playlist {
return
qr#(?:^|[^!])https?://(?:open|play).spotify.com/album/([a-zA-Z0-9]+)#;
}
sub regex {
return
qr#(?:^|[^!])https?://(?:open|play).spotify.com/track/([a-zA-Z0-9_-]+)#;
}
sub get_playlist {
shift;
my $id = shift;
my $ua = LWP::UserAgent->new(
timeout => 30,
env_proxy => 1,
);
my $response = $ua->get( $endpoint . '/albums/' . $id );
die( $response->status_line ) unless ( $response->is_success );
my $playlist = decode_json( $response->decoded_content );
my %infos;
$infos{'title'} = $playlist->{name};
$infos{'duration'} = 0;
$infos{'author'} =
join( ' ft. ', map { $_->{name} } @{ $playlist->{artists} } );
$infos{'site'} = 'spotify';
$infos{'url'} = 'https://open.spotify.com/album/' . $id;
$infos{'external_id'} = $playlist->{id};
my @urls;
foreach my $track ( @{ $playlist->{tracks}{items} } ) {
my $track_id = $track->{id};
push @urls, 'https://open.spotify.com/track/' . $track_id;
$infos{duration}++;
}
$infos{urls} = \@urls;
return %infos;
}
sub get {
shift;
my $id = shift;
my $ua = LWP::UserAgent->new(
timeout => 30,
env_proxy => 1,
);
my $response = $ua->get( $endpoint . '/tracks/' . $id );
die( $response->status_line ) unless ( $response->is_success );
my $content = decode_json( $response->decoded_content );
# die "video not found" if (not scalar @{ $content->{items} });