From c3b7a585be97a9dbcaeacaf50ec81f66bbf77f46 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@cervoi.se>
Date: Sun, 15 May 2016 14:52:41 +0200
Subject: [PATCH] Add spotify albums support

---
 PlayBot/sites/spotify.pm | 43 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/PlayBot/sites/spotify.pm b/PlayBot/sites/spotify.pm
index b10e282..93e4044 100644
--- a/PlayBot/sites/spotify.pm
+++ b/PlayBot/sites/spotify.pm
@@ -9,13 +9,51 @@ use JSON;
 use FindBin;
 
 my $conf;
-my $endpoint = "https://api.spotify.com/v1/tracks/";
+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;
@@ -24,8 +62,7 @@ sub get {
         timeout     => 30,
         env_proxy   => 1,
     );
-	my $response = $ua->get($endpoint
-        .$id);
+	my $response = $ua->get($endpoint.'/tracks/'.$id);
 	die($response->status_line) unless ($response->is_success);
 
 	my $content = decode_json($response->decoded_content);
-- 
GitLab