line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#ABSTRACT: Subroutines for FR24-Bot |
2
|
|
|
|
|
|
|
package FR24::Bot; |
3
|
3
|
|
|
3
|
|
2175
|
use v5.12; |
|
3
|
|
|
|
|
11
|
|
4
|
3
|
|
|
3
|
|
13
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
74
|
|
5
|
3
|
|
|
3
|
|
2242
|
use JSON::PP; |
|
3
|
|
|
|
|
43289
|
|
|
3
|
|
|
|
|
196
|
|
6
|
3
|
|
|
3
|
|
1269
|
use Data::Dumper; |
|
3
|
|
|
|
|
13088
|
|
|
3
|
|
|
|
|
195
|
|
7
|
3
|
|
|
3
|
|
20
|
use Exporter qw(import); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
153
|
|
8
|
3
|
|
|
3
|
|
2362
|
use HTTP::Tiny; |
|
3
|
|
|
|
|
141167
|
|
|
3
|
|
|
|
|
135
|
|
9
|
3
|
|
|
3
|
|
1464
|
use File::Which; |
|
3
|
|
|
|
|
3058
|
|
|
3
|
|
|
|
|
174
|
|
10
|
3
|
|
|
3
|
|
1422
|
use FR24::Utils; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
328
|
|
11
|
3
|
|
|
3
|
|
23
|
use Carp qw(confess); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
2539
|
|
12
|
|
|
|
|
|
|
our $VERSION = "0.0.3"; |
13
|
|
|
|
|
|
|
my $UPDATE_MS = 10 * 1000; |
14
|
|
|
|
|
|
|
# Export version |
15
|
|
|
|
|
|
|
our @EXPORT = qw($VERSION); |
16
|
|
|
|
|
|
|
our @EXPORT_OK = qw(new get); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new { |
20
|
2
|
|
|
2
|
1
|
1361
|
my $class = shift @_; |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
4
|
my $name = undef; |
23
|
2
|
|
|
|
|
4
|
my $config = undef; |
24
|
2
|
|
|
|
|
3
|
my $refresh = $UPDATE_MS; |
25
|
2
|
|
|
|
|
3
|
my $test_mode = 0; |
26
|
|
|
|
|
|
|
# Descriptive instantiation with parameters -param => value |
27
|
2
|
50
|
|
|
|
11
|
if (substr($_[0], 0, 1) eq '-') { |
28
|
2
|
|
|
|
|
7
|
my %data = @_; |
29
|
|
|
|
|
|
|
# Try parsing |
30
|
2
|
|
|
|
|
8
|
for my $i (keys %data) { |
31
|
5
|
100
|
|
|
|
49
|
if ($i =~ /^-conf/i) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
32
|
|
|
|
|
|
|
#Can be -conf, -config and other crazy stuff, hope not -confused |
33
|
2
|
|
|
|
|
8
|
$config = $data{$i}; |
34
|
2
|
50
|
|
|
|
5
|
if (ref($config) ne "HASH") { |
35
|
2
|
|
|
|
|
8
|
$config = FR24::Utils::loadconfig($config); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} elsif ($i =~ /^-(name|id)$/i) { |
38
|
2
|
|
|
|
|
7
|
$name = $data{$i}; |
39
|
|
|
|
|
|
|
} elsif ($i =~ /^-refresh$/i) { |
40
|
|
|
|
|
|
|
# Receive seconds, convert to milliseconds |
41
|
0
|
|
|
|
|
0
|
$refresh = 1000 * $data{$i}; |
42
|
|
|
|
|
|
|
} elsif ($i =~ /^-test$/i) { |
43
|
|
|
|
|
|
|
# Receive seconds, convert to milliseconds |
44
|
1
|
50
|
|
|
|
4
|
$test_mode = 1 if $data{$i}; |
45
|
|
|
|
|
|
|
} else { |
46
|
0
|
|
|
|
|
0
|
confess "ERROR FR24::Bot: Unknown parameter $i\n"; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
2
|
|
|
|
|
10
|
my $self = bless {}, $class; |
55
|
|
|
|
|
|
|
|
56
|
2
|
50
|
|
|
|
8
|
if (not defined $config->{telegram}->{apikey}) { |
57
|
0
|
|
|
|
|
0
|
confess "ERROR FR24::Bot: No config provided or no apikey found\n"; |
58
|
|
|
|
|
|
|
} |
59
|
2
|
|
|
|
|
13
|
$self->{apikey} = $config->{telegram}->{apikey}; |
60
|
2
|
|
|
|
|
4
|
$self->{name} = $name; |
61
|
2
|
|
|
|
|
4
|
$self->{config} = $config; |
62
|
2
|
|
|
|
|
3
|
$self->{ip} = $config->{server}->{ip}; |
63
|
2
|
|
|
|
|
4
|
$self->{port} = $config->{server}->{port}; |
64
|
2
|
|
|
|
|
4
|
$self->{localip} = undef; |
65
|
2
|
|
|
|
|
3
|
$self->{refresh} = $refresh; |
66
|
2
|
|
|
|
|
5
|
$self->{total} = 0; |
67
|
2
|
|
|
|
|
4
|
$self->{uploaded} = 0; |
68
|
2
|
|
|
|
|
4
|
$self->{flights} = {}; |
69
|
2
|
|
|
|
|
12
|
$self->{callsigns} = {}; |
70
|
2
|
|
|
|
|
8
|
$self->{flights_url} = "http://" . $self->{ip} . ":" . $self->{port} . "/flights.json"; |
71
|
|
|
|
|
|
|
|
72
|
2
|
|
|
|
|
4
|
$self->{users} = {}; |
73
|
2
|
|
|
|
|
3
|
$self->{last_updated} = 0; |
74
|
2
|
|
|
|
|
4
|
$self->{last_url} = undef; |
75
|
2
|
|
|
|
|
4
|
$self->{test_mode} = $test_mode; |
76
|
2
|
|
|
|
|
8
|
$self->update(); |
77
|
2
|
|
|
|
|
35
|
return $self; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _timestamp_milliseconds { |
82
|
4
|
|
|
4
|
|
20
|
return int(time * 1000); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
sub update { |
85
|
2
|
|
|
2
|
1
|
2
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
6
|
my $timestamp = _timestamp_milliseconds(); |
88
|
2
|
50
|
|
|
|
9
|
if ($timestamp - $self->{last_updated} < $self->{refresh}) { |
89
|
|
|
|
|
|
|
# Update only once per second |
90
|
0
|
|
|
|
|
0
|
return; |
91
|
|
|
|
|
|
|
} |
92
|
2
|
|
|
|
|
3
|
$self->{last_updated} = $timestamp; |
93
|
2
|
|
|
|
|
6
|
my $url = $self->{flights_url} . "?time=" . _timestamp_milliseconds(); |
94
|
2
|
|
|
|
|
4
|
$self->{last_url} = $url; |
95
|
2
|
50
|
|
|
|
5
|
confess "No URL specified for update\n" unless defined $url; |
96
|
|
|
|
|
|
|
|
97
|
2
|
|
|
|
|
5
|
my $content = _curl($url); |
98
|
2
|
|
|
|
|
5
|
$self->{content} = $content; |
99
|
2
|
|
|
|
|
7
|
my $data = FR24::Utils::parse_flights($content, $self->{test_mode}); |
100
|
|
|
|
|
|
|
# Parse the content here |
101
|
|
|
|
|
|
|
# Example: extracting flight information |
102
|
|
|
|
|
|
|
#my @flights = extract_flights($content); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Update the object properties |
105
|
2
|
|
|
|
|
8
|
$self->{flights} = $data->{data}; |
106
|
2
|
|
|
|
|
5
|
$self->{callsigns} = $data->{callsigns}; |
107
|
2
|
|
|
|
|
5
|
$self->{total} = $data->{total}; |
108
|
2
|
|
|
|
|
4
|
$self->{uploaded} = $data->{uploaded}; |
109
|
|
|
|
|
|
|
|
110
|
2
|
|
|
|
|
25
|
return; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Write a $self->update() method to update the object |
116
|
|
|
|
|
|
|
sub getflight { |
117
|
2
|
|
|
2
|
0
|
537
|
my ($self, $callsign) = @_; |
118
|
2
|
100
|
|
|
|
8
|
if (not defined $self->{'callsigns'}->{$callsign}) { |
119
|
1
|
|
|
|
|
8
|
return 0; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
1
|
|
|
|
|
9
|
return $self->{'flights'}->{ $self->{'callsigns'}->{$callsign} }; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub _curl { |
126
|
2
|
|
|
2
|
|
13
|
my $url = shift; |
127
|
2
|
|
|
|
|
4
|
my $response = ""; |
128
|
2
|
|
|
|
|
3
|
eval { |
129
|
2
|
|
|
|
|
15
|
my $http = HTTP::Tiny->new(); |
130
|
2
|
|
|
|
|
280
|
my $response = $http->get($url); |
131
|
|
|
|
|
|
|
|
132
|
2
|
50
|
|
|
|
4143
|
if ($response->{success}) { |
133
|
0
|
|
|
|
|
0
|
return $response->{content}; |
134
|
|
|
|
|
|
|
} else { |
135
|
2
|
|
|
|
|
19
|
return "Failed to retrieve URL: $response->{status} $response->{reason}\n"; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
__END__ |