Sunday, June 12, 2005

iTunes and PHP

I have a laptop and a desktop, and I use iTunes as my Music Service, but I don't have an iPod. I wanted to move a certain play list from my Desktop to my Laptop, through my network, but didn't want to have to go find all 280 songs. I noticed, though, that you could export a playlist to a text file, and the text file had the path to the file in it. Since I have a little experience in PHP, I wanted to see if I could quick hack together a system that would copy all the files in a given playlist file into a given directory. I tried to use the plain text file output for a while, until I discovered that the text file that was exported was UTF8 encoded (I think...), and I couldn't get php to read from the text file properly. My code is shown below...
<?php
   // doesn't work...
function decode_utf8($str){
       # erase null signs in string
         $str=eregi_replace("^.{10,13}q\?","",$str);
       # paterns
           $pat = "/=([0-9A-F]{2})/";
           $cha="'.chr(hexdec(";
       # to decode with eval and replace
         eval("\$str='".
                 preg_replace($pat,$cha."'$1')).'",$str).
                 "';");
       # return
           return $str;
       }

   $copyTo = "C:\\Documents and Settings\\Aron\\My Documents\\CopyToDir\\";
   $handle = fopen("AirPlayList.txt", "r");
   while ($buffer = fgets($handle,4096)) {

      //$buffer = decode_utf8($buffer);
      $buffer = html_entity_decode(htmlentities($buffer, ENT_COMPAT, 'UTF-8'));
      
      //$copyTo = iconv('ISO-8859-1', 'UTF-8', $copyTo);
      preg_match("/\t([^\t]*\\\)([^\t]*?)$/",$buffer, $matches);
      //echo "\n full path:".$matches[0];
      //echo "\n\n\npath:".$matches[1];
      //echo "\n\nfile:".$matches[2];
      
      //echo $matches[0];
      //echo $buffer;
      if (!copy($matches[1].$matches[2],$copyTo.$matches[2])) {
         echo "\n\nCopy Error";
         echo "\nFrom:".$matches[1].$matches[2];
         echo "\nTo:".$copyTo.$matches[2];
      }

   }
   fclose($handle);
?>

0 Comments:

Post a Comment

<< Home