diff --git a/PlayBot/sites.pm b/PlayBot/sites.pm index 3cf9d9a93de25f48cc1d37af1f18e8efe4d5a3bb..6edfd96e22f0f971ac6ce66cd9c724c51a296961 100644 --- a/PlayBot/sites.pm +++ b/PlayBot/sites.pm @@ -16,14 +16,12 @@ our $regex; sub parse { - my ($kernel, $user, $chan, $msg) = @_; + my ($kernel, $user, $chan, $msg, $playlist) = @_; my ($nick,$mask) = split(/!/,$user); my $chan_conf = PlayBot::utils::db::chan->new($chan); my %content; - my $id; - my $dbh = PlayBot::utils::db::main_session(); my $matching_url; my @matching_tags; @@ -49,103 +47,114 @@ sub parse { # if we get a new content, we must save it if (%content) { - # we assume it's a new content - my $new = 1; - - # insertion de la vidéo dans la bdd - eval { - my $sth = $dbh->prepare(' - INSERT INTO playbot (type, url, sender, title, duration) - VALUES (?,?,?,?,?) - '); - $log->error("Couldn't prepare querie; aborting") unless (defined $sth); - - $sth->execute( - $content{'site'}, - $content{'url'}, - $content{'author'}, - $content{'title'}, - $content{'duration'} - ); - }; - if ($@) { - # seems to be already present in database - $new = 0; - - my $sth = $dbh->prepare(' - UPDATE playbot playbot SET - sender = ?, - title = ?, - duration = ? - WHERE url = ? - '); - $log->error("Couldn't prepare querie; aborting") unless (defined $sth); - - $sth->execute( - $content{'author'}, - $content{'title'}, - $content{'duration'}, - $content{'url'}, - ); - } + @matching_tags = insert_content($kernel, $nick, $chan, \%content, $msg); + } - # sélection de l'id de la vidéo insérée - my $sth = $dbh->prepare('SELECT id FROM playbot WHERE url = ?'); - $log->error("Couldn't prepare querie; aborting") unless (defined $sth); + return ( + $matching_url, + @matching_tags + ); +} - $sth->execute($content{'url'}) - or $log->error("Couldn't finish transaction: " . $dbh->errstr); +sub insert_content +{ + my ($kernel, $nick, $chan, $content, $msg) = @_; + my $dbh = PlayBot::utils::db::main_session(); + my $id; - $id = $sth->fetch->[0]; + # we assume it's a new content + my $new = 1; - # insertion du chan - $sth = $dbh->prepare(' - INSERT INTO playbot_chan (content, chan, sender_irc) - VALUES (?,?,?)'); + # insertion de la vidéo dans la bdd + eval { + my $sth = $dbh->prepare(' + INSERT INTO playbot (type, url, sender, title, duration) + VALUES (?,?,?,?,?) + '); + $log->error("Couldn't prepare querie; aborting") unless (defined $sth); + + $sth->execute( + $content->{'site'}, + $content->{'url'}, + $content->{'author'}, + $content->{'title'}, + $content->{'duration'} + ); + }; + if ($@) { + # seems to be already present in database + $new = 0; + + my $sth = $dbh->prepare(' + UPDATE playbot playbot SET + sender = ?, + title = ?, + duration = ? + WHERE url = ? + '); $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( + $content->{'author'}, + $content->{'title'}, + $content->{'duration'}, + $content->{'url'}, + ); + } - $dbh->commit; + # sélection de l'id de la vidéo insérée + my $sth = $dbh->prepare('SELECT id FROM playbot WHERE url = ?'); + $log->error("Couldn't prepare querie; aborting") unless (defined $sth); - @matching_tags = PlayBot::commands::parser::tag($msg, $chan); + $sth->execute($content->{'url'}) + or $log->error("Couldn't finish transaction: " . $dbh->errstr); - my @tags; - # get tags - $sth = $dbh->prepare("select tag - from playbot_tags - where id = ? - "); - $sth->execute($id); + $id = $sth->fetch->[0]; - while (my $data = $sth->fetch) { - my $tag = $data->[0]; - $tag =~ s/([a-zA-Z0-9_-]+)/#$1/; - push @tags, $tag; - } - $dbh->commit; + # 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 ($new) { - # schedule download - $kernel->post('downloader' => 'ddl' => $id, $content{url}); + $sth->execute($id, $chan, $nick) + or $log->error("Couldn't finish transaction: " . $dbh->errstr); - $content{'id'} = '+'.$id; - } - else { - $content{'id'} = $id; - } - $content{'tags'} = \@tags; - delete $content{'url'}; + $dbh->commit; - # message sur irc - $irc->yield(privmsg => $chan => PlayBot::utils::print::print(\%content)); + my @matching_tags = PlayBot::commands::parser::tag($msg, $chan); + + my @tags; + # get tags + $sth = $dbh->prepare("select tag + from playbot_tags + where id = ? + "); + $sth->execute($id); + + while (my $data = $sth->fetch) { + my $tag = $data->[0]; + $tag =~ s/([a-zA-Z0-9_-]+)/#$1/; + push @tags, $tag; } + $dbh->commit; - return ( - $matching_url, - @matching_tags - ); + if ($new) { + # schedule download + $kernel->post('downloader' => 'ddl' => $id, $content->{url}); + + $content->{'id'} = '+'.$id; + } + else { + $content->{'id'} = $id; + } + $content->{'tags'} = \@tags; + delete $content->{'url'}; + + # message sur irc + $irc->yield(privmsg => $chan => PlayBot::utils::print::print($content)); + + return @matching_tags; } 1;