Initial commit.
This commit is contained in:
commit
99df91a112
175
F1DataBot.pm
Normal file
175
F1DataBot.pm
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
#!/usr/bin/perl
|
||||||
|
|
||||||
|
# Important for switch feature
|
||||||
|
use v5.34;
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
|
||||||
|
# CPAN packages
|
||||||
|
use Log::Log4perl;
|
||||||
|
|
||||||
|
# Package name
|
||||||
|
package F1DataBot;
|
||||||
|
|
||||||
|
# Constants and initalisations
|
||||||
|
Log::Log4perl->init('log.conf');
|
||||||
|
my $ERGAST_URL = 'http://ergast.com/api/f1';
|
||||||
|
my $TELEGRAM_URL = 'https://api.telegram.org';
|
||||||
|
my $TOKEN = 'bot5868933096:AAE8Oe-AxU6m_yCWfpqTqwwjERqnRpBGJtE';
|
||||||
|
|
||||||
|
sub greet {
|
||||||
|
|
||||||
|
my %Param = @_;
|
||||||
|
my $LogObject = Log::Log4perl->get_logger('F1DataBot');
|
||||||
|
use Data::Dumper;
|
||||||
|
|
||||||
|
$LogObject->info('greet: Initiating greet routine');
|
||||||
|
|
||||||
|
if ( !defined $Param{Message} ) {
|
||||||
|
$LogObject->error('greet: Message not defined!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
my @Greetings = qw(Hallo Gruezi Hello Holá Bonjour Konichiwa Shalom Godmorgen);
|
||||||
|
my $Greet = $Greetings[ int(rand(7)) ] || '';
|
||||||
|
$LogObject->debug('Random Greet is ' . $Greet);
|
||||||
|
my $ReturnContent = $Greet . ', ' . ($Param{Message}->{chat}->{first_name} ? $Param{Message}->{chat}->{first_name} : $Param{Message}->{chat}->{username}) . '!';
|
||||||
|
$LogObject->debug('ReturnContent is ' . $ReturnContent);
|
||||||
|
return $ReturnContent;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
sub statistics {
|
||||||
|
|
||||||
|
my %Param = @_;
|
||||||
|
my $LogObject = Log::Log4perl->get_logger('F1DataBot');
|
||||||
|
use Data::Dumper;
|
||||||
|
use LWP::Simple::REST qw(GET json);
|
||||||
|
use XML::Parser;
|
||||||
|
|
||||||
|
$LogObject->info('statistics: Initiating statistics routine');
|
||||||
|
|
||||||
|
if ( !defined $Param{Message} ) {
|
||||||
|
$LogObject->error('statistics: Message not defined!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse arguments
|
||||||
|
# Expected format: driver | constructor | circuit
|
||||||
|
# Circuit: [name] [statistics identifier] [individual parameters]
|
||||||
|
$Param{Message}->{text} =~ /^(\/statistics)\s(?<statclass>driver|constructor|circuit)/;
|
||||||
|
my $StatClass = $+{statclass};
|
||||||
|
given ( $StatClass ) {
|
||||||
|
|
||||||
|
when ( 'driver' ) {
|
||||||
|
$LogObject->info('statistics: Recognizing driver command');
|
||||||
|
$Param{Message}->{text} =~ /^(\/statistics)\s$StatClass\s(?<statidentifier>\w+)/;
|
||||||
|
my $StatIdentifier = $+{statidentifier};
|
||||||
|
given ( $StatIdentifier ) {
|
||||||
|
when ( 'standings' ) {
|
||||||
|
my $Standings = json POST ( join('/', ($ERGAST_URL, 'current', 'driverStandings.json')), {} );
|
||||||
|
$LogObject->info('statistics: Fetched standings are ' . Dumper($Standings));
|
||||||
|
my %DriverStandings;
|
||||||
|
my $DriverStandingsFormatted = '';
|
||||||
|
for my $Driver ( $Standings->{MRData}->{StandingsTable}->{StandingsLists}->[0]->{DriverStandings}->@* ) {
|
||||||
|
$DriverStandingsFormatted .= "Position: $Driver->{positionText}, Name: $Driver->{Driver}->{code} - Points: $Driver->{points}\n";
|
||||||
|
}
|
||||||
|
return $DriverStandingsFormatted;
|
||||||
|
}
|
||||||
|
default {
|
||||||
|
return "I'm sorry, " . ($Param{Message}->{chat}->{first_name} ? $Param{Message}->{chat}->{first_name} : $Param{Message}->{chat}->{username}) . ", I recognized you wanted to fetch a statistic about drivers, but I couldn't determine which one. Maybe you want to try again? Currently available are:\n\tstandings";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
when ( 'constructor' ) {
|
||||||
|
$LogObject->info('statistics: Recognizing constructor command');
|
||||||
|
}
|
||||||
|
when ( 'circuit' ) {
|
||||||
|
$LogObject->info('statistics: Recognizing circuit command');
|
||||||
|
}
|
||||||
|
# Statistics class not recognized
|
||||||
|
default {
|
||||||
|
return "I'm sorry, " . ($Param{Message}->{chat}->{first_name} ? $Param{Message}->{chat}->{first_name} : $Param{Message}->{chat}->{username}) . ", I recognized you wanted to fetch a statistic, but I couldn't determine which one. Maybe you want to try again? Currently available are:\n\tdriver\n\tconstructor\n\tcircuit";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
# Use system to call the python script (big shame, but nothing to do about it)
|
||||||
|
return '';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
sub processMessage {
|
||||||
|
|
||||||
|
my %Param = @_;
|
||||||
|
my $LogObject = Log::Log4perl->get_logger('F1DataBot');
|
||||||
|
use Data::Dumper;
|
||||||
|
use LWP::Simple::REST qw(POST plain);
|
||||||
|
|
||||||
|
my %Commands = (
|
||||||
|
'greet' => \&greet,
|
||||||
|
'statistics' => \&statistics,
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( !defined $Param{Message} ) {
|
||||||
|
$LogObject->error('processMessage: Message not defined!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$LogObject->info('processMessage: Starting to process message');
|
||||||
|
$LogObject->info('processMessage: Message is ' . Dumper($Param{Message}));
|
||||||
|
|
||||||
|
my $Message = $Param{Message}->{message};
|
||||||
|
|
||||||
|
my $ResponseContent;
|
||||||
|
if ( $Message->{text} =~ /\/(?<command>greet|statistics)\s?(?<arguments>.*)?/ ) {
|
||||||
|
my $Command = $+{command};
|
||||||
|
my $ArgumentsString = $+{arguments};
|
||||||
|
$ResponseContent = $Commands{$Command}(
|
||||||
|
Message => $Message,
|
||||||
|
Arguments => $ArgumentsString,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$LogObject->debug('Command not recognized. Data: ' . $Message->{text});
|
||||||
|
$ResponseContent = "I'm sorry, " . ($Message->{chat}->{first_name} ? $Message->{chat}->{first_name} : $Message->{chat}->{username}) . ", I couldn't understand your request. Currently I can process the commands:\n\n\t\/greet";
|
||||||
|
}
|
||||||
|
|
||||||
|
my $ResponseResult = plain POST ( join('/', ($TELEGRAM_URL, $TOKEN, 'sendMessage')), {
|
||||||
|
chat_id => $Message->{chat}->{id},
|
||||||
|
text => $ResponseContent,
|
||||||
|
} );
|
||||||
|
$LogObject->info('processMessage: Answering result is ' . Dumper($ResponseResult));
|
||||||
|
|
||||||
|
# mark message as read
|
||||||
|
my $SeenResult = plain POST ( join('/', ($TELEGRAM_URL, $TOKEN, 'readMessageContents')), {
|
||||||
|
id => $Message->{id},
|
||||||
|
} );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
sub fetchMessages {
|
||||||
|
|
||||||
|
my %Param = @_;
|
||||||
|
my $LogObject = Log::Log4perl->get_logger('F1DataBot');
|
||||||
|
use Data::Dumper;
|
||||||
|
use LWP::Simple::REST qw(GET json);
|
||||||
|
|
||||||
|
my $Method = 'getUpdates';
|
||||||
|
$LogObject->info('fetchMessages: Initiating getUpdates');
|
||||||
|
|
||||||
|
my $MessageDataRaw = json GET ( join('/', ($TELEGRAM_URL, $TOKEN, $Method)), {} );
|
||||||
|
$LogObject->info('fetchMessages: Messages raw are ' . Dumper($MessageDataRaw));
|
||||||
|
my @Messages = $MessageDataRaw->{result}->@*;
|
||||||
|
$LogObject->info('fetchMessages: Messages returned are ' . Dumper(\@Messages));
|
||||||
|
for my $Message ( @Messages ) {
|
||||||
|
$LogObject->info('fetchMessages: Calling processMessage');
|
||||||
|
processMessage(
|
||||||
|
Message => $Message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
1;
|
7
bot_script.pl
Normal file
7
bot_script.pl
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#/usr/bin/perl
|
||||||
|
|
||||||
|
use lib '/home/demiguise/telegram_bot';
|
||||||
|
|
||||||
|
use F1DataBot;
|
||||||
|
|
||||||
|
F1DataBot::fetchMessages();
|
14
fastf1_connection.py
Executable file
14
fastf1_connection.py
Executable file
@ -0,0 +1,14 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# Module Imports
|
||||||
|
import fastf1
|
||||||
|
from fastf1.ergast import Ergast
|
||||||
|
|
||||||
|
fastf1.Cache.enable_cache('/home/demiguise/f1_data_analysis/cache')
|
||||||
|
|
||||||
|
def get_driver_standings():
|
||||||
|
ergast = Ergast()
|
||||||
|
standings = ergast.get_driver_standings(season=2023)
|
||||||
|
return standings.content[0]
|
||||||
|
|
||||||
|
print(get_driver_standings())
|
9
log.conf
Normal file
9
log.conf
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Config file for Log4perl
|
||||||
|
log4perl.rootLogger=DEBUG, LOGFILE
|
||||||
|
|
||||||
|
log4perl.appender.LOGFILE=Log::Log4perl::Appender::File
|
||||||
|
log4perl.appender.LOGFILE.filename=/home/demiguise/log4perl.log
|
||||||
|
log4perl.appender.LOGFILE.mode=append
|
||||||
|
|
||||||
|
log4perl.appender.LOGFILE.layout=PatternLayout
|
||||||
|
log4perl.appender.LOGFILE.layout.ConversionPattern=[%r] %F %L %c - %m%n
|
Loading…
Reference in New Issue
Block a user