Difference between revisions of "MediaGrabber"

From AwkwardTV
Jump to: navigation, search
(New page: This GPL script can be run on any platform with a php interpreter (windows, linux, osx, or on the ATV directly). Run it wherever you have your data (shared or not), and it will download al...)
 
m (Code)
Line 10: Line 10:
  
 
  <?php
 
  <?php
 
+
 
  ###############################################################################
 
  ###############################################################################
 
  # CoverGrabber
 
  # CoverGrabber
 
  # v1.1
 
  # v1.1
 
  ###############################################################################
 
  ###############################################################################
 
+
 
  # Retrieves covers for files in a given directory and all subdirectories
 
  # Retrieves covers for files in a given directory and all subdirectories
 
  # Cover images are saved as jpegs to <filename.ext>.jpg
 
  # Cover images are saved as jpegs to <filename.ext>.jpg
Line 28: Line 28:
 
  # A copy of the GPL License is available @ http://www.gnu.org/copyleft/gpl.html
 
  # A copy of the GPL License is available @ http://www.gnu.org/copyleft/gpl.html
 
  # Contact: konfoo at gmail dott com
 
  # Contact: konfoo at gmail dott com
 
+
 
  $ver="v1.1";
 
  $ver="v1.1";
 
+
 
  # Changelog
 
  # Changelog
 
  # v1.1 (kon) - added timestamping, usage
 
  # v1.1 (kon) - added timestamping, usage
 
  # v1.0 (kon) - initial release
 
  # v1.0 (kon) - initial release
 
+
 
 
 
  ###############################################################################
 
  ###############################################################################
 
  # directoryToArray
 
  # directoryToArray
Line 71: Line 70:
 
         closedir($handle);
 
         closedir($handle);
 
     }
 
     }
 
 
     return $array_items;
 
     return $array_items;
 
  }
 
  }
 
+
 
  ###############################################################################
 
  ###############################################################################
 
  # file_extension
 
  # file_extension
Line 84: Line 82:
 
     return $path_info['extension'];
 
     return $path_info['extension'];
 
  }
 
  }
 
+
 
  ###############################################################################
 
  ###############################################################################
 
  # stripDirectory
 
  # stripDirectory
Line 142: Line 140:
 
     return $array_items;
 
     return $array_items;
 
  }
 
  }
 
+
 
  ###############################################################################
 
  ###############################################################################
 
  # getMovies
 
  # getMovies
Line 156: Line 154:
 
     return $array_items;
 
     return $array_items;
 
  }
 
  }
 
+
 
  ###############################################################################
 
  ###############################################################################
 
  # fetchUrlasstring
 
  # fetchUrlasstring
Line 166: Line 164:
 
     return $contents;
 
     return $contents;
 
  }
 
  }
 
+
 
  ###############################################################################
 
  ###############################################################################
 
  # download
 
  # download
Line 199: Line 197:
 
   return true;
 
   return true;
 
  }
 
  }
 
+
 
  ###############################################################################
 
  ###############################################################################
 
  # getCoverimage
 
  # getCoverimage
Line 230: Line 228:
 
                     print("Invalid Cover\n");
 
                     print("Invalid Cover\n");
 
                 }
 
                 }
 
 
             }
 
             }
           
 
 
         }
 
         }
 
     }
 
     }
 
  }
 
  }
 
+
 
  ###############################################################################
 
  ###############################################################################
 
  # main
 
  # main
Line 284: Line 280:
 
  }
 
  }
 
  print("[".date('h:i:s')."] Finished!\n");
 
  print("[".date('h:i:s')."] Finished!\n");
 
 
  ?>
 
  ?>

Revision as of 13:03, 30 April 2007

This GPL script can be run on any platform with a php interpreter (windows, linux, osx, or on the ATV directly). Run it wherever you have your data (shared or not), and it will download all images automagically.

e.g. php covergrabber.php c:\temp\mymovies DVD

If you want to contribute via paypal as a way to say thank you, that would be appreciated. :)

Code

Paste this into a file called covergrabber.php.

<?php

###############################################################################
# CoverGrabber
# v1.1
###############################################################################

