From ed0e56ba1381a414b314c437cb7aadb103feed72 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@cervoi.se>
Date: Tue, 29 Mar 2016 22:28:48 +0200
Subject: [PATCH] prepare support for playlists

---
 PlayBot/sites.pm | 82 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 64 insertions(+), 18 deletions(-)

diff --git a/PlayBot/sites.pm b/PlayBot/sites.pm
index 6edfd96..369fa06 100644
--- a/PlayBot/sites.pm
+++ b/PlayBot/sites.pm
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 
 use Module::Pluggable sub_name => 'sites', search_path => ['PlayBot::sites'], require => 1;
+use Storable qw(dclone);
 
 use PlayBot::utils::db;
 use PlayBot::utils::db::chan;
@@ -31,10 +32,19 @@ sub parse {
     {
         not grep { $site eq "PlayBot::sites::$_" } @{ $chan_conf->sites } and next;
 
-        if (my @args = ($msg =~ $site->regex))
+        my @args;
+        if (@args = ($msg =~ $site->regex))
         {
 		    eval { %content = $site->get(@args) };
             $matching_url = $&;
+            $content{playlist} = 0;
+            last;
+        }
+        elsif ($site->can('regex_playlist') and @args = ($msg =~ $site->regex_playlist))
+        {
+            eval { %content = $site->get_playlist(@args) };
+            $matching_url = $&;
+            $content{playlist} = 1;
             last;
         }
     }
@@ -45,9 +55,20 @@ sub parse {
         return;
     }
 
+    my $id;
     # if we get a new content, we must save it
     if (%content) {
-        @matching_tags = insert_content($kernel, $nick, $chan, \%content, $msg);
+        ($id, @matching_tags) = insert_content($kernel, $nick, $chan, dclone(\%content), $msg, $playlist);
+
+        if (not $playlist and $content{playlist})
+        {
+            foreach my $url (@{ $content{urls} })
+            {
+                my $new_msg = $msg;
+                $new_msg =~ s/\Q$matching_url\E/$url/;
+                parse($kernel, $user, $chan, $new_msg, $id);
+            }
+        }
     }
 
     return (
@@ -58,7 +79,7 @@ sub parse {
 
 sub insert_content
 {
-    my ($kernel, $nick, $chan, $content, $msg) = @_;
+    my ($kernel, $nick, $chan, $content, $msg, $playlist) = @_;
     my $dbh = PlayBot::utils::db::main_session();
     my $id;
 
@@ -68,8 +89,8 @@ sub insert_content
 	# insertion de la vidéo dans la bdd
     eval {
         my $sth = $dbh->prepare('
-            INSERT INTO playbot (type, url, sender, title, duration)
-            VALUES (?,?,?,?,?)
+            INSERT INTO playbot (type, url, sender, title, duration, playlist)
+            VALUES (?,?,?,?,?,?)
         ');
 		$log->error("Couldn't prepare querie; aborting") unless (defined $sth);
 
@@ -78,7 +99,8 @@ sub insert_content
             $content->{'url'},
             $content->{'author'},
             $content->{'title'},
-            $content->{'duration'}
+            $content->{'duration'},
+            $content->{'playlist'},
         );
     };
     if ($@) {
@@ -111,18 +133,39 @@ sub insert_content
 
 	$id = $sth->fetch->[0];
 
-    # insertion du chan
-    $sth = $dbh->prepare('
-        INSERT INTO playbot_chan (content, chan, sender_irc)
-        VALUES (?,?,?)');
-	$log->error("Couldn't prepare querie; aborting") unless (defined $sth);
+    if (defined($playlist))
+    {
+        # save track in the playlist
+        $sth = $dbh->prepare('
+            INSERT INTO playbot_playlist_content_association (playlist_id, content_id)
+            VALUES (?,?)');
+	    $log->error("Couldn't prepare querie; aborting") unless (defined $sth);
+
+        eval {
+            $sth->execute($playlist, $id);
+            $dbh->commit;
+        };
+        if ($@)
+        {
+            # the association already exists
+            $dbh->rollback;
+        }
+    }
+    else
+    {
+        # insertion du chan
+        $sth = $dbh->prepare('
+            INSERT INTO playbot_chan (content, chan, sender_irc)
+            VALUES (?,?,?)');
+	    $log->error("Couldn't prepare querie; aborting") unless (defined $sth);
 
-    $sth->execute($id, $chan, $nick)
-        or $log->error("Couldn't finish transaction: " . $dbh->errstr);
+        $sth->execute($id, $chan, $nick)
+            or $log->error("Couldn't finish transaction: " . $dbh->errstr);
 
-    $dbh->commit;
+        $dbh->commit;
+    }
 
-    my @matching_tags = PlayBot::commands::parser::tag($msg, $chan);
+    my @matching_tags = PlayBot::commands::parser::tag($msg, $chan, $id);
 
     my @tags;
     # get tags
@@ -151,10 +194,13 @@ sub insert_content
     $content->{'tags'} = \@tags;
     delete $content->{'url'};
 
-    # message sur irc
-	$irc->yield(privmsg => $chan => PlayBot::utils::print::print($content));
+    if (not $playlist)
+    {
+        # message sur irc
+	    $irc->yield(privmsg => $chan => PlayBot::utils::print::print($content));
+    }
 
-    return @matching_tags;
+    return $content->{id}, @matching_tags;
 }
 
 1;
-- 
GitLab