line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Ham::Reference::Callook; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# -------------------------------------------------------------------------- |
4
|
|
|
|
|
|
|
# Ham::Reference::Callook - An interface to the Callook.info Database Service |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Copyright (c) 2010-2011 Brad McConahay N8QQ. |
7
|
|
|
|
|
|
|
# Cincinnati, Ohio USA |
8
|
|
|
|
|
|
|
# -------------------------------------------------------------------------- |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
54499
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
11
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
12
|
1
|
|
|
1
|
|
481
|
use XML::Simple; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use LWP::UserAgent; |
14
|
|
|
|
|
|
|
use vars qw($VERSION); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $callook_url = "http://callook.info/"; |
19
|
|
|
|
|
|
|
my $site_name = 'Callook.info'; |
20
|
|
|
|
|
|
|
my $default_timeout = 10; |
21
|
|
|
|
|
|
|
my $default_type = 'xml'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub new |
24
|
|
|
|
|
|
|
{ |
25
|
|
|
|
|
|
|
my $class = shift; |
26
|
|
|
|
|
|
|
my %args = @_; |
27
|
|
|
|
|
|
|
my $self = {}; |
28
|
|
|
|
|
|
|
bless $self, $class; |
29
|
|
|
|
|
|
|
$self->_clear_errors; |
30
|
|
|
|
|
|
|
$self->_set_agent; |
31
|
|
|
|
|
|
|
$self->timeout($args{timeout}); |
32
|
|
|
|
|
|
|
$self->type($args{type}); |
33
|
|
|
|
|
|
|
return($self); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub listing |
37
|
|
|
|
|
|
|
{ |
38
|
|
|
|
|
|
|
my $self = shift; |
39
|
|
|
|
|
|
|
my $callsign = shift; |
40
|
|
|
|
|
|
|
$self->_clear_errors; |
41
|
|
|
|
|
|
|
$callsign =~ tr/a-z/A-Z/; |
42
|
|
|
|
|
|
|
$self->{_callsign} = $callsign; |
43
|
|
|
|
|
|
|
my $url = "$callook_url/$self->{_callsign}/$self->{_type}"; |
44
|
|
|
|
|
|
|
if ($self->{_type} eq 'text') |
45
|
|
|
|
|
|
|
{ |
46
|
|
|
|
|
|
|
$self->{_data} = $self->_get_http($url); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
elsif ($self->{_type} eq 'xml') |
49
|
|
|
|
|
|
|
{ |
50
|
|
|
|
|
|
|
$self->{_data} = $self->_get_xml($url); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
else # unknown type |
53
|
|
|
|
|
|
|
{ |
54
|
|
|
|
|
|
|
$self->{is_error} = 1; |
55
|
|
|
|
|
|
|
$self->{error_message} = "Unknown type: $self->{_type}"; |
56
|
|
|
|
|
|
|
$self->{_data} = undef; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
return $self->{_data}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub timeout |
62
|
|
|
|
|
|
|
{ |
63
|
|
|
|
|
|
|
my $self = shift; |
64
|
|
|
|
|
|
|
my $timeout = shift || $default_timeout; |
65
|
|
|
|
|
|
|
$self->{_timeout} = $timeout; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub type |
69
|
|
|
|
|
|
|
{ |
70
|
|
|
|
|
|
|
my $self = shift; |
71
|
|
|
|
|
|
|
my $type = shift || $default_type; |
72
|
|
|
|
|
|
|
$self->{_type} = $type; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub is_error { my $self = shift; $self->{is_error} } |
76
|
|
|
|
|
|
|
sub error_message { my $self = shift; $self->{error_message} } |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# ----------------------- |
80
|
|
|
|
|
|
|
# PRIVATE |
81
|
|
|
|
|
|
|
# ----------------------- |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _set_agent |
84
|
|
|
|
|
|
|
{ |
85
|
|
|
|
|
|
|
my $self = shift; |
86
|
|
|
|
|
|
|
$self->{_agent} = "Perl-Module-Ham-Reference-Callook-$VERSION"; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _get_xml |
90
|
|
|
|
|
|
|
{ |
91
|
|
|
|
|
|
|
my $self = shift; |
92
|
|
|
|
|
|
|
my $url = shift; |
93
|
|
|
|
|
|
|
my $content = $self->_get_http($url); |
94
|
|
|
|
|
|
|
return undef if $self->{is_error}; |
95
|
|
|
|
|
|
|
chomp $content; |
96
|
|
|
|
|
|
|
$content =~ s/(\r|\n)//g; |
97
|
|
|
|
|
|
|
my $xs = XML::Simple->new( SuppressEmpty => 0 ); |
98
|
|
|
|
|
|
|
my $data = $xs->XMLin($content); |
99
|
|
|
|
|
|
|
return $data; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _get_http |
103
|
|
|
|
|
|
|
{ |
104
|
|
|
|
|
|
|
my $self = shift; |
105
|
|
|
|
|
|
|
my $url = shift; |
106
|
|
|
|
|
|
|
$self->_clear_errors; |
107
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new( timeout=>$self->{_timeout} ); |
108
|
|
|
|
|
|
|
$ua->agent( $self->{_agent} ); |
109
|
|
|
|
|
|
|
my $request = HTTP::Request->new('GET', $url); |
110
|
|
|
|
|
|
|
my $response = $ua->request($request); |
111
|
|
|
|
|
|
|
if (!$response->is_success) { |
112
|
|
|
|
|
|
|
$self->{is_error} = 1; |
113
|
|
|
|
|
|
|
$self->{error_message} = "Could not contact $site_name - ".HTTP::Status::status_message($response->code); |
114
|
|
|
|
|
|
|
return undef; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
return $response->content; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub _clear_errors |
120
|
|
|
|
|
|
|
{ |
121
|
|
|
|
|
|
|
my $self = shift; |
122
|
|
|
|
|
|
|
$self->{is_error} = 0; |
123
|
|
|
|
|
|
|
$self->{error_message} = ''; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |
128
|
|
|
|
|
|
|
__END__ |