Difference between revisions of "MediaGrabber"

From AwkwardTV
Jump to: navigation, search
m (v1.4)
(Feature Requests)
 
(14 intermediate revisions by 4 users not shown)
Line 1: Line 1:
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.  
+
MediaGrabber grabs cover images and metadata for your media files automagically. Just point it at the directory root of your media items and it will scan all subdirectories and download covers and movie/tv/hddvd/etc. info automatically.  
  
  e.g. php covergrabber.php c:\temp\mymovies DVD
+
You can use it with ATVFiles, and even other platforms such as Vista Media Center's My Videos.
 +
 
 +
This script can be run on any platform with a php interpreter (windows, linux, osx, or on the ATV directly).
 +
 
 +
  e.g. php mediagrabber.php c:\temp\mymovies DVD
  
 
If you want to contribute via paypal as a way to say thank you, that would be appreciated. :)
 
If you want to contribute via paypal as a way to say thank you, that would be appreciated. :)
 +
 +
=== Version History ===
 +
 +
v1.5 (kon) - now retrieves XML metadata
 +
              renamed to mediagrabber
 +
              changed license to better reflect intended non-commercial use
 +
v1.4 (kon) - fixed file extension bs
 +
v1.3 (kon) - fixed case sensitive bug
 +
v1.2 (kon) - fixed stripping bug
 +
v1.1 (kon) - added timestamping, usage
 +
v1.0 (kon) - initial release
  
 
=== Code ===
 
=== Code ===
  
Paste this into a file called covergrabber.php.
+
* Paste this into a file called mediagrabber.php.
 +
* Download a version of PHP
 +
* Place PHP and this script on your media storage server or the AppleTV
 +
* Run the script with no parameters i.e. php mediagrabber.php
 +
* Add command line parameters as required
 +
* Download the php code: http://www.datacast.biz/mediagrabber/mediagrabber.php.txt (select all, copy, and paste to a new text document and rename to mediagrabber.php)
 +
* Windows executable: http://www.datacast.biz/mediagrabber/mediagrabber.exe
 +
* Or use the direct download links: http://www.datacast.biz/mediagrabber
 +
* If necessary run dos2unix or unix2dos on the file
 +
 
 +
=== Feature Requests ===
 +
 
 +
Please add any features you would like in this section.
 +
 
 +
* Add support for movies with QT file extension (.mov)
 +
* Give user the option to select title from multiple hits
 +
* Use another service for TV (i.e. Yahoo TV)
 +
* Add support for DVD movies in VIDEO_TS folders
  
<?php
+
[[Category:Software]]
###############################################################################
 
# CoverGrabber
 
# v1.4
 
###############################################################################
 
# 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.4";
 
# NOTES
 
# - Make sure your version of PHP allows for fopen() to remote urls
 
#  If not, all your cover downloads will fail
 
#
 
# Changelog
 
# v1.4 (kon) - fixed file extension bs
 
# v1.3 (kon) - fixed case sensitive bug
 
# v1.2 (kon) - fixed stripping bug
 
# 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;
 
                }
 
                else
 
                {
 
                    $file = $directory . "/" . $file;
 
                    $array_items[] = $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)
 
    {
 
        $ext = strtolower(file_extension($val));
 
        if($ext)
 
        {
 
            if (($ext==='mpg')||($ext==='avi')||($ext==='vob')
 
                ||($ext==='mp4')||($ext==='mkv')||($ext==='mpeg')
 
                ||($ext==='mpeg4')||($ext==='m4v'))
 
            {
 
                $array_items[] = $val;
 
            }
 
        }
 
    }
 
    return $array_items;
 
}
 
###############################################################################
 
# stripExts
 
# strip common extensions i.e. cd 1 of 2, etc.
 
###############################################################################
 
function stripExts($val)
 
{
 
    $val=str_replace('cd1', "", $val);
 
    $val=str_replace('cd2', "", $val);
 
    $val=str_replace('cd3', "", $val);
 
    $val=str_replace('CD4', "", $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('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");
 
    return $val;
 
}
 
###############################################################################
 
# stripFileeext
 
# strip off the file extension
 
###############################################################################
 
function stripFileeext($fileName)
 
{
 
    return substr($fileName, 0, strrpos($fileName, '.'));
 
}
 
###############################################################################
 
# 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').'] "'.stripExts($movieName).'" ('.$currentItem
 
            .' of '.$totalItems.'): ');
 
    $searchResult=fetchUrlasstring($searchURL
 
                                    .urlencode(stripExts($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,stripFileeext($fileName).'.jpg'))
 
                {
 
                    print("Done\n");
 
                }
 
                else
 
                {
 
                    print("Invalid Cover\n");
 
                }
 
            }
 
        }
 
    }
 
}
 
###############################################################################
 
# main
 
# main loop
 
###############################################################################
 
if ((!$argv[1])||(!$argv[2]))
 
{
 
    print("CoverGrabber ".$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')."] CoverGrabber ".$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)
 
{
 
    if (file_exists($fileList[$counter].".jpg"))
 
    {
 
        print("[".date('h:i:s').'] Skipping Existing Cover "'
 
              .stripExts($movieList[$counter]).'"'."\n");
 
    }
 
    else
 
    {
 
        getCoverimage($fileList[$counter],$movieList[$counter],$searchString,
 
                        $coverPre,$coverPos,count($fileList),$counter+1);
 
    }
 
    $counter++;
 
}
 
print("[".date('h:i:s')."] Finished!\n");
 
?>
 

Latest revision as of 11:36, 21 March 2008

MediaGrabber grabs cover images and metadata for your media files automagically. Just point it at the directory root of your media items and it will scan all subdirectories and download covers and movie/tv/hddvd/etc. info automatically.

You can use it with ATVFiles, and even other platforms such as Vista Media Center's My Videos.

This script can be run on any platform with a php interpreter (windows, linux, osx, or on the ATV directly).

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

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

Version History

v1.5 (kon) - now retrieves XML metadata
             renamed to mediagrabber
             changed license to better reflect intended non-commercial use
v1.4 (kon) - fixed file extension bs
v1.3 (kon) - fixed case sensitive bug
v1.2 (kon) - fixed stripping bug
v1.1 (kon) - added timestamping, usage
v1.0 (kon) - initial release

Code

Feature Requests

Please add any features you would like in this section.

  • Add support for movies with QT file extension (.mov)
  • Give user the option to select title from multiple hits
  • Use another service for TV (i.e. Yahoo TV)
  • Add support for DVD movies in VIDEO_TS folders