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

plugin system

parent 74a2e27d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -13,7 +13,7 @@ use Module::Reload; ...@@ -13,7 +13,7 @@ use Module::Reload;
use PlayBot::utils::Logging; use PlayBot::utils::Logging;
use PlayBot::sessions::irc::later; use PlayBot::sessions::irc::later;
use PlayBot::sites::parser; use PlayBot::sites;
use PlayBot::commands::parser; use PlayBot::commands::parser;
my $log = PlayBot::utils::Logging->new('STDOUT', 1); my $log = PlayBot::utils::Logging->new('STDOUT', 1);
...@@ -116,8 +116,8 @@ sub setConf ...@@ -116,8 +116,8 @@ sub setConf
{ {
PlayBot::commands::parser::setConf($irc, $dbh, $log, \%lastID); PlayBot::commands::parser::setConf($irc, $dbh, $log, \%lastID);
$PlayBot::sites::parser::irc = $irc; $PlayBot::sites::irc = $irc;
$PlayBot::sites::parser::log = $log; $PlayBot::sites::log = $log;
} }
...@@ -256,7 +256,7 @@ sub on_speak ...@@ -256,7 +256,7 @@ sub on_speak
my %content; my %content;
# first we check for url # first we check for url
my @processed_str = PlayBot::sites::parser::parse(@args); my @processed_str = PlayBot::sites::parse(@args);
# we remove any part processed (url and tags) # we remove any part processed (url and tags)
foreach (@processed_str) { foreach (@processed_str) {
......
package PlayBot::sites::parser; package PlayBot::sites;
use strict; use strict;
use warnings; use warnings;
use PlayBot::sites::youtube; use Module::Pluggable sub_name => 'sites', search_path => ['PlayBot::sites'], require => 1;
use PlayBot::sites::soundcloud;
use PlayBot::sites::mixcloud;
use PlayBot::sites::zippy;
use PlayBot::sites::dailymotion;
use PlayBot::utils::db; use PlayBot::utils::db;
use PlayBot::utils::print; use PlayBot::utils::print;
...@@ -15,6 +11,11 @@ use PlayBot::commands::parser; ...@@ -15,6 +11,11 @@ use PlayBot::commands::parser;
our $irc; our $irc;
our $log; our $log;
our $regex;
sub regex {
return $regex;
}
sub parse { sub parse {
my ($kernel, $user, $chan, $msg) = @_; my ($kernel, $user, $chan, $msg) = @_;
...@@ -25,33 +26,13 @@ sub parse { ...@@ -25,33 +26,13 @@ sub parse {
my $dbh = PlayBot::utils::db::main_session(); my $dbh = PlayBot::utils::db::main_session();
# parsing # parsing
if ($msg =~ m#(?:^|[^!])https?://(?:www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)#) { foreach my $site (__PACKAGE__->sites)
eval { %content = PlayBot::sites::youtube::get($1) }; {
if (my @args = ($msg =~ $site->regex))
$content{'site'} = 'youtube'; {
$content{'url'} = 'https://www.youtube.com/watch?v='.$1; eval { %content = $site->get(@args) };
} last;
elsif ($msg =~ m#(?:^|[^!])https?://(?:www\.)?soundcloud.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)(?:\?.+)?#) { }
my $url = 'https://www.soundcloud.com/'.$1;
eval { %content = PlayBot::sites::soundcloud::get($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 = PlayBot::sites::mixcloud::get($url) };
$content{'site'} = 'mixcloud';
}
elsif ($msg =~ m#((^|[^!])http://www[0-9]+.zippyshare.com/v/[0-9]+/file.html)#) {
my $url = $1;
eval { %content = PlayBot::sites::zippy::get($url) };
$content{'site'} = 'zippyshare';
}
elsif ($msg =~ m#(?:^|[^!])https?://www.dailymotion.com/video/([a-z0-9]+)#) {
eval { %content = PlayBot::sites::dailymotion::get($1) };
$content{'site'} = 'dailymotion';
$content{'url'} = 'https://www.dailymotion.com/video/' . $1;
} }
my $matching_url = $&; my $matching_url = $&;
......
...@@ -5,7 +5,15 @@ use warnings; ...@@ -5,7 +5,15 @@ use warnings;
use Inline Python => 'DATA'; use Inline Python => 'DATA';
sub regex
{
return qr#(?:^|[^!])https?://www.dailymotion.com/video/([a-z0-9]+)#;
}
sub get { sub get {
shift;
my $id = shift; my $id = shift;
my $content = weboob_get($id); my $content = weboob_get($id);
...@@ -13,6 +21,9 @@ sub get { ...@@ -13,6 +21,9 @@ sub get {
my ($h, $m, $s) = ($content->{'duration'} =~ /(.):(..):(..)/); my ($h, $m, $s) = ($content->{'duration'} =~ /(.):(..):(..)/);
$content->{'duration'} = $h * 3600 + $m * 60 + $s; $content->{'duration'} = $h * 3600 + $m * 60 + $s;
$content->{site} = 'dailymotion';
$content->{'url'} = 'https://www.dailymotion.com/video/'.$id;
return %{$content}; return %{$content};
} }
......
...@@ -7,9 +7,17 @@ use LWP::UserAgent; ...@@ -7,9 +7,17 @@ use LWP::UserAgent;
use JSON; use JSON;
use Encode; use Encode;
sub regex
{
return qr#(^|[^!])https?://www.mixcloud.com/([a-zA-Z0-9-_]+/[a-zA-Z0-9-_]+)#;
}
sub get { sub get {
my ($url) = @_; shift;
$url =~ s/www/api/; my $id = shift;
my $url = 'https://api.mixcloud.com/'.$2;
my $ua = LWP::UserAgent->new( my $ua = LWP::UserAgent->new(
timeout => 30, timeout => 30,
......
...@@ -11,8 +11,16 @@ my $root = 'http://api.soundcloud.com'; ...@@ -11,8 +11,16 @@ my $root = 'http://api.soundcloud.com';
my $clientId = 'f4956716fe1a9dc9c3725af822963365'; my $clientId = 'f4956716fe1a9dc9c3725af822963365';
sub regex
{
return qr#(?:^|[^!])https?://(?:www\.)?soundcloud.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)(?:\?.+)?#;
}
sub get { sub get {
my ($url) = @_; shift;
my $id = shift;
my $url = 'https://www.soundcloud.com/'.$id;
my $ua = LWP::UserAgent->new( my $ua = LWP::UserAgent->new(
timeout => 30, timeout => 30,
...@@ -24,9 +32,10 @@ sub get { ...@@ -24,9 +32,10 @@ sub get {
my $content = decode_json($response->decoded_content); my $content = decode_json($response->decoded_content);
my $infos = { my $infos = {
title => $content->{'title'},
author => $content->{'user'}->{'username'}, author => $content->{'user'}->{'username'},
duration => $content->{'duration'} / 1000, duration => $content->{'duration'} / 1000,
site => 'soundcloud',
title => $content->{'title'},
url => $url, url => $url,
}; };
......
...@@ -18,7 +18,14 @@ BEGIN { ...@@ -18,7 +18,14 @@ BEGIN {
$conf = decode_json($json); $conf = decode_json($json);
} }
sub regex
{
return qr#(?:^|[^!])https?://(?:www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)#;
}
sub get { sub get {
shift;
my $id = shift; my $id = shift;
my $ua = LWP::UserAgent->new( my $ua = LWP::UserAgent->new(
...@@ -51,6 +58,8 @@ sub get { ...@@ -51,6 +58,8 @@ sub get {
$infos{'duration'} = $h*3600 + $m*60 + $s - 1; $infos{'duration'} = $h*3600 + $m*60 + $s - 1;
$infos{'author'} = $video->{'snippet'}->{'channelTitle'}; $infos{'author'} = $video->{'snippet'}->{'channelTitle'};
$infos{'site'} = 'youtube';
$infos{'url'} = 'https://www.youtube.com/watch?v='.$id;
return %infos; return %infos;
} }
......
...@@ -12,8 +12,15 @@ my $inAuthor = 0; ...@@ -12,8 +12,15 @@ my $inAuthor = 0;
my %infos; my %infos;
sub regex
{
return qr#((^|[^!])http://www[0-9]+.zippyshare.com/v/[0-9]+/file.html)#;
}
sub get { sub get {
my ($url) = @_; shift;
my $url = shift;
my $ua = LWP::UserAgent->new( my $ua = LWP::UserAgent->new(
agent => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1", agent => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 GTB7.1",
...@@ -37,6 +44,7 @@ sub get { ...@@ -37,6 +44,7 @@ sub get {
$infos{'url'} = $url; $infos{'url'} = $url;
$infos{'author'} = undef; $infos{'author'} = undef;
$infos{'site'} = 'zippyshare';
return %infos; return %infos;
} }
......
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