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

New module 'parser'

parent 253c6b67
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -13,12 +13,7 @@ use FindBin;
use lib "$FindBin::Bin/lib/";
use Logging;
use sites::youtube qw(youtube);
use sites::soundcloud qw(soundcloud);
use sites::mixcloud qw(mixcloud);
use sites::zippy qw(zippy);
use parser;
# nom du fichier
my $bot = $0;
......@@ -265,92 +260,25 @@ sub on_speak
{
my ($kernel, $user, $chan, $msg) = @_[KERNEL, ARG0, ARG1, ARG2];
my ($nick,$mask) = split(/!/,$user);
my $site;
my %content;
if ($msg =~ m#(^|[^!])https?://(www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)#) {
my $url = 'https://www.youtube.com/watch?v='.$3;
eval { %content = youtube($url) };
$site = 'youtube';
}
elsif ($msg =~ m#(^|[^!])https?://soundcloud.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)#) {
my $url = 'https://www.soundcloud.com/'.$2;
eval { %content = soundcloud($url) };
$site = 'soundcloud';
}
elsif ($msg =~ m#(^|[^!])https?://www.mixcloud.com/([a-zA-Z0-9-_]+/[a-zA-Z0-9-_]+)#) {
my $url = 'https://www.mixcloud.com/'.$2;
eval { %content = mixcloud($url) };
$site = 'mixcloud';
}
elsif ($msg =~ m#((^|[^!])http://www[0-9]+.zippyshare.com/v/[0-9]+/file.html)#) {
my $url = $1;
eval { %content = zippy($url) };
$site = 'zippyshare';
}
elsif ($msg =~ /^!fav( ([0-9]+))?/) {
my $id = ($2) ? $2 : $lastID;
my $sth = $dbh->prepare_cached('SELECT user FROM playbot_codes WHERE nick = ?');
$sth->execute($nick)
or $log->error("Couldn't finish transaction: " . $dbh->errstr);
unless ($sth->rows) {
$irc->yield(privmsg => $nick => "Ce nick n'est associé à aucun login arise. Va sur http://nightiies.iiens.net/links/fav pour obtenir ton code personel.");
return;
}
my $sth2 = $dbh->prepare_cached('INSERT INTO playbot_fav (id, user) VALUES (?, ?)');
$sth2->execute($id, $sth->fetch->[0])
or $log->error("Couldn't finish transaction: " . $dbh->errstr);
return;
}
elsif ($msg =~ /^!later(?: ([0-9]+))?(?: in ([0-9]*)?(h|m|s)?)?/) {
my ($id, $time, $unit) = ($1, $2, $3);
$id = $lastID if (!$id);
$time = 6 if (!$time);
$time *= ($unit eq 's') ? 1 : ($unit eq 'm') ? 60 : 3600;
$kernel->delay_set('_later', $time, $nick, $id);
return;
}
elsif ($msg =~ /^!tag( +([0-9]+))?/) {
my $id = ($2) ? $2 : $lastID;
while ($msg =~ /#([a-zA-Z0-9_-]+)/g) {
addTag($id, $1);
}
return;
}
elsif ($msg =~ /^!help/) {
$irc->yield(privmsg => $chan => '!fav [<id>] : enregistre la vidéo dans les favoris');
$irc->yield(privmsg => $chan => '!tag [<id>] <tag1> <tag2> ... : tag la vidéo');
$irc->yield(privmsg => $chan => '!later [<id>] [in <x>[s|m|h]] : vidéo rappelée par query (par défaut temps de 6h)');
$irc->yield(privmsg => $chan => 'Sans id précisée, la dernière vidéo postée est utilisée.');
return;
}
else {
return;
}
%content = parser::parse($msg);
if ($@) {
$log->warning ($@);
return;
}
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($site, $content{'url'}, $nick, $content{'author'}, $content{'title'}, $chan->[0])
$sth->execute($content{'site'}, $content{'url'}, $nick, $content{'author'}, $content{'title'}, $chan->[0])
or $log->error("Couldn't finish transaction: " . $dbh->errstr);
}
......@@ -387,6 +315,43 @@ sub on_speak
$irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}) ;
}
}
elsif ($msg =~ /^!fav( ([0-9]+))?/) {
my $id = ($2) ? $2 : $lastID;
my $sth = $dbh->prepare_cached('SELECT user FROM playbot_codes WHERE nick = ?');
$sth->execute($nick)
or $log->error("Couldn't finish transaction: " . $dbh->errstr);
if (!$sth->rows) {
$irc->yield(privmsg => $nick => "Ce nick n'est associé à aucun login arise. Va sur http://nightiies.iiens.net/links/fav pour obtenir ton code personel.");
}
else {
my $sth2 = $dbh->prepare_cached('INSERT INTO playbot_fav (id, user) VALUES (?, ?)');
$sth2->execute($id, $sth->fetch->[0])
or $log->error("Couldn't finish transaction: " . $dbh->errstr);
}
}
elsif ($msg =~ /^!later(?: ([0-9]+))?(?: in ([0-9]*)?(h|m|s)?)?/) {
my ($id, $time, $unit) = ($1, $2, $3);
$id = $lastID if (!$id);
$time = 6 if (!$time);
$time *= ($unit eq 's') ? 1 : ($unit eq 'm') ? 60 : 3600;
$kernel->delay_set('_later', $time, $nick, $id);
}
elsif ($msg =~ /^!tag( +([0-9]+))?/) {
my $id = ($2) ? $2 : $lastID;
while ($msg =~ /#([a-zA-Z0-9_-]+)/g) {
addTag($id, $1);
}
}
elsif ($msg =~ /^!help/) {
$irc->yield(privmsg => $chan => '!fav [<id>] : enregistre la vidéo dans les favoris');
$irc->yield(privmsg => $chan => '!tag [<id>] <tag1> <tag2> ... : tag la vidéo');
$irc->yield(privmsg => $chan => '!later [<id>] [in <x>[s|m|h]] : vidéo rappelée par query (par défaut temps de 6h)');
$irc->yield(privmsg => $chan => 'Sans id précisée, la dernière vidéo postée est utilisée.');
}
}
# Boucle des events
......
package parser;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(parse);
use lib "$FindBin::Bin/lib/sites/";
use youtube qw(youtube);
use soundcloud qw(soundcloud);
use mixcloud qw(mixcloud);
use zippy qw(zippy);
sub parse {
my $msg = @_[0];
my %content;
if ($msg =~ m#(^|[^!])https?://(www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)#) {
my $url = 'https://www.youtube.com/watch?v='.$3;
eval { %content = youtube($url) };
$content{'site'} = 'youtube';
}
elsif ($msg =~ m#(^|[^!])https?://soundcloud.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)#) {
my $url = 'https://www.soundcloud.com/'.$2;
eval { %content = soundcloud($url) };
$content{'site'} = 'soundcloud';
}
elsif ($msg =~ m#(^|[^!])https?://www.mixcloud.com/([a-zA-Z0-9-_]+/[a-zA-Z0-9-_]+)#) {
my $url = 'https://www.mixcloud.com/'.$2;
eval { %content = mixcloud($url) };
$content{'site'} = 'mixcloud';
}
elsif ($msg =~ m#((^|[^!])http://www[0-9]+.zippyshare.com/v/[0-9]+/file.html)#) {
my $url = $1;
eval { %content = zippy($url) };
$content{'site'} = 'zippyshare';
}
return %content;
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter