Emusic Download Script for PHP and Linux
|
|
| diversen |
| 27-Apr-2010 09:33:13 |
#!/usr/bin/php <?php /** * Small script for downloading music from emusic.com * I have had troubles before using the client. So * I made this small script which does the same as a * java client on 8MB. * * Script will creates folders for artists and folders * for albums inside artist dir. * * REQUIREMENTS: * * You need a version of PHP5-cli and wget * * INSTALL: * * Place the script somewhere, e.g. your bin dir * and make it executable * $ chmod +x em-dl.php * * USAGE: * * When browsing the emusic.com site and your find music * you like just press the download link and save the file * to some location. Remember the location and run the script * with the downloaded emx file as argument, e.g.: * * ./em-dl.php 1231234.emx * * For a directoy filled with emx files use: * * ./em-dl.php * * * CONFIGURATION * * edit the following three lines * */ define ('DOWNLOAD_DIR' , '/home/dennis/music'); define ('VERBOSE', true ); define ('EXTENSION', 'mp3'); // {{{ __exec($command) /** * function for executing commands with php built-in command exec * @param string $command to execute * @return int $ret the value returned by the shell script being * executed through exec() */ function __exec($command){ $output = array(); exec($command.' 2>&1', $output, $ret); if ($ret == 0){ echo "Executed command: " . wordwrap($command) . " with success\n"; } else { echo "Something wrong: Here is what exec says: "; $end_output = ''; foreach($output as $key => $val){ $end_output.= $val; } $end_output = wordwrap($end_output) . "\n"; print $end_output; exit(255); } return $ret; } // }}} // {{{ get_tracklist($simple_xml) function get_tracklist($simple_xml){ $tracklist = $simple_xml->TRACKLIST; return $tracklist; } // }}} // {{{ space_replace ($str) function space_replace($str){ $str = str_replace(' ', '_', $str); $str = str_replace('/', '|', $str); // forward slash can confuse (directory) return $str; } // }}} // {{{ get_two_digit() function get_two_digit ($number){ if ($number > 0 && $number < 10){ $number = "0" . $number; } return $number; } // }}} // {{{ download_tracks($xml) function download_tracks($xml){ $tracklist = get_tracklist($xml); foreach ($tracklist->TRACK as $key => $val){ // dir to place track $save_dir = DOWNLOAD_DIR . "/" . space_replace($val->ARTIST) . "/" . space_replace($val->ALBUM); if (!file_exists($save_dir)){ mkdir($save_dir, 0755, true); } $save_file = $save_dir . "/" . get_two_digit($val->TRACKNUM) . "_" . space_replace($val->TITLE) . ".mp3"; $wget_exec = "wget -c -P \"$save_dir\" -O \"$save_file\" $val->TRACKURL"; __exec($wget_exec); } } // }}} // {{{ main() function main () { global $argv; foreach ($argv as $key => $val){ if ($key === 0){ continue; } if ($val == $argv[0]){ continue; } $xml = simplexml_load_file($val); download_tracks($xml); } } // }}} main(); exit(0);