| 1 | # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*- | ||
| 2 | # vim:ts=8:sw=2:et:sta:sts=2 | ||
| 3 | ######### | ||
| 4 | # Author: rmp@psyphi.net | ||
| 5 | # Created: 2016-12-31 | ||
| 6 | # | ||
| 7 | package Ham::NOAA::Sunspot; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use LWP::UserAgent; | ||
| 11 | |||
| 12 | our $DEFAULT_URL = q[http://services.swpc.noaa.gov/text/predicted-sunspot-radio-flux.txt]; | ||
| 13 | our $VERSION = q[0.0.1]; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ($class, $ref) = @_; | ||
| 17 | if(!ref $ref) { | ||
| 18 | $ref = {}; | ||
| 19 | } | ||
| 20 | |||
| 21 | bless $ref, $class; | ||
| 22 | return $ref; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub url { | ||
| 26 | my ($self, $url) = @_; | ||
| 27 | if(defined $url) { | ||
| 28 | $self->{url} = $url; | ||
| 29 | } | ||
| 30 | return $self->{url} || $DEFAULT_URL; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _data { | ||
| 34 | my ($self) = @_; | ||
| 35 | if($self->{_data}) { | ||
| 36 | return $self->{_data}; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $ua = LWP::UserAgent->new(); | ||
| 40 | $ua->agent(qq[Ham::NOAA::Sunspot $VERSION]); | ||
| 41 | $ua->env_proxy(); | ||
| 42 | |||
| 43 | my $res = $ua->get($self->url); | ||
| 44 | if(!$res->is_success) { | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | my $content = $res->decoded_content; | ||
| 49 | my $lines = [split /[\r\n]+/smx, $content]; | ||
| 50 | my $data = []; | ||
| 51 | |||
| 52 | for my $line (@{$lines}) { | ||
| 53 | if($line =~ m{^\s*[#:]}smx) { | ||
| 54 | next; | ||
| 55 | } | ||
| 56 | |||
| 57 | my ($year, $month, $sunspot_predicted, $sunspot_high, $sunspot_low, $flux_predicted, $flux_high, $flux_low) = $line =~ m{(\S+)}smxg; | ||
| 58 | push @{$data}, { | ||
| 59 | year => 0+$year, | ||
| 60 | month => 0+$month, | ||
| 61 | sunspot => { | ||
| 62 | predicted => $sunspot_predicted, | ||
| 63 | high => $sunspot_high, | ||
| 64 | low => $sunspot_low, | ||
| 65 | }, | ||
| 66 | flux => { | ||
| 67 | predicted => $flux_predicted, | ||
| 68 | high => $flux_high, | ||
| 69 | low => $flux_low, | ||
| 70 | }, | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 74 | $self->{_data} = $data; | ||
| 75 | return $data; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _by_year_month { | ||
| 79 | my $self = shift; | ||
| 80 | if($self->{_year_month}) { | ||
| 81 | return $self->{_year_month}; | ||
| 82 | } | ||
| 83 | |||
| 84 | my $data = $self->_data; | ||
| 85 | for my $i (@{$data}) { | ||
| 86 | $self->{_year_month}->{$i->{year}}->{$i->{month}} = { | ||
| 87 | sunspot => $i->{sunspot}, | ||
| 88 | flux => $i->{flux}, | ||
| 89 | }; | ||
| 90 | } | ||
| 91 | |||
| 92 | return $self->{_year_month}; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub sunspot_by_year_month { | ||
| 96 | my ($self, $year, $month) = @_; | ||
| 97 | |||
| 98 | return $self->_by_year_month->{0+$year}->{0+$month}->{sunspot}; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub flux_by_year_month { | ||
| 102 | my ($self, $year, $month) = @_; | ||
| 103 | |||
| 104 | return $self->_by_year_month->{0+$year}->{0+$month}->{flux}; | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; | ||
| 108 | |||
| 109 | __END__ |