In my quest to create a more organized move folder, I wrote the following script that parses the mymovies.xml file that is created with a scraper such as media browser or media center master and builds a folder structure with symbolic links that points to the real move. This is for *nix based system but it should run fine on windows with a few changes (i.e windows uses hardlinks not symlinks).
I also added a method to detect "new releases" with some limitations due to my php version and laziness.
This has made browsing my movies a lot less painful from within windows (browsing via samba). Of course, if you are running a media center, you wont have this issue but in my case, I dont always want to fire up a media center to watch a move.
As always, use at your own risk if interested.
PHP Code:
#!/usr/bin/php
<?php
ini_set("display_errors", 1);
# set the UID nad GID to a local system user. Not required.
posix_setgid(100);
posix_setuid(1026);
# location of movie folder
#
$dir_root = "/volume1/video/Movies/";
# destination path to build genre structure.
#
$destination_path = "/volume1/video/Genres/";
# number of days that a movie is considered new. < 30
$newReleaseDays = 5;
#initializes array to 0 and initialized genre array.
#
$x=0;
$genres = Array();
# Builds an array of genres. outputs genres[genre][x]=> movie_name
# Assumes each movie is within its own folder. Does not support sub folders.
# Looks for mymovies.xml within the movie root. mymoves.xml can be created with Media Center Master
# or Media browser.
#
# $dir_list contains the path to each folder that contains mymovies.xml
#
$dir_list = scandir($dir_root);
$xml = new XMLReader();
foreach($dir_list as $dir){
if(file_exists($dir_root.$dir.'/mymovies.xml')){
$mymovies = $dir_root.$dir.'/mymovies.xml';
$xml->open($mymovies);
#
# Needs to be rewritten once system is upgraded to >= php 5.3.0
# in order to use newer date functions. For now, only days are supportted < 30
#
$changeTime = date_parse(date("Y-m-d", filectime($mymovies)));
$today = date_parse(date("Y-m-d"));
if((($today['year'] - $changeTime['year']) == 0) && (($today['month'] - $changeTime['month']) == 0) && (($today['day'] - $changeTime['day']) < $newReleaseDays)){
echo "$dir has been tagged as a new release....\n";
$genres['New Releases'][$x] = $dir;
$x++;
}
while($xml->read()){
if($xml->nodeType == XMLReader::ELEMENT){
if($xml->localName == 'Genre'){
$xml->read();
$genres[$xml->value][$x] = $dir;
$x++;
}
}
}
}
else {
echo "Unable to locate mymovies.xml, skiping....\n";
}
}
unset($dir_list);
function dirCreator($item, $movie_folder) {
global $dir_root, $destination_path;
echo "-->Verifying if folder $movie_folder exists \n";
if(is_dir($destination_path.$movie_folder)){
echo "---->$movie_folder found. \n";
}
else {
echo "---->$movie_folder does not exist, creating. \n";
if(!mkdir($destination_path.$movie_folder, 0777, false)) {
echo "------>Failed to create folder $movie_folder \n\n\n";
}
}
if(!chdir($destination_path.$movie_folder)) {
echo "---->Unable to open directory\n";
}
else {
foreach($item as $movie){
echo "---->Creating symbolic links.\n";
if(!symlink($dir_root.$movie,$movie)){
echo "------>Failed to create symbolic link ".$movie."\n";
}
else {
echo "------>".$movie." created. \n";
}
}
}
}
if(!empty($genres)) {
array_walk($genres,'dirCreator');
}
?>