Skip to content

Commit f27c28a

Browse files
authored
Merge pull request hollie#787 from ricks03/patch-1
update internet_usgs.pl
2 parents 8c24ec6 + 75e96e5 commit f27c28a

File tree

1 file changed

+63
-98
lines changed

1 file changed

+63
-98
lines changed

code/public/internet_usgs.pl

Lines changed: 63 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
# Last Updated:
1010
# 20131224 - USGS adjusted the URL due to a security vulnerability
11+
# 20190828 - USGS now using https://
1112

1213
####################
1314
## to make requests of specific site numbers and email if the water is up
@@ -22,158 +23,120 @@
2223
## comma-delimited list of other people to send mail when the water is up
2324
## mh.ini: USGS_Temp = minimum temperature your friends are willing to go kayaking
2425
## mh.ini: weather_use_internet = 1
25-
# Note that this uses the outside temp. If you don't have a weather station set up, you need to be using
26-
# weather_use_internet=1 in mh.private.ini, have internet_weather enabled, and have all of that working
26+
# Note that this uses the outside temp. If you don't have a weather station set up, you need to be using
27+
# weather_use_internet=1 in mh.private.ini, have internet_weather enabled, and have all of that working
2728
# correctly.
28-
####################
29+
####################
2930

30-
my $USGS_URL; # The URL to build the request to the USGS
31-
my %USGS_Notify; # The River level to send notification
32-
my %USGS_Level; # The current River Level
33-
my %USGS_Name; # The River Name/Location
31+
my $USGS_URL; # The URL to build the request to the USGS
32+
my %USGS_Notify; # The River level to send notification
33+
my %USGS_Level; # The current River Level
34+
my %USGS_Name; # The River Name/Location
3435

35-
my @MH_Rivers = split( / /, $config_parms{USGS_Monitor} ); # The River IDs and Notification level
36+
my @MH_Rivers = split(/ /,$config_parms{USGS_Monitor}); # The River IDs and Notification level
3637

3738
my $f_USGS_list = "$config_parms{data_dir}/web/USGS.txt";
3839
my $f_USGS_html = "$config_parms{data_dir}/web/USGS.html";
3940

4041
# noloop=start
4142

4243
# Build the URL
43-
$USGS_URL = "http://waterdata.usgs.gov/nwis/current?multiple_site_no=";
44+
$USGS_URL = "https://waterdata.usgs.gov/nwis/current?multiple_site_no=";
4445

