Compare commits

..

5 Commits

View File

@ -12,6 +12,7 @@ use Encode;
use FindBin; use FindBin;
# CPAN packages # CPAN packages
use Cache::FastMmap;
use JSON; use JSON;
use Log::Log4perl; use Log::Log4perl;
use YAML; use YAML;
@ -21,6 +22,12 @@ package F1DataBot;
# Constants and initalisations # Constants and initalisations
Log::Log4perl->init("$FindBin::Bin/log.conf"); Log::Log4perl->init("$FindBin::Bin/log.conf");
my $CACHE = Cache::FastMmap->new(
share_file => '/tmp/f1_bot',
serializer => 'json',
unlink_on_exit => 0,
expire_time => 86400,
);
sub new { sub new {
my ( $Type, %Param ) = @_; my ( $Type, %Param ) = @_;
@ -186,20 +193,28 @@ sub statistics {
my $StatIdentifier = $+{statidentifier}; my $StatIdentifier = $+{statidentifier};
given ($StatIdentifier) { given ($StatIdentifier) {
when ('standings') { when ('standings') {
my $Standings = json POST( my $Standings;
join( '/', if ( $CACHE->get('driver_standings') ) {
( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' ) $Standings = $CACHE->get('driver_standings');
), }
{} else {
); $Standings = json POST(
join( '/',
( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' )
),
{}
);
$CACHE->set('driver_standings', $Standings);
}
my %DriverStandings; my %DriverStandings;
my $DriverStandingsFormatted = sprintf( "%-3s%-4s%-5s%7s\n", my $DriverStandingsFormatted = sprintf( "%2s %3s %-5s%7s\n",
"#", "No.", "Code", "Points" ); "#", "No.", "Code", "Points" );
for my $Driver ( $Standings->{MRData}->{StandingsTable} for my $Driver ( $Standings->{MRData}->{StandingsTable}
->{StandingsLists}->[0]->{DriverStandings}->@* ) ->{StandingsLists}->[0]->{DriverStandings}->@* )
{ {
$DriverStandingsFormatted .= sprintf( $DriverStandingsFormatted .= sprintf(
"%-3d%-4d%-5s%7d\n", "%2d %3d %-5s%7d\n",
$Driver->{positionText}, $Driver->{positionText},
$Driver->{Driver}->{permanentNumber}, $Driver->{Driver}->{permanentNumber},
$Driver->{Driver}->{code}, $Driver->{Driver}->{code},
@ -235,30 +250,38 @@ sub statistics {
my $StatIdentifier = $+{statidentifier}; my $StatIdentifier = $+{statidentifier};
given ($StatIdentifier) { given ($StatIdentifier) {
when ('standings') { when ('standings') {
my $Standings = json POST( my $Standings;
join( if ( $CACHE->get('constructor_standings') ) {
'/', $Standings = $CACHE->get('constructor_standings');
( }
$Self->{URL}{Ergast}, 'current', else {
'constructorStandings.json' $Standings = json POST(
) join( '/',
), ( $Self->{URL}{Ergast}, 'current', 'constructorStandings.json' )
{} ),
); {}
);
$CACHE->set('constructor_standings', $Standings);
}
$Self->{LogObject} $Self->{LogObject}
->info( 'statistics: Fetched standings are ' ->info( 'statistics: Fetched standings are '
. Dumper($Standings) ); . Dumper($Standings) );
my %ConstructorStandings; my $ConstructorStandingsFormatted = sprintf( "%2s %-15s%7s\n",
my $ConstructorStandingsFormatted = ''; "#", "Name", "Points" );
for my $Constructor ( $Standings->{MRData}->{StandingsTable} for my $Constructor ( $Standings->{MRData}->{StandingsTable}
->{StandingsLists}->[0]->{ConstructorStandings}->@* ) ->{StandingsLists}->[0]->{ConstructorStandings}->@* )
{ {
$ConstructorStandingsFormatted .= $ConstructorStandingsFormatted .= sprintf(
"Position: $Constructor->{positionText}, Name: $Constructor->{Constructor}->{name} - Points: $Constructor->{points}\n"; "%2d %-15s%7d\n",
$Constructor->{positionText},
$Constructor->{Constructor}->{name},
$Constructor->{points}
);
} }
$ConstructorStandingsFormatted =~ s/^/<pre>/; $ConstructorStandingsFormatted =~ s/^/<pre>/;
$ConstructorStandingsFormatted =~ s/$/<\/pre>/; $ConstructorStandingsFormatted =~ s/$/<\/pre>/;
$ReturnData{text} = $ConstructorStandingsFormatted; $ReturnData{text} = $ConstructorStandingsFormatted;
$ReturnData{parse_mode} = 'HTML';
} }
default { default {
$ReturnData{text} = $ReturnData{text} =
@ -434,6 +457,8 @@ sub fetchMessages {
if ( !$Self->{Whitelist}{$Message->{message}{from}{id}} ) { if ( !$Self->{Whitelist}{$Message->{message}{from}{id}} ) {
$Self->{LogObject} $Self->{LogObject}
->info( 'fetchMessages: User not whitelisted, skipping message ' . Dumper($MessageDataRaw) ); ->info( 'fetchMessages: User not whitelisted, skipping message ' . Dumper($MessageDataRaw) );
$Self->{MessageIDs}{$Message->{message}{message_id}} = 1;
next MESSAGE; next MESSAGE;
} }
if ($Self->{MessageIDs}{$Message->{message}{message_id}}) { if ($Self->{MessageIDs}{$Message->{message}{message_id}}) {
@ -446,7 +471,7 @@ sub fetchMessages {
$Self->processMessage( Message => $Message, ); $Self->processMessage( Message => $Message, );
} }
} }
YAML::DumpFile('message_ids.yml', $Self->{MessageIDs}); YAML::DumpFile("$FindBin::Bin/message_ids.yml", $Self->{MessageIDs});
} }
1; 1;