#!/usr/bin/perl #################################################################### # Lotto Grabber # # by John J Reiser # # perl@newrisedesigns.com # # Distributed and licensed under the same terms as Perl itself. # #################################################################### #################################################################### # If you use this software or find it useful, I'd love to hear it. # # Please email me with comments or questions you have. # # I would be interested in working on other state lotteries, so # # if you know of other states with web-accessible data, it would # # be my pleasure to collect and compile it. # #################################################################### use strict; use LWP::UserAgent; use HTTP::Request::Common; open(my $zf, "zips_clean.txt") or die; # I was going to add the ZIP data in this file flock($zf, 1); # but I intend to include the compiled data in my @zips = <$zf>; # a DBase file for easy integration into Arc. chomp(@zips); my %hash; foreach(@zips){ $hash{$_}++; } @zips = sort keys(%hash); close($zf); my $filename = 'lotto_by_zip_results_pick6.txt'; my $ua = LWP::UserAgent->new('agent' => "Zip Code Lottery Finder"); if(! -e $filename){ open(my $temp, "+>", $filename) or die(); flock($temp, 2); print $temp ""; close $temp; } open(my $fh, "+<", $filename) or die(); flock($fh, 2) or warn(); print $fh "NJ Lottery Results by Zipcode\n"; my ($failed, $total) = 0; my @fails; ZIPS: @fails = (''); pop(@fails); for(@zips){ $total++; print "Requesting for $_..."; # form # POST http://www.state.nj.us/cgi-bin/lottery/winnersearch.pl # searchgame = 6 # searchzip = [zips] # Submit = "Start the Search" my $resp = $ua->request(POST 'http://www.state.nj.us/cgi-bin/lottery/winnersearch.pl', [ 'searchgame' => '6', 'Submit' => "Start the Search", 'searchzip' => $_, ]) or next; if($resp->is_success){ print " Successful."; my $html = $resp->content; if($html =~ /in \(\d+) matches/){ print " $1 found.\n"; print $fh $_ . "\t" . $1 . "\n"; } else{ print " none found.\n"; print $fh $_ . "\t" . "0" . "\n"; } } else{ print " Request failed.\n"; $failed++; push(@fails, $_); next; } } if($failed > 0){ $failed = 0; @zips = @fails; goto ZIPS; } close($fh); print "\nSummary\n"; print "$failed Failed Requests\n"; print "$total Total Requests\n"; # Your search for Pick 6 in 08028 resulted in N matches. my $wait = ;