<pre>
<?php

// pearljam.com setlist harvester (nothing but love!)
// written by daniel green // march 2006
// http://antimac.org/setlistimator

// this script will harvest all setlist data from pearljam.com
// and output an easy-to-swallow ini file, for use with setlistimator
// i wonder if it will be much more accurate, given the time period and such

// config - some urls
$dates_url "http://pearljam.com/PJFeed/showsFeed.js";
$show_url "http://pearljam.com/PJFeed/setlistFeed.php?date=";
$ini_file "";//"./setlistimator.ini";
$max_execution_time 60 5// five minutes
ini_set("max_execution_time"$max_execution_time);

// get rid of all that nasty document.write() crapola
function strip_js($str)
{
  
$to_remove = Array("document.write(', ');" => "\n""document.write(\", \");" => "\n""document.write('" => """document.write(\"" => """');" => "\n""\");" => "\n");
  
  foreach (
$to_remove as $k => $v)
    
$str str_replace($k$v$str);
  
  return(
$str);
}

// remove the html tags
function strip_html($str)
{
  
$str strip_tags($str);
  
$str str_replace("&nbsp;"" "$str);
  
  return(
$str);
}

// should return a string containing text only (no markup or js)
function normalize_data($str)
{
  
$str strip_js($str);
  
$str strip_html($str);
  
$str stripslashes($str);
  
  return(
$str);
}

// expecting YYYY-MM-DD
function format_date_for_url($date)
{
  
$date str_replace("-"""$date);
  
$date .= "0000";
  
  return(
$date);
}

// increments $pos until $arr[$pos] isn't considered whitespace
function skip_whitespace(&$arr, &$pos)
{
  
$size sizeof($arr);
  
$include = Array("/""//"",""");
  
  while (
$pos $size && in_array(trim($arr[$pos]), $include))
    
$pos++;
}

// snag setlist listing data from the site
function get_show_list($url)
{
  
$dates_data implode(""file($url));
  
$dates_data normalize_data($dates_data);
  
$dd_arr explode("\n"$dates_data);
  
$fields = Array();
  
$dd_size sizeof($dd_arr);
  
$pos 0;
  
  
skip_whitespace(&$dd_arr, &$pos);
  
  
// get field names
  
while ($pos $dd_size && trim($dd_arr[$pos]) != "")
    
$fields[] = trim($dd_arr[$pos++]);
  
  
$num_fields sizeof($fields);
  
$shows = Array();
  
  
// read in each show's data
  
while ($pos $dd_size)
  {
    
skip_whitespace(&$dd_arr, &$pos);
    
    
$tmp = Array();
    
    for (
$i $pos$i $pos $num_fields$i++)
      
$tmp[$fields[$i $pos]] = trim($dd_arr[$i]);
      
    
$shows[] = $tmp;
    
$pos += $num_fields;
  }
  
  return(
$shows);
}

// get the setlist data for one show
function get_song_list($url)
{
  
$show_data implode(""file($url));
  
$show_data normalize_data($show_data);
  
$show_arr explode("\n"$show_data);
  
  return(
$show_arr);
}

// makes a big ole array of setlist data
function get_setlists($dates_url$show_url)
{
  
$shows get_show_list($dates_url);
  
$num_shows sizeof($shows);
  
  if (!
$num_shows)
    die(
"no shows collected, exiting...");
  
  
$setlists = Array();
  
  foreach (
$shows as $show)
  {
    
$songs get_song_list($show_url format_date_for_url($show["DATE"]));
    
    if (!
$songs)
      continue;
    
    
$fields array_keys($show);
    
$cur_show = Array("show" => $show"setlist" => Array());
    
$songs_size sizeof($songs);
    
$pos 0;
    
    while (
$pos $songs_size)
    {
      
skip_whitespace(&$songs, &$pos);
      
$curtext trim($songs[$pos]);
      
      
// skip:
      //   empty strings
      //   strings less than two characters in length
      //   strings that match our field names (usually column headers)
      //   strings that match field data (data matching the column headers)
      //   text describing a group of songs, like "Set N", "Encore N", etc
      
if (!$curtext || strlen($curtext) < || in_array($curtext$fields) || in_array($curtext$show) || !(strpos($curtext"Set ") === false) || !(strpos($curtext"Encore ") === false) || !(strpos($curtext"Acoustic ") === false))
      {
        
$pos++;
        continue;
      }
      
      
// skip current and next lines if we find "Description:"
      
if (strpos($curtext"Description:") !== false)
      {
        
$pos += 2;
        continue;
      }
      
      
$cur_show["setlist"][] = $curtext;
      
$pos++;
    }
    
    if (
$cur_show["setlist"])
      
$setlists[] = $cur_show;
  }
  
  return(
$setlists);
}

$setlists get_setlists($dates_url$show_url);

$ini_header "[setlistimator]\r\n";
$ini_header .= "number_of_shows=" sizeof($setlists) . "\r\n";
$ini_shows "";

$i 1;

foreach (
$setlists as $setlist)
{
  
$show_title $setlist["show"]["DATE"] . " @ " $setlist["show"]["VENUE"];
  
$ini_header .= "show_" $i++ . "=$show_title\r\n";
  
$ini_shows .= "[$show_title]\r\n";
  
$ini_shows .= "number_of_songs=" sizeof($setlist["setlist"]) . "\r\n";
  
  
$j 1;
  
  foreach (
$setlist["setlist"] as $song)
    
$ini_shows .= "song_" $j++ . "=$song\r\n";
  
  
$ini_shows .= "\r\n";
}

$ini $ini_header "\r\n" $ini_shows;

if (
$ini_file)
{
  
$fp fopen($ini_file"w");
  
  if (
$fp)
  {
    
fwrite($fp$ini);
    
fclose($fp);
  }
}
else
  print(
$ini);

?>
</pre>