Created
March 6, 2014 22:58
-
-
Save hsiboy/9401585 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Time::HiRes qw(time); | |
use LWP::UserAgent; | |
$ua = new LWP::UserAgent; #create a new instance | |
$ua->proxy(http => 'http://194.73.75.22:80'); # set the proxy server | |
$ua->agent("Search-Report/0.1 " . $ua->agent); | |
print "The user agent is: " . $ua->agent . "\n\n"; | |
# REQUEST RADIUS FOR SEARCH, DEFAULT TO NATIONAL | |
print "Enter distance to search, in miles (or press ENTER for national).\n"; | |
chomp ($input = <STDIN>); | |
if($input > 200) | |
{ | |
$distance = 1500; | |
} | |
elsif($input > 100) | |
{ | |
$distance = 200; | |
} | |
elsif($input > 60) | |
{ | |
$distance = 100; | |
} | |
elsif($input > 40) | |
{ | |
$distance = 60; | |
} | |
elsif($input > 30) | |
{ | |
$distance = 40; | |
} | |
elsif($input > 20) | |
{ | |
$distance = 30; | |
} | |
elsif($input > 10) | |
{ | |
$distance = 20; | |
} | |
elsif($input > 1) | |
{ | |
$distance = 10; | |
} | |
elsif($input == 1) | |
{ | |
$distance = 1; | |
} | |
else | |
{ | |
$distance = 1500; | |
} | |
# REQUEST price FOR SEARCH, DEFAULT TO any | |
print "Enter a min price £ (or press ENTER for ANY).\n"; | |
chomp ($input = <STDIN>); | |
if($input > 50000) | |
{ | |
$minPrice = 60000; | |
} | |
if($input > 45000) | |
{ | |
$minPrice = 50000; | |
} | |
if($input > 35000) | |
{ | |
$minPrice = 40000; | |
} | |
if($input > 30000) | |
{ | |
$minPrice = 35000; | |
} | |
if($input > 25000) | |
{ | |
$minPrice = 30000; | |
} | |
elsif($input > 20000) | |
{ | |
$minPrice = 25000; | |
} | |
elsif($input > 15000) | |
{ | |
$minPrice = 20000; | |
} | |
elsif($input > 12500) | |
{ | |
$minPrice = "15000"; | |
} | |
elsif($input > 10000) | |
{ | |
$minPrice = "12500"; | |
} | |
elsif($input > 7500) | |
{ | |
$minPrice = "10000"; | |
} | |
elsif($input > 5000) | |
{ | |
$minPrice = "7500"; | |
} | |
elsif($input > 1000) | |
{ | |
$minPrice = "5000"; | |
} | |
elsif($input == 1) | |
{ | |
$minPrice = "5000"; | |
} | |
else | |
{ | |
$minPrice = "-1"; | |
} | |
# REQUEST price FOR SEARCH, DEFAULT TO any | |
print "Enter a max price £ (or press ENTER for ANY).\n"; | |
chomp ($input = <STDIN>); | |
if($input > 60000) | |
{ | |
$maxPrice = -2; | |
} | |
if($input > 50000) | |
{ | |
$maxPrice = 60000; | |
} | |
if($input > 45000) | |
{ | |
$maxPrice = 50000; | |
} | |
if($input > 35000) | |
{ | |
$maxPrice = 40000; | |
} | |
if($input > 30000) | |
{ | |
$maxPrice = 35000; | |
} | |
if($input > 25000) | |
{ | |
$maxPrice = 30000; | |
} | |
elsif($input > 20000) | |
{ | |
$maxPrice = 25000; | |
} | |
elsif($input > 15000) | |
{ | |
$maxPrice = 20000; | |
} | |
elsif($input > 12500) | |
{ | |
$maxPrice = 15000; | |
} | |
elsif($input > 10000) | |
{ | |
$maxPrice = 12500; | |
} | |
elsif($input > 7500) | |
{ | |
$maxPrice = 10000; | |
} | |
elsif($input > 5000) | |
{ | |
$maxPrice = 7500; | |
} | |
elsif($input > 1000) | |
{ | |
$maxPrice = 5000; | |
} | |
elsif($input == 1) | |
{ | |
$maxPrice = 5000; | |
} | |
else | |
{ | |
$maxPrice = -1; | |
} | |
# SET A DEFAULT POSTCODE, FOR LOCAL SEARCHES | |
$pcode = "WA120HE"; | |
# NO NEED TO REQUEST POSTCODE IF NATIONAL SEARCH | |
unless($distance == 1500) | |
{ | |
print "Enter a valid postcode\n"; | |
chomp ($input = <STDIN>); | |
$pcode = $input; | |
print "The postcode was $pcode\n"; | |
$count = $input =~ s/\w/$1/gi; | |
if ($count < 5) | |
{ | |
$pcode = "WA120HE"; | |
} | |
} | |
&fill_arrays; | |
# OPEN FILEHANDLES TO WRITE TO | |
open (CSV_OUT, ">search-report.csv") || die ("can't open search-report.csv: $! \n"); | |
open (HTML_OUT, ">search-report.html") || die ("can't open search-report.html: $! \n"); | |
# TOP OF REPORT FORM | |
&report_header; | |
#$genesis = times; | |
$t0 = $t2 = time; | |
#print "\nInitially, time is $t0.\n"; | |
# ITERATE THROUGH COMPLETE LISTING OF MAKES AND MODELS | |
for $i(0..$#model_array) | |
{ | |
print "\nProcessing make $i of $#model_array.\n"; | |
for $j(0..$#{$model_array[$i]}) | |
{ | |
$make = @make_array[$i]; | |
$model = $model_array[$i][$j]; | |
&search_cars; | |
$t1 = time; | |
$t3 = $t1 - $t0; | |
$elapsed_time = sprintf("%.2f", $t3); | |
print " - Elapsed time: $elapsed_time seconds - \n\n"; | |
$t4 = $t1 - $t2; | |
$t2 = $t1; | |
$search_time = sprintf("%.2f", $t4); | |
&print_row; | |
$model_count++; | |
} | |
} | |
$pre_av_time = ($elapsed_time / $model_count); | |
$av_time = sprintf("%.2f", $pre_av_time); | |
$total_time = sprintf("%.2f", $elapsed_time); | |
#BOTTOM OF REPORT FORM | |
&report_footer; | |
print "\n=========================================\n"; | |
print "\n There were $model_count searches\n"; | |
print " The total search took $total_time seconds\n"; | |
print "\n=========================================\n"; | |
close CSV_OUT; | |
close HTML_OUT; | |
####### SUB-ROUTINES ########### | |
sub fill_arrays | |
{ | |
#my $req = new HTTP::Request('GET', "http://www.autotrader.co.uk/CARS/common/_make_model.js"); | |
my $req = new HTTP::Request('GET', "http://ncs.autotrader.co.uk/ncs/MakesModels.js"); | |
my $res = $ua->request($req); | |
if($res->is_error) | |
{ | |
print $res->code . "\nJS page request: " . $res->message . "\n"; | |
} | |
else | |
{ | |
print "JS page request: " . $res->message . "\n"; | |
} | |
# LOAD RESULTANT PAGE INTO SCALAR VARIABLE | |
$jspage = $ua->request($req)->as_string; | |
# REMOVE NEWLINES | |
@page_array = split(/\n+/, $jspage); | |
foreach(@page_array) | |
{ | |
# SELECT MODEL LINES | |
if(/^_model/) | |
{ | |
#REMOVE APOSTROPHES AND LOAD EACH MODEL INTO ARRAY | |
($line) = /(\'.*\')/; | |
$line =~ s/\'//g; | |
@temp = split(/,/, $line); | |
# LOAD ARRAY OF MODELS FOR PARTICULAR MAKE INTO OUTER ARRAY | |
push @model_array,[@temp]; | |
print "."; | |
} | |
# SELECT MAKE LINE | |
if(/^make_v/) | |
{ | |
# REMOVE APOSTROPHES AND LOAD INTO ARRAY | |
($line) = /(\'.*\')/; | |
$line =~ s/\'//g; | |
@make_array = split(/,/, $line); | |
print "!"; | |
} | |
} | |
#REMOVE FIRST UNNECESSARY ELEMENT TO PREVENT UNUSUAL BEHAVIOR | |
shift(@model_array); | |
} | |
sub search_cars | |
{ | |
# SEARCH DATABASE FOR SPECIFIC MAKE-MODEL | |
#my $req = new HTTP::Request('GET', "http://atsearch.autotrader.co.uk/WWW/cars_search.asp?make=$make&model=$model&postcode=$pcode&miles&$distance&source=0&max_records=200"); | |
my $req = new HTTP::Request('GET', "http://ncs.autotrader.co.uk/ncs/NewCarSearchController?make=$make&minPrice=$minPrice&postcode=$pcode&model=$model&maxPrice=$maxPrice&distance=$distance"); | |
my $res = $ua->request($req); | |
if($res->is_error) | |
{ | |
print $res->code . "\n" . $make . "\t" . $model . " search request: " . $res->message . "\n"; | |
#print scalar(times - $genesis) . "\n"; | |
} | |
else | |
{ | |
print $make . "\t" . $model . " search request: " . $res->message . "\n"; | |
#print scalar(times - $genesis) . "\n"; | |
} | |
# LOAD RESULTANT PAGE INTO SCALAR VARAIABLE | |
$page = $ua->request($req)->as_string; | |
# FIND NUMBER OF RESULTS RETURNED FROM SEARCH | |
if($page =~ /Displaying 1 - \d+\s+of\s+(\d+)\s+cars\s+found/) | |
{ | |
$results = $1; | |
} | |
elsif($page =~ /Displaying 1 - (\d+)\s+cars\s+found/) | |
{ | |
$results = $1; | |
} | |
else | |
{ | |
$results = 0; | |
} | |
} | |
sub report_header | |
{ | |
#PRINT HTML HEADER TAGS AND DATE-TIME TABLE AT HEAD OF REPORT | |
print HTML_OUT "<html><head><title>Search Report</title></head>"; | |
print HTML_OUT "<body bgcolor=\"ffffff\"><h1><center><img src = \"at_logo.gif\" align = center></center></h1><font face=\"verdana, tahoma, arial, sans-serif\" size=\"2\">"; | |
# DEFINE COLOURS TO BE USED IN REPORT | |
$at_red = "fefefe"; | |
$at_blue = "cecece"; | |
$at_indigo = "aeaeae"; | |
$date = scalar(localtime); | |
# THE TOP TABLE SHOWING DATE & TIME | |
print HTML_OUT "<table cellpadding = 5 align=center width=\"50%\">"; | |
print HTML_OUT "<tr bgcolor=\"$at_red\">"; | |
print HTML_OUT "<td align=center><font size=5><b>New Car Search Report</b></font></td></tr>"; | |
print HTML_OUT "<tr bgcolor=\"$at_blue\">"; | |
print HTML_OUT "<td align=center><font size=4>$date</font></td></tr><table><br><br>"; | |
# THE RESULTS TABLE | |
print HTML_OUT "<table align=center width=\"80%\" border=0 cellpadding=4>"; | |
print HTML_OUT "<tr bgcolor=\"$at_red\">"; | |
print HTML_OUT "<th>MAKE</th><th>MODEL</th><th>RESULTS</th><th>TIME</th></tr>"; | |
} | |
sub print_row | |
{ | |
if($model eq ANY) | |
{ | |
# DISPLAY ROW AS INDIGO FOR START OF EACH NEW MAKE | |
$row_colour = $at_indigo; | |
} | |
else | |
{ | |
$row_colour = $at_blue; | |
} | |
#PRINT EACH ROW OF REPORT WITH MAKE, MODEL, NUM OF RESULTS, TIME TAKEN AND LINK TO SEARCH PAGE | |
print HTML_OUT "<a href=\"http://ncs.autotrader.co.uk/ncs/NewCarSearchController?make=$make&minPrice=$minPrice&postcode=$pcode&model=$model&maxPrice=$maxPrice&distance=$distance\" title =\"See the search results!\"><tr bgcolor = $row_colour>"; | |
print HTML_OUT "<td nowrap>$make</td>"; | |
print HTML_OUT "<td nowrap>$model</td>"; | |
print HTML_OUT "<td nowrap>$results</td>"; | |
print HTML_OUT "<td nowrap>$search_time</td></tr></a>"; | |
#PRINT TO EXCEL FILE | |
print CSV_OUT " $make, $model, $results, $search_time\n"; | |
} | |
sub report_footer | |
{ | |
# PRINT SUMMARY TABLE AT FOOT OF REPORT | |
print HTML_OUT "</table><br><br>"; | |
print HTML_OUT "<table cellpadding=\"5\" align=\"center\" width=\"50%\">"; | |
print HTML_OUT "<tr bgcolor=\"$at_red\">"; | |
print HTML_OUT "<td align=\"center\" colspan=\"2\"><font size=\"5\"><b>Summary</b></font></td></tr>"; | |
print HTML_OUT "<tr bgcolor=\"$at_blue\"><td>Number of searches: </td><td>$model_count</td></tr>"; | |
print HTML_OUT "<tr bgcolor=\"$at_blue\"><td>Total search time: </td><td>$total_time seconds</td></tr>"; | |
print HTML_OUT "<tr bgcolor=\"$at_blue\"><td>Average time per search: </td><td>$av_time seconds</td></tr></table>"; | |
print HTML_OUT "</body></html>"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment