| 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 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | require LWP::UserAgent; | |||||||||||||||||||||||||||
| 23 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | my $solar_url = 'http://www.wm7d.net/hamradio/solar'; | |||||||||||||||||||||||||||
| 28 | 2 | my $site_name = 'wm7d.net'; | |||||||||||||||||||||||||||
| 29 | 2 | my $default_timeout = 10; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | my $items = | |||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | 'sfi' => 'SFI: <font.*?><b>\s*(.*?)\s*</b></font>', | ||||||||||||||||||||||||||||
| 34 | 'a-index' => 'A-index: <font.*?><b>\s*(.*?)\s*</b></font>', | ||||||||||||||||||||||||||||
| 35 | 'a-index-text' => -1, # calculated field | ||||||||||||||||||||||||||||
| 36 | 'k-index' => 'K-Index: <font.*?><b>\s*(.*?)\s*</b></font>', | ||||||||||||||||||||||||||||
| 37 | 'k-index-text' => -1, # calculated field | ||||||||||||||||||||||||||||
| 38 | 'forecast' => '<b>Forecast for the next 24 hours:</b><br>\s*(.*?)\s*<br>', | ||||||||||||||||||||||||||||
| 39 | 'summary' => '<b>Summary for the past 24 hours:</b><br>\s*(.*?)\s*<br>', | ||||||||||||||||||||||||||||
| 40 | 'sunspots' => '<a .*?>Current Sunspot Count:\s*(.*?)\s*</a>', | ||||||||||||||||||||||||||||
| 41 | 'image' => -1, # calculated field | ||||||||||||||||||||||||||||
| 42 | 'image_thumbnail' => '(http://umbra.nascom.nasa.gov.*?\.gif)', | ||||||||||||||||||||||||||||
| 43 | 'time' => '<font.*?>Report last updated:\s*(.*?)\s*</font>' | ||||||||||||||||||||||||||||
| 44 | }; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub new | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 49 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 50 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | $self->{timeout} = $args{timeout} || $default_timeout; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{'timeout'} || $default_timeout
MC/DC0%0/2
$args{'timeout'} || $default_timeout
$args{'timeout'}
$default_timeout
Truth table0%0/3
A || B
A= $args{'timeout'}B= $default_timeout
| |||||||||||||||||||||||||||||
| 52 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 53 | 0 | _solar_init($self); | |||||||||||||||||||||||||||
| 54 | 0 | return $self; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub get | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 60 | 0 | my $item = shift; | |||||||||||||||||||||||||||
| 61 | 0 | $self->{$item}; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | sub set | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 0 | 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 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_hashref: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 0 | my $items = $self->all_item_names; | |||||||||||||||||||||||||||
| 77 | 0 | my $hash = {}; | |||||||||||||||||||||||||||
| 78 | 0 | foreach (sort @$items) { $hash->{$_} = $self->{$_} } | |||||||||||||||||||||||||||
| 79 | 0 | return $hash; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub all_item_names | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
all_item_names: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | 0 | my @item_names; | |||||||||||||||||||||||||||
| 86 | 0 | foreach (sort(keys %$items)) { push @item_names, $_ } | |||||||||||||||||||||||||||
| 87 | 0 | return \@item_names; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub is_error { my $self = shift; $self->{error_message} } | ||||||||||||||||||||||||||
|
Subroutine
is_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub error_message { my $self = shift; $self->{error_message} } | ||||||||||||||||||||||||||
|
Subroutine
error_message: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # ----------------------- | ||||||||||||||||||||||||||||
| 94 | # PRIVATE | ||||||||||||||||||||||||||||
| 95 | # ----------------------- | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub _solar_init | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_solar_init: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 99 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | my $content = $self->_get_content($solar_url) || return 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_get_content($solar_url) || (return 0)
MC/DC0%0/1
$self->_get_content($solar_url) || (return 0)
$self->_get_content($solar_url)
Truth table0%0/2
A
A= $self->_get_content($solar_url)
| |||||||||||||||||||||||||||||
| 101 | 0 | chomp $content; | |||||||||||||||||||||||||||
| 102 | 0 | $content =~ tr/\r\n//; | |||||||||||||||||||||||||||
| 103 | 0 | foreach my $item (keys %$items) | |||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | next if $item eq "-1"; # don't parse calculated fields | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ($content =~ s#$items->{$item}##i) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||
| 108 | 0 | $self->set($item,$1) | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | _calc_fields($self); | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (!$self->{sfi}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_calc_fields: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 122 | 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 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_content: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 131 | 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 | ▶ | if (!$response->is_success) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_remove_markup: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 157 | 0 | my $text = shift; | |||||||||||||||||||||||||||
| 158 | 0 | $text =~ s/<.*?>//g; | |||||||||||||||||||||||||||
| 159 | 0 | return $text; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub _get_a_text | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_a_text: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 164 | 0 | my $num = shift; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | return 'quiet' if $num >= 0 and $num <= 7; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$num >= 0 and $num <= 7
MC/DC0%0/2
$num >= 0 and $num <= 7
$num >= 0
$num <= 7
Truth table0%0/3
A and B
A= $num >= 0B= $num <= 7
| |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | return 'unsettled' if $num >= 8 and $num <= 15; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$num >= 8 and $num <= 15
MC/DC0%0/2
$num >= 8 and $num <= 15
$num >= 8
$num <= 15
Truth table0%0/3
A and B
A= $num >= 8B= $num <= 15
| |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | return 'active' if $num >= 16 and $num <= 29; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$num >= 16 and $num <= 29
MC/DC0%0/2
$num >= 16 and $num <= 29
$num >= 16
$num <= 29
Truth table0%0/3
A and B
A= $num >= 16B= $num <= 29
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | return 'minor storm' if $num >= 30 and $num <= 49; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$num >= 30 and $num <= 49
MC/DC0%0/2
$num >= 30 and $num <= 49
$num >= 30
$num <= 49
Truth table0%0/3
A and B
A= $num >= 30B= $num <= 49
| |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | return 'major storm' if $num >= 50 and $num <= 99; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$num >= 50 and $num <= 99
MC/DC0%0/2
$num >= 50 and $num <= 99
$num >= 50
$num <= 99
Truth table0%0/3
A and B
A= $num >= 50B= $num <= 99
| |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | return 'severe storm' if $num >= 100 and $num <= 400; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$num >= 100 and $num <= 400
MC/DC0%0/2
$num >= 100 and $num <= 400
$num >= 100
$num <= 400
Truth table0%0/3
A and B
A= $num >= 100B= $num <= 400
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub _get_k_text | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_k_text: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 175 | 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 | 2 | 1; | |||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||