Skip to content
Extraits de code Groupes Projets

Comparer les révisions

Les modifications sont affichées comme si la révision source était fusionnée avec la révision cible. En savoir plus sur la comparaison des révisions.

Source

Sélectionner le projet cible
No results found
Sélectionner une révision Git
Loading items

Cible

Sélectionner le projet cible
  • morignot2011/playbot.old
  • defrance2011/playbot
2 résultats
Sélectionner une révision Git
Loading items
Afficher les modifications
......@@ -18,22 +18,18 @@ our $clientSecret;
my $access_token = '';
my $access_token_time;
sub regex_playlist
{
return qr#(?:^|[^!])https?://(?:www\.)?soundcloud.com/([a-zA-Z0-9_-]+/sets/[a-zA-Z0-9_-]+)(?:\?.+)?#;
sub regex_playlist {
return
qr#(?:^|[^!])https?://(?:www\.)?soundcloud.com/([a-zA-Z0-9_-]+/sets/[a-zA-Z0-9_-]+)(?:\?.+)?#;
}
sub regex
{
return qr#(?:^|[^!])https?://(?:www\.)?soundcloud.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)(?:\?.+)?#;
sub regex {
return
qr#(?:^|[^!])https?://(?:www\.)?soundcloud.com/([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+)(?:\?.+)?#;
}
sub get_access_token
{
if ($access_token ne '' && time() - $access_token_time <= 3000)
{
sub get_access_token {
if ( $access_token ne '' && time() - $access_token_time <= 3000 ) {
return $access_token;
}
......@@ -43,9 +39,12 @@ sub get_access_token
timeout => 30,
env_proxy => 1,
);
my $response = $ua->post(
$root.'/oauth2/token?grant_type=client_credentials&client_id='.$clientId.'&client_secret='.$clientSecret,
);
my $response =
$ua->post( $root
. '/oauth2/token?grant_type=client_credentials&client_id='
. $clientId
. '&client_secret='
. $clientSecret, );
die( $response->status_line ) unless ( $response->is_success );
my $content = decode_json( $response->decoded_content );
......@@ -55,7 +54,6 @@ sub get_access_token
return $access_token;
}
sub get_playlist {
shift;
my $id = shift;
......@@ -66,14 +64,14 @@ sub get_playlist {
);
my $response;
if (looks_like_number($id))
{
$response = $ua->get($root.'/playlists/'.$id.'?client_id='.$clientId);
if ( looks_like_number($id) ) {
$response =
$ua->get( $root . '/playlists/' . $id . '?client_id=' . $clientId );
}
else
{
else {
my $url = 'https://www.soundcloud.com/' . $id;
$response = $ua->get($root.'/resolve.json?url='.$url.'&client_id='.$clientId);
$response = $ua->get(
$root . '/resolve.json?url=' . $url . '&client_id=' . $clientId );
}
die( $response->status_line ) unless ( $response->is_success );
......@@ -88,8 +86,7 @@ sub get_playlist {
};
my @urls;
foreach my $item (@{ $content->{'tracks'} })
{
foreach my $item ( @{ $content->{'tracks'} } ) {
push @urls, $item->{'permalink_url'};
}
......@@ -97,7 +94,6 @@ sub get_playlist {
return %$infos;
}
sub get {
shift;
my $id = shift;
......@@ -106,7 +102,8 @@ sub get {
$log->info( "the id is " . $id );
my $content = `./soundcloud $id`;
my ($external_id, $url, $title, $author, $duration) = split(/\n/, $content);
my ( $external_id, $url, $title, $author, $duration ) =
split( /\n/, $content );
=cut
my $ua = LWP::UserAgent->new(
......
......@@ -11,14 +11,14 @@ use FindBin;
my $conf;
my $endpoint = "https://api.spotify.com/v1";
sub regex_playlist
{
return qr#(?:^|[^!])https?://(?:open|play).spotify.com/album/([a-zA-Z0-9]+)#;
sub regex_playlist {
return
qr#(?:^|[^!])https?://(?:open|play).spotify.com/album/([a-zA-Z0-9]+)#;
}
sub regex
{
return qr#(?:^|[^!])https?://(?:open|play).spotify.com/track/([a-zA-Z0-9_-]+)#;
sub regex {
return
qr#(?:^|[^!])https?://(?:open|play).spotify.com/track/([a-zA-Z0-9_-]+)#;
}
sub get_playlist {
......@@ -37,14 +37,14 @@ sub get_playlist {
$infos{'title'} = $playlist->{name};
$infos{'duration'} = 0;
$infos{'author'} = join(' ft. ', map { $_->{name} } @{ $playlist->{artists} });
$infos{'author'} =
join( ' ft. ', map { $_->{name} } @{ $playlist->{artists} } );
$infos{'site'} = 'spotify';
$infos{'url'} = 'https://open.spotify.com/album/' . $id;
$infos{'external_id'} = $playlist->{id};
my @urls;
foreach my $track (@{ $playlist->{tracks}{items} })
{
foreach my $track ( @{ $playlist->{tracks}{items} } ) {
my $track_id = $track->{id};
push @urls, 'https://open.spotify.com/track/' . $track_id;
$infos{duration}++;
......
......@@ -19,18 +19,17 @@ BEGIN {
$conf = decode_json($json);
}
sub regex_playlist
{
return qr#(?:^|[^!])https?://www.youtube.com/playlist\?[a-zA-Z0-9_=&-]*list=([a-zA-Z0-9_-]+)#;
sub regex_playlist {
return
qr#(?:^|[^!])https?://www.youtube.com/playlist\?[a-zA-Z0-9_=&-]*list=([a-zA-Z0-9_-]+)#;
}
sub regex
{
return qr#(?:^|[^!])https?://(?:www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)#;
sub regex {
return
qr#(?:^|[^!])https?://(?:www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)#;
}
sub get_playlist
{
sub get_playlist {
shift;
my $id = shift;
......@@ -38,9 +37,11 @@ sub get_playlist
timeout => 30,
env_proxy => 1,
);
my $response = $ua->get($endpoint
.'/playlists?id='.$id
.'&key='.$conf->{'youtube_api_key'}
my $response =
$ua->get( $endpoint
. '/playlists?id='
. $id . '&key='
. $conf->{'youtube_api_key'}
. '&part=snippet,contentDetails' );
die( $response->status_line ) unless ( $response->is_success );
......@@ -57,9 +58,11 @@ sub get_playlist
$infos{'site'} = 'youtube';
$infos{'url'} = 'https://www.youtube.com/playlist?list=' . $id;
$response = $ua->get($endpoint
.'/playlistItems?playlistId='.$id
.'&key='.$conf->{'youtube_api_key'}
$response =
$ua->get( $endpoint
. '/playlistItems?playlistId='
. $id . '&key='
. $conf->{'youtube_api_key'}
. '&part=snippet' );
die( $response->status_line ) unless ( $response->is_success );
......@@ -68,8 +71,7 @@ sub get_playlist
die "empty playlist" if ( not scalar @{ $content->{items} } );
my @urls;
foreach my $subcontent (@{ $content->{items} })
{
foreach my $subcontent ( @{ $content->{items} } ) {
my $subid = $subcontent->{snippet}{resourceId}{videoId};
push @urls, 'https://www.youtube.com/watch?v=' . $subid;
}
......@@ -78,8 +80,7 @@ sub get_playlist
return %infos;
}
sub get
{
sub get {
shift;
my $id = shift;
......@@ -87,9 +88,11 @@ sub get
timeout => 30,
env_proxy => 1,
);
my $response = $ua->get($endpoint
.'/videos?id='.$id
.'&key='.$conf->{'youtube_api_key'}
my $response =
$ua->get( $endpoint
. '/videos?id='
. $id . '&key='
. $conf->{'youtube_api_key'}
. '&part=snippet,contentDetails' );
die( $response->status_line ) unless ( $response->is_success );
......@@ -102,7 +105,8 @@ sub get
$infos{'title'} = $video->{'snippet'}->{'title'};
$video->{'contentDetails'}->{'duration'} =~ /PT((?<h>\d+)H)?((?<m>\d+)M)?((?<s>\d+)S)?/;
$video->{'contentDetails'}->{'duration'} =~
/PT((?<h>\d+)H)?((?<m>\d+)M)?((?<s>\d+)S)?/;
# default values
my ($h) = $+{'h'} || 0;
......
......@@ -12,19 +12,17 @@ my $inTitle = 0;
my $inAuthor = 0;
my %infos;
sub regex
{
sub regex {
return qr#((^|[^!])http://www[0-9]+.zippyshare.com/v/[0-9]+/file.html)#;
}
sub get {
shift;
my $url = shift;
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",
timeout => 30,
env_proxy => 1,
);
......@@ -50,8 +48,7 @@ sub get {
return %infos;
}
sub parser_text
{
sub parser_text {
my ($text) = @_;
chomp $text;
$text = decode_entities($text);
......@@ -63,14 +60,12 @@ sub parser_text
}
}
sub parser_start
{
sub parser_start {
my ($tag) = @_;
$inTitle = 1 if ( $tag eq 'title' );
}
sub parser_end
{
sub parser_end {
my ($tag) = @_;
$inTitle = 0 if ( $tag eq 'title' );
}
......
......@@ -11,13 +11,11 @@ use utf8;
use Fcntl ':mode';
# ###
# new
# Instancie la classe - un constructeur en somme
# ###
sub new
{
sub new {
my $class = shift;
my $self = {
_file => shift,
......@@ -31,95 +29,78 @@ sub new
# On active la couleur que si on est sur un terminal
# C'est moche après pour les fichiers ou un less
if($self->file eq "STDOUT")
{
if ( $self->file eq "STDOUT" ) {
$self->{"_colored"} = 1 if ( ( stat(STDOUT) )[2] & S_IFCHR );
}
elsif($self->file eq "STDERR")
{
elsif ( $self->file eq "STDERR" ) {
$self->{"_colored"} = 1 if ( ( stat(STDERR) )[2] & S_IFCHR );
}
else
{
else {
$self->{"_colored"} = 1 if ( ( stat( $self->file ) )[2] & S_IFCHR );
}
unless(defined $self->{"_right_align"})
{
unless ( defined $self->{"_right_align"} ) {
$self->{"_right_align"} = 0;
}
return $self;
} # Fin new
# ###
# file
# Renvoie/maj $self->{'_file'}
# ###
sub file
{
sub file {
my $self = shift;
$self->{"_file"} = $_[0] if ( defined( $_[0] ) );
return $self->{"_file"};
} # Fin file
# ###
# colored
# Renvoie/maj $self->{'_colored'}
# ###
sub colored
{
sub colored {
my $self = shift;
$self->{"_colored"} = $_[0] if ( defined( $_[0] ) );
return $self->{"_colored"};
} # Fin colored
# ###
# pending
# Renvoie/maj $self->{'_pending'}
# ###
sub pending
{
sub pending {
my $self = shift;
$self->{"_pending"} = $_[0] if ( defined( $_[0] ) );
return $self->{"_pending"};
} # Fin pending
# ###
# utf8
# Renvoie/maj $self->{"_utf8"}
# ###
sub utf8
{
sub utf8 {
my $self = shift;
$self->{"_utf8"} = $_[0] if ( defined( $_[0] ) );
return $self->{"_utf8"};
} # Fin utf8
# ###
# right_align
# Renvoie/maj $self->{"_right_align"}
# ###
sub right_align
{
sub right_align {
my $self = shift;
$self->{"_right_align"} = $_[0] if ( defined( $_[0] ) );
return $self->{"_right_align"};
} # Fin right_align
# ###
# debug
# Fonction gérant les logs du niveau debug (1)
# ###
sub debug
{
sub debug {
my ( $self, $text ) = @_;
return unless ( defined $text );
......@@ -128,50 +109,42 @@ sub debug
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[36mDEBUG\e[0m:$parent:$text";
}
else
{
else {
$text = "DEBUG:$parent:$text";
}
return $self->print_in_file( $text, $self->DEBUG );
} # Fin debug
# ###
# pending_debug
# Fonction gérant les logs du niveau debug (1)
# ###
sub pending_debug
{
sub pending_debug {
my ( $self, $text ) = @_;
chomp $text;
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[36mDEBUG\e[0m:$parent:$text";
}
else
{
else {
$text = "DEBUG:$parent:$text";
}
return $self->pending_in_file( $text, $self->DEBUG );
} # Fin pending_debug
# ###
# info
# Fonction gérant les logs du niveau info (2)
# ###
sub info
{
sub info {
my ( $self, $text ) = @_;
return unless ( defined $text );
......@@ -180,25 +153,21 @@ sub info
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[33;1mINFO\e[0m:$parent:$text";
}
else
{
else {
$text = "INFO:$parent:$text";
}
return $self->print_in_file( $text, $self->INFO );
} # Fin info
# ###
# pending_info
# Fonction gérant les logs du niveau info (2)
# ###
sub pending_info
{
sub pending_info {
my ( $self, $text ) = @_;
return unless ( defined $text );
......@@ -207,25 +176,21 @@ sub pending_info
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[33;1mINFO\e[0m:$parent:$text";
}
else
{
else {
$text = "INFO:$parent:$text";
}
return $self->pending_in_file( $text, $self->INFO );
} # Fin pending_info
# ###
# warning
# Fonction gérant les logs du niveau warning (3)
# ###
sub warning
{
sub warning {
my ( $self, $text ) = @_;
return unless ( defined $text );
......@@ -234,25 +199,21 @@ sub warning
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[33mWARNING\e[0m:$parent:$text";
}
else
{
else {
$text = "WARNING:$parent:$text";
}
return $self->print_in_file( $text, $self->WARNING );
} # Fin warning
# ###
# pending_warning
# Fonction gérant les logs du niveau warning (3)
# ###
sub pending_warning
{
sub pending_warning {
my ( $self, $text ) = @_;
return unless ( defined $text );
......@@ -261,25 +222,21 @@ sub pending_warning
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[33mWARNING\e[0m:$parent:$text";
}
else
{
else {
$text = "WARNING:$parent:$text";
}
return $self->pending_in_file( $text, $self->WARNING );
} # Fin pending_warning
# ###
# error
# Fonction gérant les logs du niveau error (4)
# ###
sub error
{
sub error {
my ( $self, $text ) = @_;
return unless ( defined $text );
......@@ -288,25 +245,21 @@ sub error
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[31mERROR\e[0m:$parent:$text";
}
else
{
else {
$text = "ERROR:$parent:$text";
}
return $self->print_in_file( $text, $self->ERROR );
} # Fin error
# ###
# pending_error
# Fonction gérant les logs du niveau error (4)
# ###
sub pending_error
{
sub pending_error {
my ( $self, $text ) = @_;
return unless ( defined $text );
......@@ -315,25 +268,21 @@ sub pending_error
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[31mERROR\e[0m:$parent:$text";
}
else
{
else {
$text = "ERROR:$parent:$text";
}
return $self->pending_in_file( $text, $self->ERROR );
} # Fin pending_error
# ###
# critical
# Fonction gérant les logs du niveau critical (5)
# ###
sub critical
{
sub critical {
my ( $self, $text ) = @_;
return unless ( defined $text );
......@@ -342,25 +291,21 @@ sub critical
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[31;1mCRITICAL\e[0m:$parent:$text";
}
else
{
else {
$text = "CRITICAL:$parent:$text";
}
return $self->print_in_file( $text, $self->CRITICAL );
} # Fin critical
# ###
# pending_critical
# Fonction gérant les logs du niveau critical (5)
# ###
sub pending_critical
{
sub pending_critical {
my ( $self, $text ) = @_;
return unless ( defined $text );
......@@ -369,25 +314,21 @@ sub pending_critical
my $parent = ( caller(1) )[3];
$parent = "" if ( !defined($parent) );
if($self->colored)
{
if ( $self->colored ) {
$text = "\e[31;1mCRITICAL\e[0m:$parent:$text";
}
else
{
else {
$text = "CRITICAL:$parent:$text";
}
return $self->pending_in_file( $text, $self->CRITICAL );
} # Fin pending_critical
# ###
# print_in_file
# Écrit dans le fichier
# ###
sub print_in_file
{
sub print_in_file {
my ( $self, $text, $level ) = @_;
return unless ( defined $text );
......@@ -396,16 +337,13 @@ sub print_in_file
$self->end_pending( 0, $level ) if ( $self->pending );
if($self->file eq "STDOUT")
{
if ( $self->file eq "STDOUT" ) {
print STDOUT $text . "\n";
}
elsif($self->file eq "STDERR")
{
elsif ( $self->file eq "STDERR" ) {
print STDERR $text . "\n";
}
else
{
else {
open LOG, ">>", $self->file or return 0;
print LOG $text . "\n";
close LOG;
......@@ -416,53 +354,43 @@ sub print_in_file
return 1;
} # Fin print_in_file
# ###
# pending_in_file
# Écrit dans le fichier en attendant de savoir si ça a réussi ou pas
# ###
sub pending_in_file
{
sub pending_in_file {
my ( $self, $text, $level ) = @_;
return unless ( defined $text );
chomp $text;
$text = "[" . ( scalar localtime time ) . "] $text";
if($self->file eq "STDOUT")
{
if($self->right_align)
{
if ( $self->file eq "STDOUT" ) {
if ( $self->right_align ) {
printf STDOUT "%-90s", $text;
}
else
{
else {
print STDOUT $text;
}
}
elsif($self->file eq "STDERR")
{
if($self->right_align)
{
elsif ( $self->file eq "STDERR" ) {
if ( $self->right_align ) {
printf STDERR "%-90s", $text;
}
else
{
else {
print STDERR $text;
}
}
else
{
if($self->right_align)
{
else {
if ( $self->right_align ) {
open LOG, ">>", $self->file or return 0;
printf LOG "%-90s", $text;
close LOG;
printf "%-90s", $text if(defined($level) && $Config::debug >= $level);
printf "%-90s", $text
if ( defined($level) && $Config::debug >= $level );
}
else
{
else {
open LOG, ">>", $self->file or return 0;
print LOG $text;
close LOG;
......@@ -476,38 +404,30 @@ sub pending_in_file
return 1;
} # Fin pending_in_file
# ###
# end_pending
# Écrit dans le fichier le résultat de l'attente
# ###
sub end_pending
{
sub end_pending {
my ( $self, $done_or_error, $level ) = @_;
my $done = "";
if($done_or_error)
{
if($self->colored)
{
if ($done_or_error) {
if ( $self->colored ) {
$done = sprintf "%c[32m Done ", 0x1B;
}
else
{
else {
$done = sprintf " Done \n";
}
$done .= "" if ( $self->utf8 );
}
else
{
if($self->colored)
{
else {
if ( $self->colored ) {
$done = sprintf "%c[31m Error ", 0x1B;
}
else
{
else {
$done = sprintf " Error \n";
}
......@@ -516,18 +436,14 @@ sub end_pending
$done .= "\e[0m\n" if ( $self->colored );
# À partir d'ici, $done peut vouloir dire que c'est bon, ou pas
if($self->file eq "STDOUT")
{
if ( $self->file eq "STDOUT" ) {
printf STDOUT $done;
}
elsif($self->file eq "STDERR")
{
elsif ( $self->file eq "STDERR" ) {
printf STDERR $done;
}
else
{
else {
open LOG, ">>", $self->file or return 0;
printf LOG $done;
close LOG;
......@@ -535,13 +451,11 @@ sub end_pending
printf $done if ( defined($level) && $Config::debug >= $level );
}
$self->pending(0);
return 1;
} # Fin end_pending
#
# Fonctions pour récupérer les différents niveaux de debug
#
......@@ -550,6 +464,7 @@ sub INFO { return 2; }
sub WARNING { return 1; }
sub ERROR { return 0; }
sub CRITICAL { return -1; }
# Pour ceux qui préfèrent utiliser des variables...
our $DEBUG = 3;
our $INFO = 2;
......@@ -565,8 +480,7 @@ our %LVL_NAME = (
);
# Et dans l'autre sens
sub LVL
{
sub LVL {
my ( $self, $num ) = @_;
my %LVL = (
......@@ -587,20 +501,17 @@ our %LVL = (
"-1" => "CRITICAL"
);
# ###
# dbg
# Imprime des infos de debug à l'écran (STDOUT)
# ###
sub dbg
{
sub dbg {
my $self = shift;
require Data::Dumper;
print Data::Dumper->Dump( [$self], [qw(Logging)] );
} # Fin dbg
1;
__END__
......
......@@ -25,11 +25,19 @@ sub main_session {
}
sub get_new_session {
my $dbh = DBI->connect('DBI:mysql:'.$conf->{'bdd'}.';host='.$conf->{'host'}.';mysql_enable_utf8mb4=1', $conf->{'user'}, $conf->{'passwd'}, {
my $dbh = DBI->connect(
'DBI:mysql:'
. $conf->{'bdd'}
. ';host='
. $conf->{'host'}
. ';mysql_enable_utf8mb4=1',
$conf->{'user'},
$conf->{'passwd'},
{
RaiseError => 1,
AutoCommit => 0
})
or die("Couldn't connect to database: ".DBI->errstr);
}
) or die( "Couldn't connect to database: " . DBI->errstr );
#$dbh->{'mysql_enable_utf8mb4'} = 1;
#$dbh->do(qq{SET NAMES utf8;});
......
......@@ -9,13 +9,10 @@ use Moose;
use PlayBot::utils::db;
has 'name' => ( is => 'ro', isa => 'Str' );
has 'sites' => ( is => 'ro', isa => 'ArrayRef[Str]', writer => '_set_sites' );
around 'BUILDARGS' => sub
{
around 'BUILDARGS' => sub {
my ( $orig, $class, $name ) = @_;
not $name and die "missing arg 'name'";
......@@ -31,15 +28,11 @@ around 'BUILDARGS' => sub
my $sites;
my $row = $sth->fetch;
if (not $row)
{
$sites = ['dailymotion',
'mixcloud',
'soundcloud',
'youtube',
'spotify',
'deezer',
'facebook'];
if ( not $row ) {
$sites = [
'dailymotion', 'mixcloud', 'soundcloud', 'youtube',
'spotify', 'deezer', 'facebook'
];
$sth = $dbh->prepare( '
insert into playbot_config (name, sites)
......@@ -47,12 +40,8 @@ around 'BUILDARGS' => sub
' );
$sth->execute( $name, encode_json($sites) );
}
else
{
eval
{
$sites = decode_json($row->[0]);
};
else {
eval { $sites = decode_json( $row->[0] ); };
$@ and $sites = [];
}
......@@ -65,9 +54,7 @@ around 'BUILDARGS' => sub
);
};
sub add_site
{
sub add_site {
my $self = shift;
my $site = shift;
......@@ -77,9 +64,7 @@ sub add_site
$self->_set_sites( \@new_sites );
}
sub remove_site
{
sub remove_site {
my $self = shift;
my $site = shift;
......@@ -89,9 +74,7 @@ sub remove_site
$self->_set_sites( \@new_sites );
}
sub _update_db
{
sub _update_db {
my $self = shift;
my $sites = shift;
......@@ -105,6 +88,4 @@ sub _update_db
$dbh->commit;
}
1;
......@@ -9,7 +9,6 @@ use Moose;
use PlayBot::utils::db;
has '_dbh' => (
is => 'rw',
isa => 'HashRef',
......@@ -36,7 +35,9 @@ sub get {
my $chan = $query->chan;
my $result;
if (defined $self->_queries->{$chan} and $self->_queries->{$chan} ~~ $query) {
if ( defined $self->_queries->{$chan}
and $self->_queries->{$chan} ~~ $query )
{
if ( $self->_queries->{$chan}->date < time - 3600 * 6 ) {
$self->_clean($chan);
$self->_init($query);
......@@ -74,7 +75,8 @@ sub get_rows {
my $dbh = PlayBot::utils::db::get_new_session();
my ( $request, @args ) = $self->_prepare_request($query);
my $sth = $dbh->prepare('select count(*) from ('.$request.') as TGTGTG');
my $sth =
$dbh->prepare( 'select count(*) from (' . $request . ') as TGTGTG' );
$sth->execute(@args);
my $rows = $sth->fetch->[0];
......@@ -92,14 +94,17 @@ sub make_view {
$dbh->{AutoCommit} = 0;
my ( $request, @args ) = $self->_prepare_request($query);
my $view_name = join'', map +(0..9,'a'..'z','A'..'Z')[rand(10+26*2)], 1..32;
my $view_name = join '',
map +( 0 .. 9, 'a' .. 'z', 'A' .. 'Z' )[ rand( 10 + 26 * 2 ) ], 1 .. 32;
my $sth = $dbh->prepare( 'create view ' . $view_name . ' AS ' . $request );
$sth->execute(@args);
$dbh->do('create event clear'.$view_name.'
$dbh->do(
'create event clear' . $view_name . '
on schedule at current_timestamp + interval 1 hour
do
drop view '.$view_name);
drop view ' . $view_name
);
$dbh->commit;
$dbh->disconnect;
......@@ -119,8 +124,7 @@ sub _get_next {
return undef;
}
sub _clean
{
sub _clean {
my ( $self, $chan ) = @_;
# we take care of the db
......@@ -170,7 +174,8 @@ sub _prepare_request {
}
if ( $query->id >= 0 ) {
$req = 'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req =
'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req .= ' from playbot p where id = ?';
@args = ( $query->id );
......@@ -181,13 +186,15 @@ sub _prepare_request {
my @where;
foreach my $tag ( @{ $query->tags } ) {
unshift @where, 'p.id in (select pt.id from playbot_tags pt where pt.tag = ?)';
unshift @where,
'p.id in (select pt.id from playbot_tags pt where pt.tag = ?)';
}
my $where = join ' and ' => @where;
if ( $query->is_global ) {
$req = 'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req =
'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req .= ' from playbot p where ' . $where;
$req .= ' and ' . $words_sql if ($words_sql);
$req .= ' and p.playlist is false';
......@@ -196,7 +203,8 @@ sub _prepare_request {
@args = ( @{ $query->tags }, @words_param );
}
else {
$req = 'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req =
'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req .= ' from playbot p join playbot_chan pc on p.id = pc.content';
$req .= ' where ' . $where;
$req .= ' and ' . $words_sql if ($words_sql);
......@@ -208,7 +216,8 @@ sub _prepare_request {
}
else {
if ( $query->is_global ) {
$req = 'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req =
'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req .= ' from playbot p where';
$req .= ' ' . $words_sql . ' and' if ($words_sql);
$req .= ' p.playlist is false';
......@@ -217,7 +226,8 @@ sub _prepare_request {
@args = (@words_param);
}
else {
$req = 'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req =
'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$req .= ' from playbot p join playbot_chan pc on p.id = pc.content';
$req .= ' where pc.chan = ?';
$req .= ' and ' . $words_sql if ($words_sql);
......@@ -229,7 +239,6 @@ sub _prepare_request {
}
if ( $query->fts and $words_sql ) {
$req .= ' order by ' . $words_sql . ' desc';
push @args, @words_param;
......@@ -244,9 +253,9 @@ sub _prepare_request {
sub _get_dbh {
my ( $self, $chan ) = @_;
if (not defined $self->_dbh->{$chan} or
not $self->_dbh->{$chan}->ping
) {
if ( not defined $self->_dbh->{$chan}
or not $self->_dbh->{$chan}->ping )
{
$self->_dbh->{$chan} = PlayBot::utils::db::get_new_session();
}
......
......@@ -10,7 +10,6 @@ use List::Util qw( reduce );
use PlayBot::utils::db;
has 'sender' => ( is => 'ro', isa => 'Str' );
has 'chan' => ( is => 'ro', isa => 'Str' );
has 'date' => ( is => 'ro', isa => 'Str' );
......@@ -19,7 +18,6 @@ has 'senders' => (is => 'ro', isa => 'HashRef[Str]');
has 'channels' => ( is => 'ro', isa => 'HashRef[Str]' );
has 'fav' => ( is => 'ro', isa => 'Int' );
around 'BUILDARGS' => sub {
my ( $orig, $class, $id ) = @_;
......@@ -41,6 +39,7 @@ around 'BUILDARGS' => sub {
$sth->execute($id);
while ( my $row = $sth->fetch ) {
# first entry
if ( not $sender ) {
$sender = $row->[2];
......@@ -79,21 +78,18 @@ around 'BUILDARGS' => sub {
sub max_sender {
my $self = shift;
my $max = reduce {
$self->senders->{$a} > $self->senders->{$b} ? $a : $b
} keys %{$self->senders};
my $max = reduce { $self->senders->{$a} > $self->senders->{$b} ? $a : $b }
keys %{ $self->senders };
return $max;
}
sub max_channel {
my $self = shift;
my $max = reduce {
$self->channels->{$a} > $self->channels->{$b} ? $a : $b
} keys %{$self->channels};
my $max = reduce { $self->channels->{$a} > $self->channels->{$b} ? $a : $b }
keys %{ $self->channels };
return $max;
}
1;
......@@ -29,13 +29,17 @@ sub get {
if ( !defined($index) or !length($index) ) {
$id = _get_last_id($chan);
} elsif (!looks_like_number($index)) {
}
elsif ( !looks_like_number($index) ) {
die "wrong index";
} elsif ($index < 0) {
}
elsif ( $index < 0 ) {
$id = _get_from_offset( $index, $chan );
} elsif (_test_if_exists ($index, $chan)) {
}
elsif ( _test_if_exists( $index, $chan ) ) {
$id = $index;
} else {
}
else {
die "wrong index";
}
......
......@@ -21,23 +21,21 @@ use IRC::Utils qw(YELLOW ORANGE GREEN NORMAL LIGHT_BLUE GREY);
# returns :
# - a string well formated
sub print
{
sub print {
my ($content) = @_;
my $id = $content->{'id'};
# BRUTALE
$id = 'BRUTALE' if ( $id eq 9320 );
my $msg = YELLOW . '[' . $id . '] ' . GREEN . $content->{'title'};
if (defined $content->{'author'})
{
if ( defined $content->{'author'} ) {
$msg .= ' | ' . $content->{'author'};
}
if (defined $content->{'duration'})
{
if ( defined $content->{'duration'} ) {
my $h = int( $content->{'duration'} / 3600 );
my $m = int( ( $content->{'duration'} % 3600 ) / 60 );
my $s = int( ( $content->{'duration'} % 3600 ) % 60 );
......@@ -51,8 +49,7 @@ sub print
$msg .= ' => ' . $content->{'url'} if ( defined $content->{'url'} );
if (defined $content->{'tags'})
{
if ( defined $content->{'tags'} ) {
$msg .= ' ' . ORANGE;
$msg .= join( ' ', @{ $content->{'tags'} } );
}
......@@ -62,8 +59,8 @@ sub print
return $msg;
}
sub stats
{
sub stats {
# a utils::db::stats object;
my $stats = shift;
my $line1;
......@@ -75,8 +72,7 @@ sub stats
$line1 .= ' le ' . $stats->date;
$line1 .= ' sur ' . $stats->chan;
if ($stats->count > 1)
{
if ( $stats->count > 1 ) {
my $senders_count = keys %{ $stats->senders };
my $channels_count = keys %{ $stats->channels };
......@@ -87,23 +83,20 @@ sub stats
$line2 .= 's' if ( $channels_count > 1 );
my $max_sender_count = $stats->senders->{ $stats->max_sender };
if ($max_sender_count > 1)
{
if ( $max_sender_count > 1 ) {
$line3 .= $stats->max_sender . " l'a posté ";
$line3 .= $max_sender_count . ' fois';
}
my $max_channel_count = $stats->channels->{ $stats->max_channel };
if ($max_channel_count != $stats->count and $max_channel_count > 1)
{
if ( $max_channel_count != $stats->count and $max_channel_count > 1 ) {
$line3 .= ' et ' if ( $max_sender_count > 1 );
$line3 .= 'il a été posté ' . $max_channel_count;
$line3 .= ' fois sur ' . $stats->max_channel;
}
}
if ($stats->fav)
{
if ( $stats->fav ) {
$line4 = "Sauvegardé dans ses favoris par ";
$line4 .= $stats->fav . ' personne';
$line4 .= 's' if ( $stats->fav > 1 );
......
......@@ -36,6 +36,7 @@ sub on_start {
my $content = decode_json $json;
print "$json\n";
$kernel->post('IKC', 'post', 'poe://PlayBot/fbrecv/fbmsg', [ $content->{'author'}, $content->{'msg'} ]);
$kernel->post( 'IKC', 'post', 'poe://PlayBot/fbrecv/fbmsg',
[ $content->{'author'}, $content->{'msg'} ] );
$kernel->post( 'IKC', 'shutdown' );
}
......@@ -15,7 +15,8 @@ sub get_txt_path {
foreach ( 1 .. $num_parts ) {
return get_txt_path( $entity->parts( $_ - 1 ) );
}
} else {
}
else {
# we only want text
if ( $entity->effective_type =~ /^text\/(?!(html|enriched))/ ) {
my $path = $entity->bodyhandle->path;
......@@ -35,9 +36,11 @@ sub get_content {
while (<$fh>) {
if (/^(.*) posted in NightIIEs/) {
$content{'author'} = $1;
} elsif (/^https:\/\/l.facebook.com\/l\/.*\/(.*)/) {
}
elsif (/^https:\/\/l.facebook.com\/l\/.*\/(.*)/) {
$content{'msg'} .= ' ' . $uri->decode($1);
} elsif (/#/) {
}
elsif (/#/) {
chomp;
$content{'msg'} .= ' ' . $_;
}
......@@ -46,7 +49,8 @@ sub get_content {
return \%content;
}
my ($fh, $filename) = tempfile('playbot' . time . '_XXXX', UNLINK => 0, DIR => '/tmp');
my ( $fh, $filename ) =
tempfile( 'playbot' . time . '_XXXX', UNLINK => 0, DIR => '/tmp' );
while (<STDIN>) {
print $fh $_;
}
......@@ -66,11 +70,15 @@ pipe(READ,WRITE);
select( ( select(READ), $| = 1 )[0] );
if ( my $pid = fork ) {
# parent
# map READ to STDIN
open( STDIN, "<&READ" );
exec('ssh -i /usr/local/lib/playbot/key morignot2011@perso.iiens.net ./PlayBot/tools/readjson.pl');
} else {
exec(
'ssh -i /usr/local/lib/playbot/key morignot2011@perso.iiens.net ./PlayBot/tools/readjson.pl'
);
}
else {
# child
print WRITE "$json\n";
}