line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Ham::Resources::HamQTH; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
27410
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
32
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3664
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
113678
|
|
|
1
|
|
|
|
|
38
|
|
7
|
1
|
|
|
1
|
|
1582
|
use XML::LibXML::Reader; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use vars qw($VERSION); |
9
|
|
|
|
|
|
|
use Data::Dumper; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $qth_url = "http://www.hamqth.com"; |
14
|
|
|
|
|
|
|
my $site_name = 'HamQTH XML Database service'; |
15
|
|
|
|
|
|
|
my $default_timeout = 10; |
16
|
|
|
|
|
|
|
my $default_strip_html = 1; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new |
20
|
|
|
|
|
|
|
{ |
21
|
|
|
|
|
|
|
my $class = shift; |
22
|
|
|
|
|
|
|
my %args = @_; |
23
|
|
|
|
|
|
|
my $self = {}; |
24
|
|
|
|
|
|
|
bless $self, $class; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$self->_set_agent; |
27
|
|
|
|
|
|
|
$self->set_timeout($args{timeout}); |
28
|
|
|
|
|
|
|
$self->set_callsign($args{callsign}) if $args{callsign}; |
29
|
|
|
|
|
|
|
$self->set_username($args{username}) if $args{username}; |
30
|
|
|
|
|
|
|
$self->set_password($args{password}) if $args{password}; |
31
|
|
|
|
|
|
|
$self->set_strip_html($args{strip_html_bio}); |
32
|
|
|
|
|
|
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub login |
36
|
|
|
|
|
|
|
{ |
37
|
|
|
|
|
|
|
my $self = shift; |
38
|
|
|
|
|
|
|
my $url = "$qth_url/xml.php?u=".$self->{_username}."&p=".$self->{_password}; |
39
|
|
|
|
|
|
|
my $login = $self->_get_content($url); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub set_callsign |
43
|
|
|
|
|
|
|
{ |
44
|
|
|
|
|
|
|
my $self = shift; |
45
|
|
|
|
|
|
|
my $callsign = shift; |
46
|
|
|
|
|
|
|
$callsign =~ tr/a-z/A-Z/; |
47
|
|
|
|
|
|
|
$self->{_callsign} = $callsign; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub set_username |
51
|
|
|
|
|
|
|
{ |
52
|
|
|
|
|
|
|
my $self = shift; |
53
|
|
|
|
|
|
|
my $username = shift; |
54
|
|
|
|
|
|
|
$self->{_username} = $username; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub set_password |
58
|
|
|
|
|
|
|
{ |
59
|
|
|
|
|
|
|
my $self = shift; |
60
|
|
|
|
|
|
|
my $password = shift; |
61
|
|
|
|
|
|
|
$self->{_password} = $password; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub set_strip_html |
65
|
|
|
|
|
|
|
{ |
66
|
|
|
|
|
|
|
my $self = shift; |
67
|
|
|
|
|
|
|
my $strip_html_bio = shift; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
if (!$strip_html_bio) |
70
|
|
|
|
|
|
|
{ |
71
|
|
|
|
|
|
|
$strip_html_bio = 0; |
72
|
|
|
|
|
|
|
} else { |
73
|
|
|
|
|
|
|
$strip_html_bio = $default_strip_html; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
$self->{_strip_html} = $strip_html_bio; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
sub set_key |
79
|
|
|
|
|
|
|
{ |
80
|
|
|
|
|
|
|
my $self = shift; |
81
|
|
|
|
|
|
|
my $key = shift; |
82
|
|
|
|
|
|
|
$self->{_key} = $key; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub set_timeout |
86
|
|
|
|
|
|
|
{ |
87
|
|
|
|
|
|
|
my $self = shift; |
88
|
|
|
|
|
|
|
my $timeout = shift || $default_timeout; |
89
|
|
|
|
|
|
|
$self->{_timeout} = $timeout; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub get_session |
93
|
|
|
|
|
|
|
{ |
94
|
|
|
|
|
|
|
my $self = shift; |
95
|
|
|
|
|
|
|
return $self->{session_id}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub get_list |
99
|
|
|
|
|
|
|
{ |
100
|
|
|
|
|
|
|
my $self = shift; |
101
|
|
|
|
|
|
|
my @tags; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
if ($self->{error}) |
104
|
|
|
|
|
|
|
{ |
105
|
|
|
|
|
|
|
push(@tags, $self->{error}); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
else |
108
|
|
|
|
|
|
|
{ |
109
|
|
|
|
|
|
|
foreach my $tag (sort keys %{$self}) |
110
|
|
|
|
|
|
|
{ |
111
|
|
|
|
|
|
|
push(@tags, $tag) if ($tag !~ m/^_.+/i && $tag !~ m/@.+/i && $tag ne "HamQTH" && $tag ne "search" && $tag ne "session"); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
return \@tags; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub get_bio |
118
|
|
|
|
|
|
|
{ |
119
|
|
|
|
|
|
|
my $self = shift; |
120
|
|
|
|
|
|
|
my $result = {}; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
if (!$self->{_callsign}) |
123
|
|
|
|
|
|
|
{ |
124
|
|
|
|
|
|
|
$self->{error} = "Ops!! ... Without a callsign I can not search anything"; |
125
|
|
|
|
|
|
|
&_clean_response($self); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
&_check_session_id($self); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
if (!$self->{_session_id}) |
131
|
|
|
|
|
|
|
{ |
132
|
|
|
|
|
|
|
$self->login; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
if ($self->{error}) |
136
|
|
|
|
|
|
|
{ |
137
|
|
|
|
|
|
|
$result->{error} = $self->{error}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
else |
140
|
|
|
|
|
|
|
{ |
141
|
|
|
|
|
|
|
my $url = "$qth_url/xml.php?id=".$self->{_session_id}."&callsign=".$self->{_callsign}."&prg=".$self->{_agent}; |
142
|
|
|
|
|
|
|
my $bio = $self->_get_content($url); |
143
|
|
|
|
|
|
|
$url = "$qth_url/xml_bio.php?id=".$self->{_session_id}."&callsign=".$self->{_callsign}."&prg=".$self->{_agent}."&strip_html=".$self->{_strip_html}; |
144
|
|
|
|
|
|
|
$bio = $self->_get_content($url); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
if (!$bio->{_session_id}) { |
147
|
|
|
|
|
|
|
$self->{error} = $bio->{error}; |
148
|
|
|
|
|
|
|
return undef; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
$result = &_clean_response($bio); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
return $result; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub get_dxcc |
156
|
|
|
|
|
|
|
{ |
157
|
|
|
|
|
|
|
my $self = shift; |
158
|
|
|
|
|
|
|
my $result = {}; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
if (!$self->{_callsign}) |
161
|
|
|
|
|
|
|
{ |
162
|
|
|
|
|
|
|
$self->{error} = "Ops!! ... Without a callsign I can not search anything"; |
163
|
|
|
|
|
|
|
&_clean_response($self); |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
&_check_session_id($self); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
if (!$self->{_session_id}) |
169
|
|
|
|
|
|
|
{ |
170
|
|
|
|
|
|
|
$self->login; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
if ($self->{error}) |
174
|
|
|
|
|
|
|
{ |
175
|
|
|
|
|
|
|
$result->{error} = $self->{error}; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
else |
178
|
|
|
|
|
|
|
{ |
179
|
|
|
|
|
|
|
#my $url = "$qth_url/xml.php?id=".$self->{_session_id}."&callsign=".$self->{_callsign}."&prg=".$self->{_agent}; |
180
|
|
|
|
|
|
|
#my $bio = $self->_get_content($url); |
181
|
|
|
|
|
|
|
my $url = "$qth_url/dxcc.php?id=".$self->{_session_id}."&callsign=".$self->{_callsign}."&prg=".$self->{_agent}; |
182
|
|
|
|
|
|
|
my $bio = $self->_get_content($url); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
if (!$bio->{_session_id}) { |
185
|
|
|
|
|
|
|
$self->{error} = $bio->{error}; |
186
|
|
|
|
|
|
|
return undef; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
if ($self->{adif} == 0) |
190
|
|
|
|
|
|
|
{ |
191
|
|
|
|
|
|
|
$self->{error} = "Not a valid DXCC"; |
192
|
|
|
|
|
|
|
&_clean_response($self); |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
$result = &_clean_response($bio); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
return $result; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# ----------------------- |
202
|
|
|
|
|
|
|
# PRIVATE SUBS |
203
|
|
|
|
|
|
|
# ----------------------- |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub _set_agent |
206
|
|
|
|
|
|
|
{ |
207
|
|
|
|
|
|
|
my $self = shift; |
208
|
|
|
|
|
|
|
$self->{_agent} = "Ham-Resources-HamQTH-$VERSION"; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub _get_content |
212
|
|
|
|
|
|
|
{ |
213
|
|
|
|
|
|
|
my ($self, $url) = @_; |
214
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new( timeout=>$self->{_timeout} ); |
215
|
|
|
|
|
|
|
$ua->agent( $self->{_agent} ); |
216
|
|
|
|
|
|
|
my $request = HTTP::Request->new('GET', $url); |
217
|
|
|
|
|
|
|
my $response = $ua->request($request); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
if (!$response->is_success) |
220
|
|
|
|
|
|
|
{ |
221
|
|
|
|
|
|
|
$self->{error} = "Ops! ... ".$response->{_msg}." - ".HTTP::Status::status_message($response->code); |
222
|
|
|
|
|
|
|
return undef; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
my $content = $response->content; |
226
|
|
|
|
|
|
|
$content =~ s/&/&/; # this character will crash the parser |
227
|
|
|
|
|
|
|
my $reader = XML::LibXML::Reader->new( encoding => 'UTF-8', string => $content ); |
228
|
|
|
|
|
|
|
my $tag_name; |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
while ($reader->read) |
231
|
|
|
|
|
|
|
{ |
232
|
|
|
|
|
|
|
$tag_name = $reader->name if ($reader->nodeType == 1); |
233
|
|
|
|
|
|
|
$self->{_session_id} = $reader->value if ($reader->nodeType == 3 && $tag_name eq "session_id"); |
234
|
|
|
|
|
|
|
$self->{$tag_name} = $reader->value if ($reader->nodeType == 3 && $tag_name ne "session_id"); |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
if (!$self->{error} || $self->{error} ne 'Callsign not found' || $self->{error} eq '' ) { |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
$self->{link} = $qth_url."/".$self->{_callsign} if length($self->{_callsign}) > 3; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
&_save_session_id($self); # save SESSION ID |
241
|
|
|
|
|
|
|
return $self; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
sub _clean_response |
245
|
|
|
|
|
|
|
{ |
246
|
|
|
|
|
|
|
my $self = shift; |
247
|
|
|
|
|
|
|
my $result = {}; |
248
|
|
|
|
|
|
|
foreach (sort keys %{$self}) |
249
|
|
|
|
|
|
|
{ |
250
|
|
|
|
|
|
|
if ($_ !~ m/^_.+/i && $_ !~ m/@.+/i && $_ ne "HamQTH" && $_ ne "search" && $_ ne "session" ) |
251
|
|
|
|
|
|
|
{ |
252
|
|
|
|
|
|
|
$result->{$_} = $self->{$_}; |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
return $result; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub _check_session_id |
259
|
|
|
|
|
|
|
{ |
260
|
|
|
|
|
|
|
my $self = shift; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
if (-e "hamqth_session.id") { |
263
|
|
|
|
|
|
|
open my $record_load, '<', 'hamqth_session.id' or $self->{error} = "Cannot open filename: $!"; |
264
|
|
|
|
|
|
|
if (not $self->{error}) { return; } |
265
|
|
|
|
|
|
|
($self->{_session_id},$self->{_timestamp}) = split("-", $record_load); |
266
|
|
|
|
|
|
|
close $record_load; |
267
|
|
|
|
|
|
|
&_check_timestamp($self); |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
else |
270
|
|
|
|
|
|
|
{ |
271
|
|
|
|
|
|
|
$self->login; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
sub _check_timestamp |
276
|
|
|
|
|
|
|
{ |
277
|
|
|
|
|
|
|
my $self = shift; |
278
|
|
|
|
|
|
|
my $time_actual_epoch = time(); |
279
|
|
|
|
|
|
|
my $timestamp_epoch = $self->{_timestamp} || 0; |
280
|
|
|
|
|
|
|
my $timestamp_epoch_plus_1h = $timestamp_epoch + (1*60*60); |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
if ($time_actual_epoch > $timestamp_epoch_plus_1h or $time_actual_epoch < $timestamp_epoch) |
283
|
|
|
|
|
|
|
{ |
284
|
|
|
|
|
|
|
$self->login; |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
sub _save_session_id |
289
|
|
|
|
|
|
|
{ |
290
|
|
|
|
|
|
|
my $self = shift; |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
open my $record_session, '>', 'hamqth_session.id' or $self->{error} = "Cannot open filename: $!"; |
293
|
|
|
|
|
|
|
my $my_session = $self->{_session_id}."-".time(); |
294
|
|
|
|
|
|
|
print $record_session $my_session; |
295
|
|
|
|
|
|
|
close $record_session; |
296
|
|
|
|
|
|
|
return $self; |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
1; |
301
|
|
|
|
|
|
|
__END__ |