# Retrieves covers for files in a given directory and all subdirectories
# Cover images are saved as jpegs to <filename.ext>.jpg
# You should try to name your video files as close to the search text as
# possible. This is a given with movie titles, for example.
#
# Usage:
#   php covergrabber.php <target directory>
#   e.g. php covergrabber.php c:\temp\movies
#
# Released under the GPL
# A copy of the GPL License is available @ http://www.gnu.org/copyleft/gpl.html
# Contact: konfoo at gmail dott com

$ver="v1.1";

# Changelog
# v1.1 (kon) - added timestamping, usage
# v1.0 (kon) - initial release

###############################################################################
# directoryToArray
# recursively convert a dir to an array
###############################################################################
function directoryToArray($directory, $recursive) 
{
    $array_items = array();
    if ($handle = opendir($directory)) 
    {
        while (false !== ($file = readdir($handle))) 
        {
            if ($file != "." && $file != "..") 
            {
                if (is_dir($directory. "/" . $file))
                {
                    if($recursive) 
                    {
                        $array_items = array_merge($array_items, 
                            directoryToArray($directory. "/" . $file, 
                            $recursive));
                    }
                    $file = $directory . "/" . $file;
                    $array_items[] = $file;
                                        //preg_replace("/\/\//si", "/", $file);
                } 
                else 
                {
                    $file = $directory . "/" . $file;
                    $array_items[] = $file;
                                        //preg_replace("/\/\//si", "/", $file);
                }
            }
        }
        closedir($handle);
    }
    return $array_items;
}

###############################################################################
# file_extension
# return ext for a file
###############################################################################
function file_extension($filename)
{
    $path_info = pathinfo($filename);
    return $path_info['extension'];
}

###############################################################################
# stripDirectory
# strip junk items that are not media from a directory array
###############################################################################
function stripDirectory($directory, $directoryArray) 
{
    $array_items = array();
    foreach($directoryArray as $val)
    {
        //$val = str_replace($directory, , $val );
        $ext = strtolower(file_extension($val));
        if($ext) 
        {
            if (($ext==='mpg')||($ext==='avi')||($ext==='vob')
                ||($ext==='mp4')||($ext==='mkv')||($ext==='mpeg')
                ||($ext==='mpeg4'))
            {
                $val=strtolower($val);
                $val=str_replace('cd1', , $val);
                $val=str_replace('cd2', , $val);
                $val=str_replace('cd3', , $val);
                $val=str_replace('cd4', , $val);
                $val=str_replace('part 1', , $val);
                $val=str_replace('part 2', , $val);
                $val=str_replace('part 3', , $val);
                $val=str_replace('part 4', , $val);
                $val=str_replace('part 5', , $val);
                $val=str_replace('part 6', , $val);
                $val=str_replace('1of1', , $val);
                $val=str_replace('1of2', , $val);
                $val=str_replace('2of2', , $val);
                $val=str_replace('1of3', , $val);
                $val=str_replace('2of3', , $val);
                $val=str_replace('3of3', , $val);
                $val=str_replace('1of4', , $val);
                $val=str_replace('2of4', , $val);
                $val=str_replace('3of4', , $val);
                $val=str_replace('4of4', , $val);
                $val=str_replace('1 of 1', , $val);
                $val=str_replace('1 of 2', , $val);
                $val=str_replace('2 of 2', , $val);
                $val=str_replace('1 of 3', , $val);
                $val=str_replace('2 of 3', , $val);
                $val=str_replace('3 of 3', , $val);
                $val=str_replace('1 of 4', , $val);
                $val=str_replace('2 of 4', , $val);
                $val=str_replace('3 of 4', , $val);
                $val=str_replace('4 of 4', , $val);
                $val=str_replace('-', ' ', $val);
                $val=str_replace('_', ' ', $val);
                $val=rtrim($val,"\x20");
                $array_items[] = $val;
            }
        }
    }
    return $array_items;
}

###############################################################################
# getMovies
# extract title names from a directory array
###############################################################################
function getMovies($fileList)
{
    $array_items = array();
    foreach($fileList as $val)
    {
        $array_items[] = basename($val,".".file_extension($val));
    }
    return $array_items;
}

###############################################################################
# fetchUrlasstring
# grab a url, return a string
###############################################################################
function fetchUrlasstring($url)
{
    $contents=file_get_contents($url);
    return $contents;
}

