#!/usr/bin/php
<?php
define
("DOWNLOAD_DIR"'/nas/maxtor3/ogg_casts/incoming');
define("OUTPUT_DIR",   '/nas/maxtor3/ogg_casts/out');


define('BEGIN_COMMENTS''User comments section follows...'); 

$podcasts = array('http://www.gnegg.ch/las_convert.php''http://www.thelinuxlink.net/tllts/tllts_ogg.rss');


function 
_sort_entries($a$b){
  if (
$a['date'] == $b['date']) {
        return 
0;
    }
    return (
$a['date'] > $b['date']) ? -1;
}

function 
downloadFiles($casturl){
    
$cast simplexml_load_file($casturl);
    
$urls = array();

    
$title $cast->channel->title;
    
$target_dir DOWNLOAD_DIR."/$title";
    
$output_dir OUTPUT_DIR."/$title";
    if (!
is_dir($target_dir)) mkdir($target_dir);
    if (!
is_dir($output_dir)) mkdir($output_dir);


    foreach(
$cast->channel->item as $item){
        if(
$item->enclosure['url']){
            
$urls[] = array('url' => (string)$item->enclosure['url'], 'date' => strtotime((string)$item->pubDate));         
        }
    }
    
usort($urls'_sort_entries');
    
array_splice($urls3);
    
$tmp = array(); 
    foreach(
$urls as $u){
       
$tmp[] = $u['url'];
    }
    
$urls $tmp;
    
$do_convert false;
    foreach(
$urls as $url){
        
$file basename($url'.ogg');
        if (!
file_exists($target_dir."/$file.ogg") && !file_exists($output_dir."/$file.m4a")){
            
$target escapeshellarg("$target_dir/$file.ogg");
            echo 
"$file not found in archive. Downloading...\n";
            
system(sprintf('wget -O %s %s'$target$url));
            
$do_convert true;
        }else{
            echo 
"$file already exists in archive $std. Skipping\n";        
        }
    }
}

function 
getOggInfo($path){
    
$path escapeshellarg($path);
    
$info = `ogginfo $path`;
    
$info substr($infostrpos($info,BEGIN_COMMENTS) + strlen(BEGIN_COMMENTS));
    
preg_match_all('#\s+([a-z]+)=(.+)\n#i'$info$matchesPREG_SET_ORDER);
    
$info = array();
    foreach(
$matches as $match)
        
$info[strtolower(trim($match[1]))] = trim($match[2]);  
    return 
$info;
}

function 
convert_dir($entry){
    if (!
is_dir(OUTPUT_DIR."/$entry")) mkdir(OUTPUT_DIR."/$entry");
    
$d dir(DOWNLOAD_DIR."/$entry");
    while((
$file $d->read()) !== false){
        if (!
preg_match('#\.ogg$#'$file)) continue;
        
$f basename($file'.ogg');
        if (
file_exists(OUTPUT_DIR."$entry/$f.m4a")){
           echo 
"$f already converted. Removing original\n";
           
unlink(DOWNLOAD_DIR."/$entry/$file");
           continue;
        }
        
$info getOggInfo(DOWNLOAD_DIR."/$entry/$file");
        
print_r($info);
        echo 
"Converting file...\n";
        
system(sprintf('oggdec -Q %s -o - | faac -o %s --artist %s --album %s --genre %s --title %s --track %s --year %s -',
                       
escapeshellarg(DOWNLOAD_DIR."/$entry/$file"),
                       
escapeshellarg(OUTPUT_DIR."/$entry/$f.m4a"),
                       
escapeshellarg($info['artist']),
                       
escapeshellarg($info['album']),
                       
escapeshellarg($info['genre']),
                       
escapeshellarg($info['title']),
                       
escapeshellarg($info['tracknumber'] ? $info['tracknumber'] : 1),
                       
escapeshellarg($info['year'] ? $info['year'] : date('Y'))));
        echo 
"\n";
        
unlink(DOWNLOAD_DIR."/$entry/$file");
    }
}

echo 
"Pilif's OGG-Cast-Converter\n";
foreach(
$podcasts as $url){
    echo 
"Working with $url\n";
    
downloadFiles($url);
}
echo 
"Download done!\n";
echo 
"Checking for needed conversion\n";
$d dir(DOWNLOAD_DIR);
while((
$entry $d->read()) !== false){
    if (
preg_match('#^\.+$#'$entry)) continue;
    if (
is_dir(DOWNLOAD_DIR."/$entry")){
        echo 
"Checking for unconverted files in cast '$entry'\n";
        
convert_dir($entry);
    }

$d->close();
?>