4546
#Add all of the rivers
46-
for ( my $i = 0; $i <= @MH_Rivers; $i += 2 ) {
47-
$USGS_URL .= @MH_Rivers[$i] . "%2C";
48-
$USGS_Notify{"@MH_Rivers[$i]"} = @MH_Rivers[ $i + 1 ];
47+
for (my $i = 0; $i <= @MH_Rivers; $i+=2) {
48+
$USGS_URL .= @MH_Rivers[$i] . "%2C";
49+
$USGS_Notify{"@MH_Rivers[$i]"} = @MH_Rivers[$i+1];
4950
}
5051

5152
#More of the general URL
52-
$USGS_URL .= "&search_site_no_match_type=exact&index_pmcode_STATION_NM=1&index_pmcode_DATETIME=2";
53-
53+
$USGS_URL .= "&search_site_no_match_type=exact&index_pmcode_STATION_NM=1&index_pmcode_DATETIME=2";
5454
# Add whether CFS or Level
55-
if ( $config_parms{USGS_Rate} ) { $USGS_URL .= "&index_pmcode_00065=3"; }
56-
else { $USGS_URL .= "&index_pmcode_00060=3"; }
57-
55+
if ($config_parms{USGS_Rate}) { $USGS_URL .= "&index_pmcode_00065=3"; }
56+
else { $USGS_URL .= "&index_pmcode_00060=3"; }
5857
# And the last of the URL
59-
$USGS_URL .=
60-
"&sort_key=site_no&group_key=NONE&sitefile_output_format=html_table&column_name=agency_cd&column_name=site_no&column_name=station_nm&column_name=lat_va&column_name=long_va&column_name=state_cd&column_name=county_cd&column_name=alt_va&column_name=huc_cd&sort_key_2=site_no&html_table_group_key=NONE&format=rdb&rdb_compression=value&list_of_search_criteria=multiple_site_no%2Crealtime_parameter_selection";
58+
$USGS_URL .= "&sort_key=site_no&group_key=NONE&sitefile_output_format=html_table&column_name=agency_cd&column_name=site_no&column_name=station_nm&column_name=lat_va&column_name=long_va&column_name=state_cd&column_name=county_cd&column_name=alt_va&column_name=huc_cd&sort_key_2=site_no&html_table_group_key=NONE&format=rdb&rdb_compression=value&list_of_search_criteria=multiple_site_no%2Crealtime_parameter_selection";
6159

6260
# noloop=stop
6361

64-
$p_USGS_list = new Process_Item("get_url $USGS_URL $f_USGS_html");
62+
$p_USGS_list = new Process_Item("get_url -ua $USGS_URL $f_USGS_html");
6563

6664
#############################################
6765

68-
$v_USGS_list = new Voice_Cmd('[Get,Read,Show] water levels');
69-
$v_USGS_list->set_info('This is the USGS Water Level for selected rivers.');
66+
$v_USGS_list = new Voice_Cmd('[Get,Read,Show] water levels');
67+
$v_USGS_list -> set_info('This is the USGS Water Level for selected rivers.');
7068

7169
$state = said $v_USGS_list;
7270

7371
#Check to see if there's anything in the file before speaking
7472
#speak (text => $f_USGS_list, display => 0) if ($state eq 'Read' && file_size $f_USGS_list);
75-
if ( $state eq 'Read' ) {
76-
if ( file_size $f_USGS_list) { speak text => "app=usgs $f_USGS_list"; }
77-
else { speak text => 'app=usgs No water anywhere!'; }
78-
}
79-
elsif ( $state eq 'Show' ) {
80-
if ( file_size $f_USGS_list) {
81-
respond
82-
text => $f_USGS_list,
83-
time => 240,
84-
font => 'Times 25 bold',
85-
geometry => '+0+0',
86-
width => 36,
87-
height => 12;
88-
}
89-
else {
90-
respond
91-
text => 'No water anywhere!',
92-
time => 240,
93-
font => 'Times 25 bold',
94-
geometry => '+0+0',
95-
width => 36,
96-
height => 12;
97-
}
98-
}
99-
elsif ( $state eq 'Get' ) {
100-
73+
if ($state eq 'Read') {
74+
if (file_size $f_USGS_list) { speak text => "app=usgs $f_USGS_list"; } else { speak text => 'app=usgs No water anywhere!';}
75+
} elsif ($state eq 'Show') {
76+
if (file_size $f_USGS_list) { respond text => $f_USGS_list, time => 240, font => 'Times 25 bold', geometry => '+0+0', width => 36, height => 12; }
77+
else { respond text => 'No water anywhere!', time => 240, font => 'Times 25 bold', geometry => '+0+0', width => 36, height => 12; }
78+
} elsif ($state eq 'Get') {
10179
# Do this only if the file has not already been updated in the last hour
102-
if ( -s $f_USGS_html > 10
103-
and time_date_stamp( 3, $f_USGS_html ) eq time_date_stamp(3) )
104-
{
80+
if (-s $f_USGS_html > 10 and
81+
time_date_stamp(3, $f_USGS_html) eq time_date_stamp(3)) {
10582
print_log "USGS list is current";
106-
107-
# fixed 100219 looks like it spoke, then set things to read, and then read it again.
108-
#speak text => "app=usgs $f_USGS_list" if file_size $f_USGS_list;
109-
start $p_USGS_list 'do_nothing'; # Fire the process with no-op, so we can still run the parsing code for debug
83+
# fixed 100219 looks like it spoke, then set things to read, and then read it again.
84+
#speak text => "app=usgs $f_USGS_list" if file_size $f_USGS_list;
85+
start $p_USGS_list 'do_nothing'; # Fire the process with no-op, so we can still run the parsing code for debug
11086
}
11187
else {
11288
if (&net_connect_check) {
11389
print_log "Retrieving USGS list from the net ...";
114-
11590
# Use start instead of run so we can detect when it is done
11691
start $p_USGS_list;
11792
}
11893
else { speak "app=usgs Sorry, you must be logged onto the net"; }
119-
}
120-
&respond_wait; # Tell web browser to wait for respond
94+
}
95+
&respond_wait; # Tell web browser to wait for respond
12196
}
12297

123-
if ( done_now $p_USGS_list) {
98+
if (done_now $p_USGS_list) {
12499
my $text = file_read $f_USGS_html;
125100

126101
#To clean up the CGI USGS Site
127102
# clean up the top explanation
128103
$text =~ s/#.+#//s;
129104
print "Text1 = $text\n";
130-
131105
#To clean up the table headers
132106
$text =~ s/^.+?(USGS)/$1/s;
133107

134108
# Split out the values
135-
my @USGS_RiverList = split( /\n/, $text );
136-
my $USGS_River = "";
109+
my @USGS_RiverList = split(/\n/,$text);
110+
my $USGS_River = "";
137111
my @USGS_RiverValues = ();
138-
my $USGS_Message = "";
112+
my $USGS_Message = "";
139113
foreach $USGS_River (@USGS_RiverList) {
140-
@USGS_RiverValues = split( "\t", $USGS_River );
141-
$USGS_Level{"@USGS_RiverValues[1]"} = @USGS_RiverValues[7];
142-
$USGS_Name{"@USGS_RiverValues[1]"} = @USGS_RiverValues[2];
143-
print "Result: " . $USGS_Level{"@USGS_RiverValues[1]"} . $USGS_Name{"@USGS_RiverValues[1]"} . "\n";
144-
if ( ( $USGS_Level{"@USGS_RiverValues[1]"} >= $USGS_Notify{"@USGS_RiverValues[1]"} )
145-
&& $USGS_Level{"@USGS_RiverValues[1]"} ne "" )
146-
{
147-
$USGS_Message .= $USGS_Name{"@USGS_RiverValues[1]"} . " is at " . $USGS_Level{"@USGS_RiverValues[1]"} . ".\n";
148-
}
114+
@USGS_RiverValues = split("\t", $USGS_River);
115+
$USGS_Level{"@USGS_RiverValues[1]"} = @USGS_RiverValues[7];
116+
$USGS_Name{"@USGS_RiverValues[1]"} = @USGS_RiverValues[2];
117+
print "Result: " . $USGS_Level{"@USGS_RiverValues[1]"} . $USGS_Name{"@USGS_RiverValues[1]"} . "\n";
118+
if (($USGS_Level{"@USGS_RiverValues[1]"} >= $USGS_Notify{"@USGS_RiverValues[1]"} ) && $USGS_Level{"@USGS_RiverValues[1]"} ne "") {
119+
$USGS_Message .= $USGS_Name{"@USGS_RiverValues[1]"} . " is at " . $USGS_Level{"@USGS_RiverValues[1]"} . ".\n";
120+
}
149121
}
150-
151122
# Send mail if the rivers are up.
152123
if ($USGS_Message) {
153-
print_log("Sending notification that rivers are up!");
154-
155-
# Notify friends that they need to go if it's warm enough!
156-
if ( ( $Weather{TempOutdoor} >= $config_parms{USGS_Temp} )
157-
&& ( $config_parms{USGS_Friends} ) )
158-
{
159-
print_log("Notifying friends who need to know!");
160-
&net_mail_send(
161-
subject => "Woohoo! Rivers are up",
162-
to => $config_parms{USGS_Friends},
163-
text => $USGS_Message
164-
);
165-
}
166-
else { # Email me anyway even if it's too cold
167-
$USGS_Message .= "Current temp is " . $Weather{TempOutdoor} . " degrees.\n";
168-
&net_mail_send(
169-
subject => "Woohoo! Rivers are up",
170-
text => $USGS_Message
171-
);
172-
}
173-
174-
file_write( $f_USGS_list, $USGS_Message );
175-
set $v_USGS_list 'Read';
176-
}
124+
print_log ("Sending notification that rivers are up!");
125+
# Notify friends that they need to go if it's warm enough!
126+
if (($Weather{TempOutdoor} >= $config_parms{USGS_Temp}) && ($config_parms{USGS_Friends})) {
127+
print_log ( "Notifying friends who need to know!");
128+
&net_mail_send(subject => "Woohoo! Rivers are up",
129+
to => $config_parms{USGS_Friends},
130+
text => $USGS_Message);
131+
} else { # Email me anyway even if it's too cold
132+
$USGS_Message .= "Current temp is " . $Weather{TempOutdoor} . " degrees.\n";
133+
&net_mail_send(subject => "Woohoo! Rivers are up",
134+
text => $USGS_Message);
135+
}
136+
137+
file_write($f_USGS_list, $USGS_Message);
138+
set $v_USGS_list 'Read';
139+
}
177140
}
178141

179142
###########################################################
@@ -186,4 +149,6 @@
186149
# 1.05 fixed so when there's nothing to report it doesn't try to talk.
187150
# 1.06 fixed for correct outside weather.
188151
# 1.07 Fixed logic error in how multiple notifications work
189-
# 1.08 120611 Fixed from changes on the USGS site
152+
# 1.08 20120611 Fixed from changes on the USGS site
153+
# 1.09 20131224 USGS adjusted the URL due to a security vulnerability
154+
# 1.10 20190827 Added https support, as the USGS site went to only https 12/2018

0 commit comments

Comments
 (0)