blib/lib/Ham/Reference/Solar.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 9 | 80 | 11.2 |
branch | 0 | 20 | 0.0 |
condition | 0 | 23 | 0.0 |
subroutine | 3 | 16 | 18.7 |
pod | 7 | 7 | 100.0 |
total | 19 | 146 | 13.0 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Ham::Reference::Solar; | ||||||
2 | |||||||
3 | # -------------------------------------------------------------------------- | ||||||
4 | # Ham::Reference::Solar - A scraper to return solar data useful for | ||||||
5 | # Amateur Radio applications. | ||||||
6 | # | ||||||
7 | # Copyright (c) 2008-2010 Brad McConahay N8QQ. | ||||||
8 | # Cincinnat, Ohio USA | ||||||
9 | # | ||||||
10 | # This module is free software; you can redistribute it and/or | ||||||
11 | # modify it under the terms of the Artistic License 2.0. For | ||||||
12 | # details, see the full text of the license in the file LICENSE. | ||||||
13 | # | ||||||
14 | # This program is distributed in the hope that it will be | ||||||
15 | # useful, but it is provided "as is" and without any express | ||||||
16 | # or implied warranties. For details, see the full text of | ||||||
17 | # the license in the file LICENSE. | ||||||
18 | # -------------------------------------------------------------------------- | ||||||
19 | |||||||
20 | 1 | 1 | 23081 | use strict; | |||
1 | 3 | ||||||
1 | 37 | ||||||
21 | 1 | 1 | 5 | use warnings; | |||
1 | 2 | ||||||
1 | 51 | ||||||
22 | require LWP::UserAgent; | ||||||
23 | 1 | 1 | 5 | use vars qw($VERSION); | |||
1 | 6 | ||||||
1 | 1094 | ||||||
24 | |||||||
25 | our $VERSION = '0.03'; | ||||||
26 | |||||||
27 | my $solar_url = 'http://www.wm7d.net/hamradio/solar'; | ||||||
28 | my $site_name = 'wm7d.net'; | ||||||
29 | my $default_timeout = 10; | ||||||
30 | |||||||
31 | my $items = | ||||||
32 | { | ||||||
33 | 'sfi' => 'SFI: |
||||||
34 | 'a-index' => 'A-index: |
||||||
35 | 'a-index-text' => -1, # calculated field | ||||||
36 | 'k-index' => 'K-Index: |
||||||
37 | 'k-index-text' => -1, # calculated field | ||||||
38 | 'forecast' => 'Forecast for the next 24 hours: \s*(.*?)\s* ', |
||||||
39 | 'summary' => 'Summary for the past 24 hours: \s*(.*?)\s* ', |
||||||
40 | 'sunspots' => 'Current Sunspot Count:\s*(.*?)\s*', | ||||||
41 | 'image' => -1, # calculated field | ||||||
42 | 'image_thumbnail' => '(http://umbra.nascom.nasa.gov.*?\.gif)', | ||||||
43 | 'time' => ' |
||||||
44 | }; | ||||||
45 | |||||||
46 | sub new | ||||||
47 | { | ||||||
48 | 0 | 0 | 1 | my $class = shift; | |||
49 | 0 | my %args = @_; | |||||
50 | 0 | my $self = {}; | |||||
51 | 0 | 0 | $self->{timeout} = $args{timeout} || $default_timeout; | ||||
52 | 0 | bless $self, $class; | |||||
53 | 0 | _solar_init($self); | |||||
54 | 0 | return $self; | |||||
55 | } | ||||||
56 | |||||||
57 | sub get | ||||||
58 | { | ||||||
59 | 0 | 0 | 1 | my $self = shift; | |||
60 | 0 | my $item = shift; | |||||
61 | 0 | $self->{$item}; | |||||
62 | } | ||||||
63 | |||||||
64 | sub set | ||||||
65 | { | ||||||
66 | 0 | 0 | 1 | my $self = shift; | |||
67 | 0 | my $item = shift; | |||||
68 | 0 | my $value = shift; | |||||
69 | # $self->{$item} = _remove_markup($value); | ||||||
70 | 0 | $self->{$item} = $value; | |||||
71 | } | ||||||
72 | |||||||
73 | sub get_hashref | ||||||
74 | { | ||||||
75 | 0 | 0 | 1 | my $self = shift; | |||
76 | 0 | my $items = $self->all_item_names; | |||||
77 | 0 | my $hash = {}; | |||||
78 | 0 | foreach (sort @$items) { $hash->{$_} = $self->{$_} } | |||||
0 | |||||||
79 | 0 | return $hash; | |||||
80 | } | ||||||
81 | |||||||
82 | sub all_item_names | ||||||
83 | { | ||||||
84 | 0 | 0 | 1 | my $self = shift; | |||
85 | 0 | my @item_names; | |||||
86 | 0 | foreach (sort(keys %$items)) { push @item_names, $_ } | |||||
0 | |||||||
87 | 0 | return \@item_names; | |||||
88 | } | ||||||
89 | |||||||
90 | 0 | 0 | 1 | sub is_error { my $self = shift; $self->{error_message} } | |||
0 | |||||||
91 | 0 | 0 | 1 | sub error_message { my $self = shift; $self->{error_message} } | |||
0 | |||||||
92 | |||||||
93 | # ----------------------- | ||||||
94 | # PRIVATE | ||||||
95 | # ----------------------- | ||||||
96 | |||||||
97 | sub _solar_init | ||||||
98 | { | ||||||
99 | 0 | 0 | my $self = shift; | ||||
100 | 0 | 0 | my $content = $self->_get_content($solar_url) || return 0; | ||||
101 | 0 | chomp $content; | |||||
102 | 0 | $content =~ tr/\r\n//; | |||||
103 | 0 | foreach my $item (keys %$items) | |||||
104 | { | ||||||
105 | 0 | 0 | next if $item eq "-1"; # don't parse calculated fields | ||||
106 | 0 | 0 | if ($content =~ s#$items->{$item}##i) | ||||
107 | { | ||||||
108 | 0 | $self->set($item,$1) | |||||
109 | } | ||||||
110 | } | ||||||
111 | 0 | _calc_fields($self); | |||||
112 | 0 | 0 | if (!$self->{sfi}) | ||||
113 | { | ||||||
114 | 0 | $self->{is_error} = 1; | |||||
115 | 0 | $self->{error_message} = "Data Parsing error - Format at $site_name may have changed"; | |||||
116 | 0 | return 0; | |||||
117 | } | ||||||
118 | } | ||||||
119 | |||||||
120 | sub _calc_fields | ||||||
121 | { | ||||||
122 | 0 | 0 | my $self = shift; | ||||
123 | 0 | $self->{'a-index-text'} = _get_a_text($self->{'a-index'}); | |||||
124 | 0 | $self->{'k-index-text'} = _get_k_text($self->{'k-index'}); | |||||
125 | 0 | $self->{'image'} = $self->{'image_thumbnail'}; | |||||
126 | 0 | $self->{'image'} =~ s/_thumbnail//; | |||||
127 | } | ||||||
128 | |||||||
129 | sub _get_content | ||||||
130 | { | ||||||
131 | 0 | 0 | my $self = shift; | ||||
132 | 0 | my $url = shift; | |||||
133 | 0 | my $ua = LWP::UserAgent->new( timeout=>$self->{timeout} ); | |||||
134 | 0 | $ua->agent("Ham/Reference/Solar.pm $VERSION"); | |||||
135 | 0 | my $request = HTTP::Request->new('GET', $url); | |||||
136 | 0 | my $response = $ua->request($request); | |||||
137 | 0 | 0 | if (!$response->is_success) | ||||
138 | { | ||||||
139 | 0 | $self->{is_error} = 1; | |||||
140 | 0 | $self->{error_message} = "Error at $site_name - ".HTTP::Status::status_message($response->code); | |||||
141 | 0 | return 0; | |||||
142 | } | ||||||
143 | 0 | return $response->content; | |||||
144 | } | ||||||
145 | |||||||
146 | #sub _items | ||||||
147 | #{ | ||||||
148 | # my $self = shift; | ||||||
149 | # my @item_names; | ||||||
150 | # my $items = $self->all_item_names; | ||||||
151 | # foreach (sort @$items) { push @item_names, $_ if $self->{$_} } | ||||||
152 | # return \@item_names; | ||||||
153 | #} | ||||||
154 | |||||||
155 | sub _remove_markup | ||||||
156 | { | ||||||
157 | 0 | 0 | my $text = shift; | ||||
158 | 0 | $text =~ s/<.*?>//g; | |||||
159 | 0 | return $text; | |||||
160 | } | ||||||
161 | |||||||
162 | sub _get_a_text | ||||||
163 | { | ||||||
164 | 0 | 0 | my $num = shift; | ||||
165 | 0 | 0 | 0 | return 'quiet' if $num >= 0 and $num <= 7; | |||
166 | 0 | 0 | 0 | return 'unsettled' if $num >= 8 and $num <= 15; | |||
167 | 0 | 0 | 0 | return 'active' if $num >= 16 and $num <= 29; | |||
168 | 0 | 0 | 0 | return 'minor storm' if $num >= 30 and $num <= 49; | |||
169 | 0 | 0 | 0 | return 'major storm' if $num >= 50 and $num <= 99; | |||
170 | 0 | 0 | 0 | return 'severe storm' if $num >= 100 and $num <= 400; | |||
171 | } | ||||||
172 | |||||||
173 | sub _get_k_text | ||||||
174 | { | ||||||
175 | 0 | 0 | my $num = shift; | ||||
176 | 0 | my @text = ( | |||||
177 | 'inactive', | ||||||
178 | 'very quiet', | ||||||
179 | 'quiet', | ||||||
180 | 'unsettled', | ||||||
181 | 'active', | ||||||
182 | 'minor storm', | ||||||
183 | 'major storm', | ||||||
184 | 'severe storm', | ||||||
185 | 'very severe storm', | ||||||
186 | 'extremely severe storm', | ||||||
187 | ); | ||||||
188 | 0 | return $text[$num]; | |||||
189 | } | ||||||
190 | |||||||
191 | 1; | ||||||
192 | __END__ |