use strict;
use LWP::Simple;
use LWP::UserAgent;
use CGI::Pretty qw(:all);
use Date::Manip; &Date_Init ("TZ=US/Mountain");
my %state_abbr = (
"ALABAMA" => 'AL',
"ALASKA" => 'AK',
"ARIZONA" => 'AZ',
"ARKANSAS" => 'AR',
"CALIFORNIA" => 'CA',
"COLORADO" => 'CO',
"CONNECTICUT" => 'CT',
"DELAWARE" => 'DE',
"D.C." => 'DC',
"FLORIDA" => 'FL',
"GEORGIA" => 'GA',
"HAWAII" => 'HI',
"IDAHO" => 'ID',
"ILLINOIS" => 'IL',
"INDIANA" => 'IN',
"IOWA" => 'IA',
"KANSAS" => 'KS',
"KENTUCKY" => 'KY',
"LOUISIANA" => 'LA',
"MAINE" => 'ME',
"MARYLAND" => 'MD',
"MASSACHUSETTS" => 'MA',
"MICHIGAN" => 'MI',
"MINNESOTA" => 'MN',
"MISSISSIPPI" => 'MS',
"MISSOURI" => 'MO',
"MONTANA" => 'MT',
"NEBRASKA" => 'NE',
"NEVADA" => 'NV',
"NEW HAMPSHIRE" => 'NH',
"NEW JERSEY" => 'NJ',
"NEW MEXICO" => 'NM',
"NEW YORK" => 'NY',
"NORTH CAROLINA" => 'NC',
"NORTH DAKOTA" => 'ND',
"OHIO" => 'OH',
"OKLAHOMA" => 'OK',
"OREGON" => 'OR',
"PENNSYLVANIA" => 'PA',
"PUERTO RICO" => 'PR',
"RHODE ISLAND" => 'RI',
"SOUTH CAROLINA" => 'SC',
"SOUTH DAKOTA" => 'SD',
"TENNESSEE" => 'TN',
"TEXAS" => 'TX',
"UTAH" => 'UT',
"VERMONT" => 'VT',
"VIRGIN ISLANDS" => 'VI',
"VIRGINIA" => 'VA',
"WASHINGTON" => 'WA',
"WEST VIRGINIA" => 'WV',
"WISCONSIN" => 'WI',
"WYOMING" => 'WY',
);
my $newsurl ="http://headlinenews.cnn.com/QUICKNEWS/virtual/swf.headline.txt";
my $weatherurl ="http://headlinenews.cnn.com/QUICKNEWS/virtual/swf.weather.txt";
my $interurl ="http://headlinenews.cnn.com/QUICKNEWS/virtual/swf.interactive.txt";
my $interactive_ticker;
my $news_ticker = retrieve_news();
my $weather_ticker = retrieve_weather();
if (param("showinteractive")!~/no/i){
$interactive_ticker= retrieve_interactive();
}
my @state_weather =split /&/, $weather_ticker;
my $citystate;
my $lowstate; my $lowtemp;
my $histate; my $hitemp;
my ($URL, @lines, @entry, $content);
my ($moddate);
my (%headlines);
write_header("CNN wrapup");
%headlines = log_news ($news_ticker);
write_news( $interactive_ticker, %headlines),"";
if (param("showweather")!~/no/i){
write_weather(@state_weather);
}
write_footer();
####################
#
# Prints out a nice HTML header.
#
####################
sub write_header {
my $title=shift;
print header();
print start_html(-Title => $title, -BGCOLOR=>"#000000",-TEXT=>"#00FF00", -LINK=>"#33FF00", -VLINK=>"00CC00", -ALINK=>"FFFFFF");
print start_form(); # start_multipart_form() if file upload
my $das_url =&self_url();
print "";
}
####################
#
# End of page stuff.
#
####################
sub write_footer {
my $newsdate=get_moddate($newsurl);$newsdate= scalar localtime $newsdate; $newsdate = UnixDate($newsdate, "%m/%d/%Y %H:%M");
my $weatherdate= get_moddate($weatherurl);$weatherdate= scalar localtime $weatherdate ;$weatherdate = UnixDate($weatherdate, "%m/%d/%Y %H:%M");
my $presenttime=ParseDate("now");
print "
Headlines last updated ", $newsdate,
"\n | Weather last updated ", $weatherdate,
"\n | This page last updated ", UnixDate($presenttime, "%m/%d/%Y %H:%M"),"
",
end_form(), end_html() }
####################
#
# wrapper for LWP->get() call.
#
####################
sub retrieve_weather {
return "&" . get ($weatherurl);
}
####################
#
# wrapper for LWP->get() call.
#
####################
sub retrieve_news {
return get ($newsurl);
}
####################
#
# wrapper for LWP->get() call.
#
####################
sub retrieve_interactive{
return get ($interurl);
}
####################
#
# prints out the interactive and state headlines in a table
#
####################
sub write_news{
my ($l_inter, %l_headlines) = @_;
my $statectr;
my $new_tagstart;
my $new_tagend;
#start the table.
print '';
print ' Breaking news
';
# split up the interactive headlines.
@lines = split /&intheadline\d*\=/, $l_inter;
# get last modified date of the $interurl file. format nicely.
$moddate = scalar localtime get_moddate ($interurl);
$moddate= UnixDate($moddate, "%m/%d/%Y %H:%M");
# split the headline information URL & headline text & put into table.
foreach (@lines) {
my $hl_found=0;
@entry = split /&inturl\d*\=/;
@entry[1] =~ s/^\s//;
@entry[1] =~ s/\s\s/ /g;
@entry[0] =~ s/interactivecount\=\d*//;
if (@entry[0]){
print "@entry[0]<\/TD><\/TR>\n";
}
}
# begin next part of table.
print ' Headlines from around the country
';
# print out the state HTML anchors in two lines.
# note there's no checking to see if one exists or not :(
foreach my $thisstate ( sort keys %state_abbr) {
$statectr ++;
print "", $state_abbr{$thisstate}, " ";
($statectr==26) && print "
";
}
# print an HTML anchor to the Weather.
print '
Weather ';
# sorting headlines on state, then timestamp, then headline text.
PRINTHEADLINE: foreach my $thisheadline (sort {($l_headlines{$a}{"State"} cmp $l_headlines{$b}{"State"})or
($l_headlines{$b}{"Timestamp"} cmp $l_headlines{$a}{"Timestamp"}) or
($l_headlines{$a}{"Headline"} cmp $l_headlines{$b}{"Headline"}) } keys %l_headlines) {
my $broken_headline ="";# $l_headlines{$thisheadline}{"Headline"};
if (UnixDate ($l_headlines{$thisheadline}{"Timestamp"}, "%m/%d/%Y") eq UnixDate (&ParseDate("Today"),"%m/%d/%Y")) {
$new_tagstart='' ;$new_tagend= '';
} else {
if (param ("newonly")=~/yes/i) {
next PRINTHEADLINE
}
$new_tagstart='' ;$new_tagend= '';
}
if (param("search")!~/^$/) {
my $searchin = param("search");
$searchin =~ s/(\?)/\\w/gi;
$searchin =~ s/(\*)/\.$1?/gi; #line noise? ha!
$searchin = "\\b". $searchin . "\\b";
if (defined(param("searchmethod"))) {
if (param("searchmethod")=~/showonly/ ) {
if ($l_headlines{$thisheadline}{"Headline"}!~/$searchin/i) {next PRINTHEADLINE}
}
}
$l_headlines{$thisheadline}{"Headline"}=~ s/($searchin)/$1<\/FONT>/i;
}
if (param("definitions")=~/yes/i){
map {$broken_headline .= "$_ " }split (/ /,$l_headlines{$thisheadline}{"Headline"});
} else {
$broken_headline=$l_headlines{$thisheadline}{"Headline"}
}
#
# Can you figure out why the anchor's printed out in the headline cell, rather than
# the state or at the beginning of the row?
#
print "",
"",$new_tagstart,$l_headlines{$thisheadline}{"State"},$new_tagend,"<\/TD>",
" ",$new_tagstart,$l_headlines{$thisheadline}{"Timestamp"},"<\/TD>",$new_tagend,
" "," ",$new_tagstart,$broken_headline,"<\/TD>",$new_tagend,
"<\/TR>\n";
}
# end table.
print "<\/TABLE>";
print &self_url();
}
####################
#
# put the weather into a table.
#
#
####################
sub write_weather {
my %cityinfo;
my $ctr=0; # used for columnating.
#
# split into HoH format
# {INDEX}{INFOTYPE}{INFODATA}
# e.g. {1}{CITY}{BIRMINGHAM, AL}
# {1}{TEMP}{-5}
# etc...
#
foreach my $temp (@_) {
$temp=~/([a-zA-z]*)(\d*)\=(.*)/;
$cityinfo{$2}{$1}=$3;
}
# print out the HTML Anchor.
print "";
# begin table.
print "