use zUtil;

sub boxOfficeMojo
{
   my $html = shift;
   my @rValue = ();

   # Isolate the single data table on the page
   my ($table) = $html =~ /<table[^>]*>(.*?)<\/table>/si;
   return @rValue unless $table;

   my $count = 0;
   while( $table =~ s/<tr[^>]*>(.*?)<\/tr>//si )
   {
      my $row = $1;
      next if $row =~ /<th\b/i;          # skip header

      my @cells;
      while( $row =~ s/<td[^>]*>(.*?)<\/td>//si )
      {
         my $cell = $1;
         $cell =~ s/<[^>]+>//gs;          # strip nested tags
         $cell =~ s/^\s+|\s+$//g;
         push @cells, $cell;
      }

      # Columns: rank, lw_rank, title, weekend_gross, %change, theaters,
      # theater_change, avg_per_theater, overall_gross, week, studio, ...
      # The "gross" we expose is cumulative (cell 8), matching what the
      # old tribute.ca handler showed -- weekend-only (cell 3) is too
      # narrow a number for a "Top Movies" surface where users expect
      # to see total earnings.
      next unless @cells >= 9;
      next unless $cells[0] =~ /^\d+$/;   # data rows have a numeric rank

      push @rValue, {
         'thisweek' => $cells[0],
         'lastweek' => $cells[1],
         'name'     => $cells[2],
         'gross'    => $cells[8],
      };

      last if ++$count >= 10;
   }

   return @rValue;
}

1;
