#!/usr/local/bin/perl # Reformat-Hourly-Weather.pl # # Mostly finished on July 12, 2023. # # Tidied up and published July 14, 2023. # # # Released under the GNU Affero General Public License 3.0: # # https://www.gnu.org/licenses/agpl-3.0.en.html # # # Paste raw copied-and-pasted hourly weather forecasts from Weather.com # into "inputfile.txt" in the same folder as this script. # # Then, simply run the script, and the raw input will be # converted into a nicer format and placed in "outputfile.txt". # # If you run into any problems, check "debug-output.txt" for clues. # # # Based on a messy (but effective enough) old script I wrote in # perhaps 2006. # # I wish it was more elegant, but being overly perfectionistic about # that when it already does what I need it to do doesn't seem like it # would be the best use of my limited energy. use strict; use warnings; use Text::Wrap; use constant false => 0; use constant true => 1; ####################### # # Some settings you can change. # # my $inputfile="inputfile.txt"; my $outputfile="outputfile.txt"; my $debugfile="debug-output.txt"; # # # End of settings you can change. # # ####################### my @input_file_line; # contains the text file grabbed. my $numofinputlines=0; # contains number of lines in file my $numoflines=0; my $Search_String____Comment=">"; my $Search_String____Hourly_Weather="^Hourly Weather"; my $Search_String____As_of="^As of "; my $Search_String____Day="day, "; my $Search_String____Time=":[0-9][0-9] [a|p]m"; #my $Search_String____Time="[0-9]+ [a|p]m"; my $Search_String____Degree="°"; my $Search_String____Percent="%"; my $Search_String____Wind=" mph"; my $Search_String____Feels_Like="Feels Like[0-9]"; my $BLANKLINE=111; my $NULLLINE=112; my $COMMENTLINE=222; my $NOT_HOURLY_WEATHER_LINE=700; my $HOURLY_WEATHER_LINE=701; my $AS_OF_LINE=702; my $DAY_OF_WEEK_LINE=703; my $TIME_LINE=704; my $WEATHER_LINE=705; my $DEGREE_LINE=706; my $PERCENT_LINE=707; my $WIND_LINE=708; my $FEELS_LIKE_SECTION_LINE=709; my $RAIN_AMOUNT_LINE=710; my $FEELS_LIKE_LINE=711; my $DEFAULT_MODE=555; my $MODE=$DEFAULT_MODE; my $should_find_Hourly_Weather_line=true; my $now_in_feels_like_section=false; my $Final____feels_like_section; my $feels_like_section; my $now_in_an_hour; my $should_assume_this_is_a_weather_line=false; my $Hour____Time; my $Hour____Last_Time=""; my $Hour____Weather; my $Hour____Degree; my $Hour____Percent; my $Hour____Wind; my $Final____Time; my $Final____Percent; my $Final____Degree; my $Final____Weather; my $Final____Wind; sub ltrim { my $s = shift; $s =~ s/^\s+//; return $s }; main(); sub main { OpenFiles(); GrabInput(); ProcessInput(); print "\n\nDone; results should be in $outputfile.\n\n"; exit; } sub OpenFiles { open(INPUTFILE,$inputfile); open(RESULTS, ">$outputfile"); open(DEBUG, ">$debugfile"); } sub GrabInput { @input_file_line=; #slurps the entire file into one array #each array entry contains a single line $numofinputlines=@input_file_line; } sub GetInputLine { my $thisline=$_[0]; return $input_file_line[$thisline]; } sub ProcessInput() { my $atline; my $linestring; my $linetype; for ($atline=0;$atline<$numofinputlines;$atline++) { $linestring=GetInputLine($atline); # $linestring=~s/\n//; #removes the newline at the end chomp $linestring; chomp $linestring; print DEBUG "Current line $atline: '$linestring'"; $linetype=LineType($linestring); if ($MODE==$DEFAULT_MODE) { if ($linetype==$TIME_LINE) { if ( $now_in_feels_like_section == true ) { #$Text::Wrap::columns = 36; #$Text::Wrap::columns = 80; #$Final____feels_like_section=wrap('', '', $feels_like_section); $Final____feels_like_section=$feels_like_section; print RESULTS "\n$Final____feels_like_section"; $now_in_feels_like_section = false; } $Hour____Time=$linestring; #print "\nHour____Time: '$Hour____Time' "; #print "Hour____Last_Time: '$Hour____Last_Time'"; if ( $Hour____Time eq $Hour____Last_Time ) { return true; } $now_in_an_hour=true; $should_assume_this_is_a_weather_line=true; next; } if ($now_in_an_hour=true) { if ($linetype==$WEATHER_LINE) { $Hour____Weather=$linestring; } if ($linetype==$DEGREE_LINE) { $Hour____Degree=$linestring; } if ($linetype==$PERCENT_LINE) { $Hour____Percent=$linestring; } if ($linetype==$WIND_LINE) { $Hour____Last_Time=$Hour____Time; $Hour____Wind=$linestring; $now_in_an_hour=false; $Final____Time=sprintf "%8s", $Hour____Time; $Final____Percent=sprintf "%3s", $Hour____Percent; $Final____Degree=sprintf "%5s", $Hour____Degree; #$Final____Weather=sprintf "%-22s", $Hour____Weather; $Final____Weather=$Hour____Weather; $Final____Wind=sprintf "%10s", $Hour____Wind; print RESULTS "\n$Final____Wind $Final____Time $Final____Degree $Final____Percent $Final____Weather"; } } if ($linetype==$FEELS_LIKE_LINE) { $linestring =~ s/Like/Like /; $feels_like_section=$linestring; $now_in_feels_like_section=true; next; } if ( $linetype==$FEELS_LIKE_SECTION_LINE ) { $linestring =~ s/Wind/Wind /; $linestring =~ s/Humidity/Humidity /; $linestring =~ s/Index/Index /; $linestring =~ s/Cover/Cover /; $linestring =~ s/Amount/Amount /; my $Search_String____Rain_Amount='[\s]*Rain Amount'; if ($linestring =~ $Search_String____Rain_Amount ) { $linestring=ltrim($linestring); } $feels_like_section="$feels_like_section - $linestring"; } if ($linetype==$HOURLY_WEATHER_LINE) { $linestring =~ s/[-]/ - /; print RESULTS $linestring; } if ($linetype==$AS_OF_LINE) { print RESULTS "\n$linestring"; } if ($linetype==$DAY_OF_WEEK_LINE) { print RESULTS "\n\n$linestring"; } } }; }; sub LineType { my $thisline=$_[0]; my $thislinelength=length($thisline); print DEBUG "Line: '$thisline' Line length: $thislinelength\n\n"; if ( $should_assume_this_is_a_weather_line == true ) { print DEBUG "Returning WEATHER_LINE\n\n"; $should_assume_this_is_a_weather_line=false; return $WEATHER_LINE; } if ($thisline =~ $Search_String____Feels_Like ) { print DEBUG "Returning FEELS_LIKE_LINE\n\n"; return $FEELS_LIKE_LINE; } if ( $should_find_Hourly_Weather_line == true ) { if ($thisline =~ $Search_String____Hourly_Weather ) { print DEBUG "Returning HOURLY_WEATHER_LINE\n\n"; $should_find_Hourly_Weather_line = false; return $HOURLY_WEATHER_LINE; } else { print DEBUG "Returning NOT_HOURLY_WEATHER_LINE\n\n"; return $NOT_HOURLY_WEATHER_LINE; }; }; if ($thisline =~ $Search_String____As_of ) { print DEBUG "Returning AS_OF_LINE\n\n"; return $AS_OF_LINE; } if ($thisline =~ $Search_String____Time ) { print DEBUG "Returning TIME_LINE\n\n"; return $TIME_LINE; } if ($thislinelength<=1) { print DEBUG "Returning BLANKLINE\n\n"; return $BLANKLINE; }; if ( $now_in_feels_like_section == true ) { print DEBUG "Returning FEELS_LIKE_SECTION_LINE\n\n"; return $FEELS_LIKE_SECTION_LINE; } if ($thisline =~ $Search_String____Day ) { print DEBUG "Returning DAY_OF_WEEK_LINE\n\n"; return $DAY_OF_WEEK_LINE; } if ($thisline =~ $Search_String____Degree ) { print DEBUG "Returning DEGREE_LINE\n\n"; return $DEGREE_LINE; } if ($thisline =~ $Search_String____Percent ) { print DEBUG "Returning PERCENT_LINE\n\n"; return $PERCENT_LINE; } if ($thisline =~ $Search_String____Wind ) { print DEBUG "Returning WIND_LINE\n\n"; return $WIND_LINE; } if ($thisline =~ $Search_String____Comment) { print DEBUG "Returning COMMENTLINE\n\n"; return $COMMENTLINE; }; return $NULLLINE; };