###############################################################################
# download
# download a file from a given URL
###############################################################################
function download($file_source, $file_target)
{
    $file_source = str_replace(' ', '%20', html_entity_decode($file_source));
    if (file_exists($file_target)) 
    {
        chmod($file_target, 0777); 
    }
    if (($rh = fopen($file_source, 'rb')) === FALSE) 
    { 
        return false; 
    }
    if (($wh = fopen($file_target, 'wb')) === FALSE) 
    { 
        return false; 
    }
    while (!feof($rh))
    {
        if (fwrite($wh, fread($rh, 1024)) === FALSE) 
        { 
            fclose($rh); 
            fclose($wh); 
            return false; 
        }
  }
  fclose($rh);
  fclose($wh);
  return true;
}

###############################################################################
# getCoverimage
# grab a cover image from a remote site for a given title, save to disk
###############################################################################
function getCoverimage($fileName,$movieName,$searchURL,$cPreURL,$cPosURL,
                        $totalItems,$currentItem)
{
    print("[".date('h:i:s').'] "'.$movieName.'" ('.$currentItem
            .' of '.$totalItems.'): ');
    $searchResult=fetchUrlasstring($searchURL.urlencode($movieName));
    if ($searchResult)
    {
        $itemStartpos=strpos($searchResult,'item_id=');
        if ($itemStartpos)
        {
            $itemEndpos=strpos($searchResult,'&',$itemStartpos-1);
            $chopStr=substr($searchResult,$itemStartpos+8,
                            $itemEndpos-$itemStartpos-8);
            if ($chopStr)
            {
                $imageURL=$cPreURL.$chopStr.$cPosURL;
                print('Grabbing... ');
                if (download($imageURL,$fileName.'.jpg'))
                {
                    print("Done\n");
                }
                else
                {
                    print("Invalid Cover\n");
                }
            }
        }
    }
}

###############################################################################
# main
# main loop
###############################################################################
if ((!$argv[1])||(!$argv[2]))
{
    print("Cover Grabber ".$ver."\nAuthor: konfoo at gmail dott com\n");
    print("An automated downloader for HDDVD, DVD and other cover images\n\n");
    print("Released under the GPL. See contents for more info.\n");
    print("A copy of the GPL License is available at:\n");
    print("       http://www.gnu.org/licenses/gpl.txt\n\n");
    print("Usage: covergrabber <target directory> <searchtype>\n");
 print("       covergrabber c:\\temp\\movies DVD\n");
    print("       covergrabber c:\\temp\\tvshows All\n\n");
    print("Supported searchtypes:\n");
    print("       All, DVD, HDDVD, Bluray, UMD, Music, VOD, User\n");
    die;
}
error_reporting(0);
ini_set('default_socket_timeout', 120);    
print("[".date('h:i:s')."] Cover Grabber ".$ver."\n");
print("[".date('h:i:s')."] Author: konfoo at gmail dott com\n");
print("[".date('h:i:s')."] Released under the GPL. See contents for more info.\n");
print("[".date('h:i:s')."] A copy of the GPL License is available at:\n");
print("[".date('h:i:s')."] http://www.gnu.org/licenses/gpl.txt\n");
print("[".date('h:i:s')."] Initializing\n");
$searchString='http://www.dv'.'dempire.com/Exec/v4'.'_search_pre.asp?userid='
                .rand(99000000000000,99555555555555)
                .'&view=&pp=&site_id=4&search_type='.$argv[2]
                .'&media_type=0&search_string=';
$coverPre='http://images2.dv'.'dempire.com/gen/movies/';
$coverPos='h.jpg';
print("[".date('h:i:s')."] Building list of files\n");
$rescursiveDirlist=directoryToArray($argv[1],true);
print("[".date('h:i:s')."] Removing invalid entries and filetypes\n");
$fileList=stripDirectory($argv[1],$rescursiveDirlist);
print("[".date('h:i:s')."] Building list of titles\n");
$movieList=getMovies($fileList);
$counter=0;
print("[".date('h:i:s')."] Starting Cover Download...\n");
foreach($fileList as $val)
{
    getCoverimage($fileList[$counter],$movieList[$counter],$searchString,
                    $coverPre,$coverPos,count($fileList),$counter+1);
    $counter++;
}
print("[".date('h:i:s')."] Finished!\n");
?>