Compare commits

..

5 Commits

View File

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