#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use strict; use CGI qw(:standard); use Text::ParseWords; my $cgiquery=new CGI; use LWP::Simple qw(!head); my $FTSE100; my $FTSEoffset; my $url = 'http://www.moneyworld.co.uk/stocks/ftse100/'; my $myfile="ftse.txt"; my $line; my @line; my $getLWP = 0; my $LWPdelay = 15; my $midnightMinutes; my $midnightMinutesFile; #Open file for reading, place a shared lcok on it and read all vaues for timestamp and ftse. open(MYFILE, "$myfile") || die; flock(MYFILE, 1); $line = ; @line = split(/ /, $line); $FTSE100 = @line[0]; my $fileYear = @line[1]; my $fileMon = @line[2]; my $fileMday = @line[3]; my $fileHour = @line[4]; my $fileMin = @line[5]; close(MYFILE); #Get server time (my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localtime(time); $year += 1900; $mon += 1; #Decide wether to download page using LWP $midnightMinutesFile = $fileHour * 60 + $fileMin; $midnightMinutes = $hour * 60 + $min; if ($midnightMinutes > 5) { if ($fileYear != $year) { $getLWP = 1; } else { if ($fileMon != $mon) { $getLWP = 1; } else { if ($fileMday !=$mday) { $getLWP = 1; } else { $midnightMinutesFile = $midnightMinutesFile +$LWPdelay; if ($midnightMinutesFile < $midnightMinutes) { $getLWP = 1; } } } } } else { $getLWP = 1; } #if $LWPdelay minutes have passed then update the file using LWP and an exclusive file lock #if ($getLWP ==1) { if (2==1) { my $content = get $url; die "Couldn't get $url" unless defined $content; while ($content =~ /Market Indicators/ig) { $FTSEoffset = pos($content); } $FTSE100=substr ($content, $FTSEoffset, 300); my @FTSEParts = split(/ +/,$FTSE100); my $FTSEPartsCounter = 0; my $FoundFTSEPart = 0; while ($FTSEPartsCounter <= @FTSEParts && $FoundFTSEPart == 0) { if (@FTSEParts[$FTSEPartsCounter] =~ /[0-9][0-9][0-9][0-9]\.[0-9][0-9]/ig){ my $temp; my $found = 0; my $counter = 0; while ( $found == 0 ) { $temp= substr (@FTSEParts[$FTSEPartsCounter], $counter, 7); $found = 1 if ($temp =~ /[0-9][0-9][0-9][0-9]\.[0-9][0-9]/ig) || ($counter > 5000); $counter +=1; } $FTSE100 = $temp; $FoundFTSEPart = 1; if ($counter > 5000) { $FTSE100 = "Unavailable"; } } else{ $FTSE100 = "Unavailable"; } $FTSEPartsCounter += 1; } open(MYFILE, ">$myfile") || die; flock(MYFILE, 2); print MYFILE ($FTSE100 . " " . $year . " " . $mon . " " . $mday . " " . $hour. " " . $min); close(MYFILE); } # print MYFILE ("\nXXX 1 XXX " . @FTSEParts[1]); # print MYFILE ("XXX 2 XXX " . @FTSEParts[2]); # print MYFILE ("XXX 3 XXX " . @FTSEParts[3]); # print MYFILE ("XXX 4 XXX " . @FTSEParts[4]); # print MYFILE ("XXX 5 XXX " . @FTSEParts[5]); # print MYFILE ("XXX 6 XXX " . @FTSEParts[6]); # print MYFILE ("XXX 7 XXX " . @FTSEParts[7]); # print MYFILE ("XXX 8 XXX " . @FTSEParts[8]); # print MYFILE ("XXX 9 XXX " . @FTSEParts[9]); # print MYFILE ("XXX 10 XXX " . @FTSEParts[10]); # print MYFILE ("XXX 11 XXX " . @FTSEParts[11]); print $cgiquery->header(); print < Aveton Gifford Associates - Independent Financial Adviser - Edinburgh, Glasgow, Central Belt
EOF