All pastes #411260 Raw Edit

Anonymous

public php v1 · immutable
#411260 ·published 2007-03-26 22:13 UTC
rendered paste body
<?php//This file will take a get variable 'show', use it to get the tv show data from epguides.com, parse the data, and make an iCal file that can be added to your google calendar to show upcoming episodes.//Tested with: American Dad, Family Guy, Lost, Saturday Night Live, The Simpsons, The Sopranos, Mythbusters, South Park//I know this is messy (I haven't written PHP in a couple years), and there are some very inelegant parts, but it works.  Hopefully others can improve it//Start the iCal file.  Messing with this section seems to cause a lot of trouble.  I don't know what the real formatting restrictions areecho "BEGIN:VCALENDAR\r\n";echo "PRODID:-//google.com\r\n";echo "VERSION:2.0\r\n";echo "X-WR-CALDESC:".$show.": TV episode dates from epguides.com and thanks to Forbidden with the regex\r\n";echo "X-WR-CALNAME:".$show."\r\n"; //Set up epguides.com variables$show = $HTTP_GET_VARS['show'];$showurl = 'http://epguides.com/'.$show.'/';//Get epguides.com Titlepreg_match('#<title>.+\(#iU', file_get_contents($showurl), $title);if ($show == 'sopranos') { $show= "The Sopranos"; }  //Hack to fix Sopranos title grabelse {   $show =  trim(substr($title[0], 7, -2),  " :\n\t\r!");}//Parse and split up epguides content$subject = file_get_contents($showurl);$pattern = '#([0-9]{1,3}\.)(\s+)([0-9]{1,2}\-[\s]?[0-9 ]{1,2})(\s+)([0-9a-zA-Z\s-]+)(\s+)([0-9]{1,2}[\s]?[a-zA-Z]{3}[\s]?[0-9]{2})(\s+)(<a target="(_blank|visit)" href="http://www.tv.com[\D\d\s]+</a>|[0-9a-zA-Z\s-]+)#iU';preg_match_all($pattern, $subject, $matches);   $numbers = $matches[1];   $episodes = $matches[3];   $dates = $matches[7];   $urls = $matches[9];   $arrayShows = array();      for( $x = 0; $x < count($numbers); $x++ ){         $number = array('number' => $numbers[$x], 'episode' => $episodes[$x], 'date' => $dates[$x], 'url' => $urls[$x]);         array_push($arrayShows, $number);      }//Begin event iterationforeach($arrayShows as $value) {   //Format Date      $episodesequencenumber = trim($value['number'], " .");      $episodeseason = trim(substr($value['episode'], 0,2), " -");      $episodenumber = trim(substr($value['episode'],-2, 2), " ");         if ($episodenumber <= "9"){ $episodenumber = "0".$episodenumber; }         if ($episodeseason <= "9") { $episodeseason = "0".$episodeseason; }      $episodedateday = trim(substr($value['date'],0,2));      if ($episodedateday <= "9") { $episodedateday = "0".$episodedateday; }      $episodedatemonth = substr($value['date'],-6,3);      switch ($episodedatemonth) {         case "Jan":            $episodedatemonth = "01";            break;         case "Feb":            $episodedatemonth = "02";            break;         case "Mar":            $episodedatemonth = "03";            break;         case "Apr":            $episodedatemonth = "04";            break;         case "May":            $episodedatemonth = "05";            break;         case "Jun":            $episodedatemonth = "06";            break;         case "Jul":            $episodedatemonth = "07";            break;         case "Aug":            $episodedatemonth = "08";            break;         case "Sep":            $episodedatemonth = "09";            break;         case "Oct":            $episodedatemonth = "10";            break;         case "Nov":            $episodedatemonth =  "11";            break;         case "Dec":            $episodedatemonth = "12";            break;      }   //Convert two digit year to four digits: any year above the two digit value for this year is presumed to be 19--      $episodedateyear = substr($value['date'],-2,2);      $date = getdate();      $year = substr($date['year'], 2,2);         if (substr($year, 0,1) == "0") { $thisyear = substr($year, 1, 1);} else { $thisyear = $year; }         if (substr($episodedateyear, 0,1) == "0") { $episodeyear = substr($episodeyear, 1, 1);} else { $episodeyear = $episodedateyear; }         if($episodeyear <= $thisyear){ $episodedateyear = "20".substr($value['date'], -2,2); }         elseif($episodeyear > $thisyear){ $episodedateyear = "19".substr($value['date'], -2,2); }//Echo out the event   $value['url'] = str_replace(" &amp", "&", $value['url']);   if ( ctype_digit($episodedateyear) && ctype_digit($episodedatemonth) && ctype_digit($episodedateday)){      echo "BEGIN:VEVENT\r\n";      echo "DESCRIPTION:".$show." - ".$episodeseason."x".$episodenumber.": ". $value['url']."\r\n";      echo "DTEND:".$episodedateyear.$episodedatemonth.$episodedateday."\r\n";      echo "DTSTAMP:".$episodedateyear.$episodedatemonth.$episodedateday."T000000Z\r\n";      echo "DTSTART:".$episodedateyear.$episodedatemonth.$episodedateday."\r\n";      echo "SUMMARY:".$show." - ".$episodeseason."x".$episodenumber." - ".$value['url']."\r\n";      echo "UID:".str_replace(" ", "_", $show)."_".$episodeseason."x".$episodenumber."@epguides.com\r\n";      echo "END:VEVENT\r\n";   }}echo "END:VCALENDAR";?>