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
  • cinch
  • gh-pages
  • master
  • ruby
  • v1.0.0
5 résultats

Cible

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