FR24-Bot

 view release on metacpan or  search on metacpan

lib/FR24/Bot.pm  view on Meta::CPAN

    my $refresh = $UPDATE_MS;
    my $test_mode = 0;
    # Descriptive instantiation with parameters -param => value
    if (substr($_[0], 0, 1) eq '-') {
        my %data = @_;
        # Try parsing
        for my $i (keys %data) {
            if ($i =~ /^-conf/i) {
                #Can be -conf, -config and other crazy stuff, hope not -confused
                $config = $data{$i};
                if (ref($config) ne "HASH") { 
                    $config = FR24::Utils::loadconfig($config);
                }
            } elsif ($i =~ /^-(name|id)$/i) {
                $name = $data{$i};
            } elsif ($i =~ /^-refresh$/i) {
                # Receive seconds, convert to milliseconds
                $refresh = 1000 * $data{$i};
            } elsif ($i =~ /^-test$/i) {
                # Receive seconds, convert to milliseconds
                $test_mode = 1 if $data{$i};
            } else {
                confess "ERROR FR24::Bot: Unknown parameter $i\n";
            }
        }
    } 
  
   
 
 
    my $self = bless {}, $class;
     
    if (not defined $config->{telegram}->{apikey}) {
        confess "ERROR FR24::Bot: No config provided or no apikey found\n";
    }
    $self->{apikey} = $config->{telegram}->{apikey};
    $self->{name} = $name;
    $self->{config} = $config;
    $self->{ip} = $config->{server}->{ip};
    $self->{port} = $config->{server}->{port};
    $self->{localip} = undef;
    $self->{refresh} = $refresh;
    $self->{total} = 0;
    $self->{uploaded} = 0;
    $self->{flights} = {};
    $self->{callsigns} = {};
    $self->{flights_url} = "http://" . $self->{ip} . ":" . $self->{port} . "/flights.json";

    $self->{users} = {};
    $self->{last_updated} = 0;
    $self->{last_url} = undef;
    $self->{test_mode} = $test_mode;
    $self->update();
    return $self;
  
}

sub _timestamp_milliseconds {
    return int(time * 1000);
}
sub update {
    my $self = shift;
    
    my $timestamp = _timestamp_milliseconds();
    if ($timestamp - $self->{last_updated} < $self->{refresh}) {
        # Update only once per second
        return;
    }
    $self->{last_updated} = $timestamp;
    my $url = $self->{flights_url} . "?time=" . _timestamp_milliseconds();
    $self->{last_url} = $url;
    confess "No URL specified for update\n" unless defined $url;

    my $content = _curl($url);
    $self->{content} = $content;
    my $data = FR24::Utils::parse_flights($content, $self->{test_mode});
    # Parse the content here
    # Example: extracting flight information
    #my @flights = extract_flights($content);

    # Update the object properties
    $self->{flights} = $data->{data};
    $self->{callsigns} = $data->{callsigns};
    $self->{total} = $data->{total};
    $self->{uploaded} = $data->{uploaded};

    return;
}



# Write a $self->update() method to update the object
sub getflight {
    my ($self, $callsign) = @_; 
    if (not defined $self->{'callsigns'}->{$callsign}) {
        return 0;
    }
   
    return $self->{'flights'}->{ $self->{'callsigns'}->{$callsign} };
}

sub _curl {
    my $url = shift;
    my $response = "";
    eval {
        my $http = HTTP::Tiny->new();
        my $response = $http->get($url);

        if ($response->{success}) {
            return $response->{content};
        } else {
            return "Failed to retrieve URL: $response->{status} $response->{reason}\n";
        }
    };
}

__END__

=pod

=encoding UTF-8



( run in 1.407 second using v1.01-cache-2.11-cpan-39bf76dae61 )