#!/usr/bin/perl -w # #Perl script for AFL Footie Tipping # #Author: jim@jimohalloran.com # #Configurable Items $TEAM_NAME_REGEXP = "[A-Za-z\/. ]+"; #RegEx's to parse the tips page. $TIP_URL = "http://afl.com.au/default.asp?pg=experttipsters"; $TIP_REGEXP = "\($TEAM_NAME_REGEXP)\<\/td\>"; $TIP_MARGIN_REGEXP = "\([0-9]+)\<\/td\>"; # Upper and lower bounds to accepting a game as validly tipped. $MIN_GAME_TIPS = 18; $MAX_GAME_TIPS = 20; $MIN_VALID_GAMES = 6; #RegEx's to parse the fixture page. $FIXTURE_URL = "http://afl.com.au/default.asp?pg=fixture&spg=round"; ##Kangaroos vs Adelaide  $FIXTURE_REGEXP = "\\($TEAM_NAME_REGEXP)\ \;vs ($TEAM_NAME_REGEXP)\<\/a\>"; $FIXTURE_DATE_REGEXP = "\([0-9A-Za-z ]+)\<\/span\>"; $NumValid = 0; sub TranslateTeams { my $TeamIn; $TeamIn = $_[0]; $TeamIn =~ s/\s+$//; $TeamIn =~ s/^\s+//; if ($TeamIn eq "Brisbane Lions") { $TeamIn = "Brisbane"; } elsif ($TeamIn eq "Por Adelaide") { $TeamIn = "Port Adelaide"; } elsif ($TeamIn eq "Sydney Swans") { $TeamIn = "Sydney"; } elsif ($TeamIn eq "Western Bulldogs") { $TeamIn = "Bulldogs"; } elsif ($TeamIn eq "St.Kilda") { $TeamIn = "St Kilda"; } elsif ($TeamIn eq "St. Kilda") { $TeamIn = "St Kilda"; } elsif ($TeamIn eq "Saints") { $TeamIn = "St Kilda"; } elsif ($TeamIn eq "StKilda") { $TeamIn = "St Kilda"; } elsif ($TeamIn eq "West Coast Eagles") { $TeamIn = "West Coast"; } elsif ($TeamIn eq "Colingwood") { $TeamIn = "Collingwood"; }; return $TeamIn; }; #Download tips page and parse into teams. $TipPage =`wget "$TIP_URL" -q -O -`; @HTMLLines = split(/\n/, $TipPage); foreach $ThisOne (@HTMLLines) { if ( ($Team) = ($ThisOne =~ /$TIP_REGEXP/) ) { $Tipped{TranslateTeams($Team)}++; $LastTeam=TranslateTeams($Team); } elsif ( ($Margin) = ($ThisOne =~ /$TIP_MARGIN_REGEXP/) ) { if (!defined($Margins{$LastTeam})) { $Margins{$LastTeam} = $Margin; } else { $Margins{$LastTeam} = (($Margins{$LastTeam} * ($Tipped{$LastTeam} - 1)) + $Margin) / $Tipped{$LastTeam}; }; }; }; #Download tips page and parse into the games. $FixturePage =`wget "$FIXTURE_URL" -q -O -`; @HTMLLines = split(/\n/, $FixturePage); foreach $ThisOne (@HTMLLines) { if ( ($Home, $Away) = ($ThisOne =~ /$FIXTURE_REGEXP/) ) { $Home = TranslateTeams($Home); $Away = TranslateTeams($Away); if (!defined($Tipped{$Home})) { $Tipped{$Home}=0; $Margins{$Home}=0; }; if (!defined($Tipped{$Away})) { $Tipped{$Away}=0; $Margins{$Away}=0; }; if (($Home eq "Adelaide") or ($Home eq "Port Adelaide")) { $FootballPark = 1; } else { $FootballPark = 0; }; print "$Home ($Tipped{$Home}) vs $Away ($Tipped{$Away}) "; if ($Tipped{$Home} >= $Tipped{$Away}) { print " ... $Home tipped.\n"; $Tips .= $Home ."\n"; if ($FootballPark) { $FPPoints = $Margins{$Home}; $FPTeam = $Home; }; } else { print " ... $Away tipped.\n"; $Tips .= $Away ."\n"; if ($FootballPark) { $FPPoints = $Margins{$Away}; $FPTeam = $Away; }; }; $NumTips = $Tipped{$Home} + $Tipped{$Away}; if (($NumTips >= $MIN_GAME_TIPS) && ($NumTips <= $MAX_GAME_TIPS)) { $NumValid++; }; } elsif ( ($ThisFixtureDate) = ($ThisOne =~ /$FIXTURE_DATE_REGEXP/) ) { if (! defined($FixtureDate)) { $FixtureDate = $ThisFixtureDate; print "Fixture for round starting $FixtureDate\n\n"; }; }; }; if ($NumValid >= $MIN_VALID_GAMES) { print "\n\nSummary of tips...\n$Tips\n"; printf("$FPTeam to win by %.1f pts.\n\n", $FPPoints); print "Teams tipped for this round were....\n"; foreach $ThisOne (sort {$Tipped{$b}<=>$Tipped{$a} } keys %Tipped) { printf("$ThisOne was picked %1d time(s) to win by %.1f pts\n", $Tipped{$ThisOne}, $Margins{$ThisOne}); }; } else { print "\n\nOnly $NumValid valid games were found, tip summary not displayed.\n\n"; };