Compare commits
2 Commits
master
...
366f740795
Author | SHA1 | Date | |
---|---|---|---|
366f740795 | |||
5a1c720181 |
73
F1DataBot.pm
73
F1DataBot.pm
@ -12,7 +12,6 @@ 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;
|
||||||
@ -22,12 +21,6 @@ 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 ) = @_;
|
||||||
@ -193,28 +186,20 @@ sub statistics {
|
|||||||
my $StatIdentifier = $+{statidentifier};
|
my $StatIdentifier = $+{statidentifier};
|
||||||
given ($StatIdentifier) {
|
given ($StatIdentifier) {
|
||||||
when ('standings') {
|
when ('standings') {
|
||||||
my $Standings;
|
my $Standings = json POST(
|
||||||
if ( $CACHE->get('driver_standings') ) {
|
join( '/',
|
||||||
$Standings = $CACHE->get('driver_standings');
|
( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' )
|
||||||
}
|
),
|
||||||
else {
|
{}
|
||||||
$Standings = json POST(
|
);
|
||||||
join( '/',
|
|
||||||
( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' )
|
|
||||||
),
|
|
||||||
{}
|
|
||||||
);
|
|
||||||
$CACHE->set('driver_standings', $Standings);
|
|
||||||
}
|
|
||||||
|
|
||||||
my %DriverStandings;
|
my %DriverStandings;
|
||||||
my $DriverStandingsFormatted = sprintf( "%2s %3s %-5s%7s\n",
|
my $DriverStandingsFormatted = sprintf( "%-3s%-4s%-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(
|
||||||
"%2d %3d %-5s%7d\n",
|
"%-3d%-4d%-5s%7d\n",
|
||||||
$Driver->{positionText},
|
$Driver->{positionText},
|
||||||
$Driver->{Driver}->{permanentNumber},
|
$Driver->{Driver}->{permanentNumber},
|
||||||
$Driver->{Driver}->{code},
|
$Driver->{Driver}->{code},
|
||||||
@ -250,38 +235,30 @@ sub statistics {
|
|||||||
my $StatIdentifier = $+{statidentifier};
|
my $StatIdentifier = $+{statidentifier};
|
||||||
given ($StatIdentifier) {
|
given ($StatIdentifier) {
|
||||||
when ('standings') {
|
when ('standings') {
|
||||||
my $Standings;
|
my $Standings = json POST(
|
||||||
if ( $CACHE->get('constructor_standings') ) {
|
join(
|
||||||
$Standings = $CACHE->get('constructor_standings');
|
'/',
|
||||||
}
|
(
|
||||||
else {
|
$Self->{URL}{Ergast}, 'current',
|
||||||
$Standings = json POST(
|
'constructorStandings.json'
|
||||||
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 $ConstructorStandingsFormatted = sprintf( "%2s %-15s%7s\n",
|
my %ConstructorStandings;
|
||||||
"#", "Name", "Points" );
|
my $ConstructorStandingsFormatted = '';
|
||||||
for my $Constructor ( $Standings->{MRData}->{StandingsTable}
|
for my $Constructor ( $Standings->{MRData}->{StandingsTable}
|
||||||
->{StandingsLists}->[0]->{ConstructorStandings}->@* )
|
->{StandingsLists}->[0]->{ConstructorStandings}->@* )
|
||||||
{
|
{
|
||||||
$ConstructorStandingsFormatted .= sprintf(
|
$ConstructorStandingsFormatted .=
|
||||||
"%2d %-15s%7d\n",
|
"Position: $Constructor->{positionText}, Name: $Constructor->{Constructor}->{name} - Points: $Constructor->{points}\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} =
|
||||||
@ -457,8 +434,6 @@ 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}}) {
|
||||||
@ -471,7 +446,7 @@ sub fetchMessages {
|
|||||||
$Self->processMessage( Message => $Message, );
|
$Self->processMessage( Message => $Message, );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
YAML::DumpFile("$FindBin::Bin/message_ids.yml", $Self->{MessageIDs});
|
YAML::DumpFile('message_ids.yml', $Self->{MessageIDs});
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
Reference in New Issue
Block a user