diff --git a/PlayBot.pl b/PlayBot.pl index 2bc1d5ececf3745088efd6f197602f6f7ff19fd4..6f57ca9bd60bf20b4881cb60c9ffda16036b52c7 100755 --- a/PlayBot.pl +++ b/PlayBot.pl @@ -128,6 +128,10 @@ sub cycle commands::parser::setConf($irc, $dbh, $log); %commands::parser::lastID = %lastID; + + $sites::parser::dbh = $dbh; + $sites::parser::irc = $irc; + $sites::parser::log = $log; } @@ -254,51 +258,18 @@ sub on_speak my ($nick,$mask) = split(/!/,$user); my %content; + # first we test if it's a command if (!commands::parser::exec(@args)) { - %content = sites::parser::parse($msg); - - if ($@) { - $log->warning ($@); - return; - } + # if not, maybe there is an url we can parse + my $id = sites::parser::parse(@args); - if (%content) { - if ($debug) { - $log->debug($content{'url'}); - } - else { - # insertion de la vidéo dans la bdd - my $sth = $dbh->prepare_cached('INSERT INTO playbot (date, type, url, sender_irc, sender, title, chan) VALUES (NOW(),?,?,?,?,?,?)'); - $log->error("Couldn't prepare querie; aborting") unless (defined $sth); - - $sth->execute($content{'site'}, $content{'url'}, $nick, $content{'author'}, $content{'title'}, $chan->[0]) - or $log->error("Couldn't finish transaction: " . $dbh->errstr); - } - - # sélection de l'id de la vidéo insérée - my $id = $dbh->{mysql_insert_id}; - if (!$id) { - my $sth = $dbh->prepare_cached('SELECT id FROM playbot WHERE url = ?'); - $log->error("Couldn't prepare querie; aborting") unless (defined $sth); - - $sth->execute($content{'url'}) - or $log->error("Couldn't finish transaction: " . $dbh->errstr); - - $id = $sth->fetch->[0]; - } + if ($id) { + # if yes, we need to save the new content's id $lastID{$chan->[0]} = $id; $commands::parser::lastID{$chan->[0]} = $id; - # insertion des éventuels tags + # we insert the potiential tags commands::parser::tag($msg, $chan); - - # message sur irc - if (defined $content{'author'}) { - $irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}.' | '.$content{'author'}) ; - } - else { - $irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}) ; - } } } } @@ -306,6 +277,10 @@ sub on_speak commands::parser::setConf($irc, $dbh, $log); +$sites::parser::dbh = $dbh; +$sites::parser::irc = $irc; +$sites::parser::log = $log; + # Boucle des events $poe_kernel->run(); exit 0; diff --git a/lib/sites/parser.pm b/lib/sites/parser.pm index 4c78328cc4e5ed63d13eadb6979e991939f2c7c2..21114ea22b0b7b87c6f8cdf0a66a54afa28719c4 100644 --- a/lib/sites/parser.pm +++ b/lib/sites/parser.pm @@ -10,11 +10,18 @@ use soundcloud; use mixcloud; use zippy; +our $irc; +our $dbh; +our $log; sub parse { - my $msg = shift; + my ($kernel, $user, $chan, $msg) = @_; + my ($nick,$mask) = split(/!/,$user); + my %content; + my $id; + # parsing if ($msg =~ m#(?:^|[^!])https?://(?:www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)#) { eval { %content = youtube::get($1) }; @@ -37,5 +44,48 @@ sub parse { $content{'site'} = 'zippyshare'; } - return %content; + # something goes wrong ? + if ($@) { + $log->warning ($@); + return; + } + + # if we get a new content, we must save it + if (%content) { + if ($debug) { + $log->debug($content{'url'}); + } + else { + # insertion de la vidéo dans la bdd + my $sth = $dbh->prepare_cached('INSERT INTO playbot (date, type, url, sender_irc, sender, title, chan) VALUES (NOW(),?,?,?,?,?,?)'); + $log->error("Couldn't prepare querie; aborting") unless (defined $sth); + + $sth->execute($content{'site'}, $content{'url'}, $nick, $content{'author'}, $content{'title'}, $chan->[0]) + or $log->error("Couldn't finish transaction: " . $dbh->errstr); + } + + # sélection de l'id de la vidéo insérée + $id = $dbh->{mysql_insert_id}; + if (!$id) { + my $sth = $dbh->prepare_cached('SELECT id FROM playbot WHERE url = ?'); + $log->error("Couldn't prepare querie; aborting") unless (defined $sth); + + $sth->execute($content{'url'}) + or $log->error("Couldn't finish transaction: " . $dbh->errstr); + + $id = $sth->fetch->[0]; + } + + # message sur irc + if (defined $content{'author'}) { + $irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}.' | '.$content{'author'}) ; + } + else { + $irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}) ; + } + } + + return $id; } + +1;