#!/opt/bin/php formatOutput = true; $xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/css" href="bibDisplay.css"'); $doc->appendChild($xslt); // add the top element, import DC $movies = $doc->createElement( "subtitleDB" ); $movies->setAttribute ( 'xmlns', "http://purl.org/dc/elements/1.1/"); $doc->appendChild( $movies ); // scan data directory $fd = @opendir('data'); while (($foldername = @readdir($fd)) == true) { // scan each movie folder if ($foldername != '.' && $foldername != '..') ScanMoviePath($doc, $movies, $foldername); } // dump the xml echo $doc->saveXML(); // scan a movie for information and dump to xml function ScanMoviePath($doc, $movies, $foldername) { // fetch 8 lines of metadata $file = fopen('data/' . $foldername . '/info.txt', 'r'); $MDTitle = U(fgets($file)); $MDRating = U(fgets($file)); $MDDirectors = U(fgets($file)); $MDWriters = U(fgets($file)); $MDYear = U(fgets($file)); $MDGenre = U(fgets($file)); $MDTagline = U(fgets($file)); $MDPlot = U(fgets($file)); $MDImdbLink = U(fgets($file)); $MDIsbn = $foldername; fclose($file); // strip metadata $MDRating = str_replace('User Rating: ', '', $MDRating); $MDGenre = str_replace('Genre: ', '', $MDGenre); $MDDirectors = str_replace('Directors: ', '', $MDDirectors); $MDWriters = str_replace('Writers: ', '', $MDWriters); $MDWriters = str_replace('Writer: ', '', $MDWriters); $MDDirectors = str_replace('Director: ', '', $MDDirectors); $MDTitle = str_replace('Title: ', '', $MDTitle); $MDYear = str_replace('Year: ', '', $MDYear); $MDTagline = str_replace('Tagline: ', '', $MDTagline); $MDPlot = str_replace('Plot: ', '', $MDPlot); // create movie element, with ISBN and DVDTitle attributes $movie = $doc->createElement( "video" ); $movies->appendChild( $movie ); $movie->setAttribute( 'ISBN', $foldername ); $movie->setAttribute( 'DVDTitle', $MDTitle ); // create the metadata element $metadata = $doc->createElement( "metadata" ); $movie->appendChild( $metadata ); // create the title element $title = $doc->createElement( "title"); $metadata->appendChild( $title ); $title->appendChild( $doc->createTextNode ($MDTitle) ); // create the ISBN element, with fancy urn linkin' stuff $isbn = $doc->createElement( "link"); $metadata->appendChild( $isbn ); $isbn->appendChild( $doc->createTextNode ($MDIsbn) ); $isbn->setAttribute( 'rel', 'DC.relation' ); $isbn->setAttribute( 'href', 'urn:'.'ISBN:'.$foldername ); // create creator / director element with Director attribute using foreach loop to take a group and make into foreach ( explode(",",$MDDirectors) as $IndividualDirector) { $director = $doc->createElement( "creator"); $metadata->appendChild( $director ); $director->appendChild( $doc->createTextNode ( trim($IndividualDirector)) ); $director->setAttribute('type',"Director"); } // create description / ISBN rating element with attribute IMDB User Rating $rating = $doc->createElement( "description"); $metadata->appendChild( $rating ); $rating->appendChild( $doc->createTextNode ($MDRating) ); $rating->setAttribute('type',"IMDB User Rating"); // create creator / writer element with Writer attribute using foreach loop to take a group and make into foreach ( explode(",",$MDWriters) as $IndividualWriter) { $writer = $doc->createElement( "creator"); $metadata->appendChild( $writer ); $writer->appendChild( $doc->createTextNode ( trim($IndividualWriter)) ); $writer->setAttribute('type',"Writer"); } $year = $doc->createElement( "date"); $metadata->appendChild( $year ); $year->appendChild( $doc->createTextNode (trim($MDYear)) ); foreach ( explode(",",$MDGenre) as $IndividualGenre) { $genre = $doc->createElement( "subject"); $metadata->appendChild( $genre ); $genre->appendChild( $doc->createTextNode ( trim($IndividualGenre)) ); } $tagline = $doc->createElement( "description"); $metadata->appendChild( $tagline ); $tagline->appendChild( $doc->createTextNode ($MDTagline) ); $tagline->setAttribute('type',"Film Tagline"); $plot = $doc->createElement( "description"); $metadata->appendChild( $plot ); $plot->appendChild( $doc->createTextNode ($MDPlot) ); $plot->setAttribute('type',"Plot Summary"); // scan subtitle directory $fd = @opendir('data/' . $foldername . '/'); while (($srt = @readdir($fd)) == true) { // parse each *.srt as an srt file if (preg_match('/^.*srt$/', $srt)) ScanlanguageSrt($doc, $movie, $foldername, $srt, $MDTitle); } } // scan a movie language for information and dump to xml function ScanlanguageSrt($doc, $movie, $foldername, $srt, $movietitle) { $movielanguage = basename($srt, '.srt'); $movielanguage = substr($movielanguage, 0, 3); // create the subtitles element $subtitles = $doc->createElement( "subtitles" ); $movie->appendChild( $subtitles ); $subtitles->setAttribute( 'language', $movielanguage ); if (strlen(strstr($srt,'os'))>0) { $subtitles->setAttribute( 'source', 'OpenSubtitles.org'); } // open the srt file $file = fopen('data/' . $foldername . '/' . $srt, 'r'); // scan it ScanSrt($file, $doc, $subtitles, $movietitle, $movielanguage); // close things down fclose($file); } function U($str) { // some folder have bad unicode, strip nonprintables $str = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $str); return $str; } function ScanSrt($file, $doc, $language, $movietitle, $movielanguage) { while (true) { // read the first two lines $line1 = U(fgets($file)); $line2 = U(fgets($file)); // read the content $content = U(fgets($file)); while (!feof($file)) { $line= U(fgets( $file)); if ($line == "") break; $content = $content . " " . $line; } // we are finished if we are at the end of the file if (feof($file)) return; // extract the start and end times $pos = strpos($line2, "-->"); $start = substr($line2, 0, $pos-1); $end = substr($line2, $pos+4); // add the xml $subtitle = $doc->createElement( "subtitle", sillychar($content) ); $language->appendChild( $subtitle ); // subtitle start & end time $timeframe = $start.'-'.$end; $timeframe = str_replace(':', '', $timeframe); $timeframe = str_replace(',', '', $timeframe); //$filmtitle = $movietitle; $filmtitle = str_replace('Title: ','',$movietitle); $filmlanguage = substr($movielanguage,0,3); $subID = $filmlanguage . '_' . $filmtitle . '_' . $timeframe; $subtitle->setAttribute( 'subID', $subID); // subtitle end time // $subtitle->setAttribute( 'end', $end ); } } function sillychar($content) { $content = str_replace('&','&',$content); $content = str_replace('>','>',$content); $content = str_replace('<','<',$content); return $content; } function ScanInfo($file, $doc, $language) { // read the first line of the .info file $file = fopen('data/' . $foldername . '/info.txt', 'r'); $infoText = U(fgets($file)); fclose($file); while (true) { // read the eight lines $line1 = U(fgets($file)); $line2 = U(fgets($file)); $line3 = U(fgets($file)); $line4 = U(fgets($file)); $line5 = U(fgets($file)); $line6 = U(fgets($file)); $line7 = U(fgets($file)); $line8 = U(fgets($file)); // read the content $content = U(fgets($file)); while (!feof($file)) { $line= U(fgets($file)); if ($line == "") break; $content = $content + " " + $line; } // we are finished if we are at the end of the file if (feof($file)) return; } } ?>