#!/usr/bin/perl # Author: Peter R. Wood, http://prwdot.org/ use strict; use LWP::Simple; my $sku = $ARGV[0]; my $base_url = "http://www.macmall.com/macmall/shop/detail.asp?dpno="; my $full_url = "$base_url$sku"; my $current_stock = 0; my $current_price = ""; my $working_file = "$sku.html"; my $data_file = "$sku.dat"; my $output_file = "$sku.html"; ## Fetch the current data my $content = getstore( "$full_url", "$working_file" ); open( FILE, "$sku.html" ); my @lines = ; close(FILE); chomp(@lines); my $num_lines = scalar(@lines); my $curr_line = 0; while ( $curr_line <= $num_lines ) { if ( $lines[$curr_line] =~ /in\s+stock/oi ) { $curr_line++; if ( $lines[$curr_line] =~ />(\d+)([0-9\.\$,]+)>$data_file" ); print DAT $line; close(DAT); ## Build the data structure my %stock_data; open( DAT, "$data_file" ); while () { chomp; my ( $this_time, $this_stock, $this_price ) = split(/:/); $stock_data{$this_time}{'stock'} = $this_stock; $stock_data{$this_time}{'price'} = $this_price; } close(DAT); my @timeindex = sort( keys(%stock_data) ); ## Rebuild the HTML File my $rows = ""; my $prev_price; my $prev_stock; foreach my $time (@timeindex) { if ( ( $stock_data{$time}{'stock'} != $prev_stock ) || ( $stock_data{$time}{'price'} != $prev_price ) ) { $prev_stock = $stock_data{$time}{'stock'}; $prev_price = $stock_data{$time}{'price'}; $rows .= "" . scalar( localtime($time) ) . "" . "" . $stock_data{$time}{'stock'} . "" . "" . $stock_data{$time}{'price'} . "\n"; } } $rows = "Time/DateUnits in StockPrice\n" . $rows; my $table = "\n" . $rows . "
\n"; my $update = scalar( localtime( time() ) ); my $output = "Stock Status\n" . "

Stock Status For $sku

\n" . $table . "

Last Update: $update

\n" . ""; open( OUTPUT, ">$output_file" ); print OUTPUT $output; close(OUTPUT);