Skip to content
Extraits de code Groupes Projets
Valider ed0e56ba rédigé par Alexandre Morignot's avatar Alexandre Morignot
Parcourir les fichiers

prepare support for playlists

parent 2ac328d1
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -4,6 +4,7 @@ use strict; ...@@ -4,6 +4,7 @@ use strict;
use warnings; use warnings;
use Module::Pluggable sub_name => 'sites', search_path => ['PlayBot::sites'], require => 1; use Module::Pluggable sub_name => 'sites', search_path => ['PlayBot::sites'], require => 1;
use Storable qw(dclone);
use PlayBot::utils::db; use PlayBot::utils::db;
use PlayBot::utils::db::chan; use PlayBot::utils::db::chan;
...@@ -31,10 +32,19 @@ sub parse { ...@@ -31,10 +32,19 @@ sub parse {
{ {
not grep { $site eq "PlayBot::sites::$_" } @{ $chan_conf->sites } and next; 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) }; eval { %content = $site->get(@args) };
$matching_url = $&; $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; last;
} }
} }
...@@ -45,9 +55,20 @@ sub parse { ...@@ -45,9 +55,20 @@ sub parse {
return; return;
} }
my $id;
# if we get a new content, we must save it # if we get a new content, we must save it
if (%content) { 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 ( return (
...@@ -58,7 +79,7 @@ sub parse { ...@@ -58,7 +79,7 @@ sub parse {
sub insert_content sub insert_content
{ {
my ($kernel, $nick, $chan, $content, $msg) = @_; my ($kernel, $nick, $chan, $content, $msg, $playlist) = @_;
my $dbh = PlayBot::utils::db::main_session(); my $dbh = PlayBot::utils::db::main_session();
my $id; my $id;
...@@ -68,8 +89,8 @@ sub insert_content ...@@ -68,8 +89,8 @@ sub insert_content
# insertion de la vidéo dans la bdd # insertion de la vidéo dans la bdd
eval { eval {
my $sth = $dbh->prepare(' my $sth = $dbh->prepare('
INSERT INTO playbot (type, url, sender, title, duration) INSERT INTO playbot (type, url, sender, title, duration, playlist)
VALUES (?,?,?,?,?) VALUES (?,?,?,?,?,?)
'); ');
$log->error("Couldn't prepare querie; aborting") unless (defined $sth); $log->error("Couldn't prepare querie; aborting") unless (defined $sth);
...@@ -78,7 +99,8 @@ sub insert_content ...@@ -78,7 +99,8 @@ sub insert_content
$content->{'url'}, $content->{'url'},
$content->{'author'}, $content->{'author'},
$content->{'title'}, $content->{'title'},
$content->{'duration'} $content->{'duration'},
$content->{'playlist'},
); );
}; };
if ($@) { if ($@) {
...@@ -111,18 +133,39 @@ sub insert_content ...@@ -111,18 +133,39 @@ sub insert_content
$id = $sth->fetch->[0]; $id = $sth->fetch->[0];
# insertion du chan if (defined($playlist))
$sth = $dbh->prepare(' {
INSERT INTO playbot_chan (content, chan, sender_irc) # save track in the playlist
VALUES (?,?,?)'); $sth = $dbh->prepare('
$log->error("Couldn't prepare querie; aborting") unless (defined $sth); 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) $sth->execute($id, $chan, $nick)
or $log->error("Couldn't finish transaction: " . $dbh->errstr); 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; my @tags;
# get tags # get tags
...@@ -151,10 +194,13 @@ sub insert_content ...@@ -151,10 +194,13 @@ sub insert_content
$content->{'tags'} = \@tags; $content->{'tags'} = \@tags;
delete $content->{'url'}; delete $content->{'url'};
# message sur irc if (not $playlist)
$irc->yield(privmsg => $chan => PlayBot::utils::print::print($content)); {
# message sur irc
$irc->yield(privmsg => $chan => PlayBot::utils::print::print($content));
}
return @matching_tags; return $content->{id}, @matching_tags;
} }
1; 1;
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter