/movies_get_images
PHP | 145 lines | 134 code | 9 blank | 2 comment | 20 complexity | d53fbf50b68b12e2c9a0373104bc5d09 MD5 | raw file
Possible License(s): GPL-2.0
- #!/usr/bin/env php
- <?php
- $dirs = array();
- if (count($argv) > 1) {
- for ($x=1; $x < count($argv); $x++) {
- $dirs[] = $argv[$x];
- }
- } else {
- $dirs[] = '/media/movies';
- }
- $options['recursive'] = true;
- while(count($dirs) > 0) {
- $dir = array_shift($dirs);
-
- if (!file_exists($dir) || !is_dir($dir)) {
- print "Error: Directory does not exist: $dir\n\n";
- continue;
- }
- print "dir=$dir\n";
- $dp = opendir($dir);
- while ($file = readdir($dp)) {
- if (substr($file, 0, 1) == '.') {
- continue;
- }
- if (is_dir($dir . '/' . $file)) {
- if ($options['recursive']) {
- $dirs[] = $dir . '/' . $file;
- print "adding $dir/$file as a directory\n";
- } else {
- print "$file is a directory, skipping\n";
- }
- continue;
- }
- // print $file."\n";
- $pathinfo = pathinfo($file);
- if (!isset($pathinfo['extension'])) {
- print_r($pathinfo);
- print "no extension\n";
- exit;
- }
- switch ($pathinfo['extension']) {
- case 'xml':
- case 'metathumb':
- continue;
- case 'jpg':
- continue;
- case 'mp4':
- case 'm4v':
- case 'avi':
- case 'mkv':
- case 'mpg':
- $coverFile = $dir . '/' . $pathinfo['filename'] . '.jpg';
- $missing = array();;
- if (!preg_match('/\([0-9]{4}\)/', $file)) {
- print "year Missing for $file\n";
- $missing['year'] = true;
- }
-
- if (file_exists($coverFile)) {
- if (!empty($options['verbose'])) {
- print "Cover found for $file\n";
- }
- } else {
- print "couldn't find: $coverFile\n";
- $missing['cover'] = true;
-
- print_r($pathinfo);
- $data = getData($pathinfo['filename']);
- if (isset($data['YEAR'])) {
- $year = $data['YEAR'];
- if (isset($missing['year'])) {
- print "\tFOUND year: $year\n";
- $origFile = $dir . '/' . $file;
- $newFilename = $pathinfo['filename'] . " ($year)";
- $newFile = $dir . '/' . $newFilename. '.' . $pathinfo['extension'];
- rename($origFile, $newFile);
- $pathinfo['filename'] = $newFilename;
- print "rename($origFile, $newFile)\n";
- $coverFile = $dir . '/' . $pathinfo['filename'] . '.jpg';
-
- // exit;
- unset($missing['year']);
- }
- }
- if (isset($missing['cover'])) {
- if (isset($data['POSTER_LARGE'])) {
- $poster = $data['POSTER_LARGE'];
- } elseif (isset($data['POSTER_FULL'])) {
- $poster = $data['POSTER_FULL'];
- } elseif (isset($data['POSTER'])) {
- $poster = $data['POSTER'];
- } else {
- print "poster not found for $file\n";
- exit;
- }
- if (!empty($poster)) {
- print "\tFOUND cover $poster\n";
- $posterData = file_get_contents($poster);
- $fp = fopen($coverFile, 'w');
- fwrite($fp, $posterData);
- fclose($fp);
- unset($missing['cover']);
- }
- }
-
- if (count($missing) > 0) {
- print_r($data);
- }
-
- // exit;
- }
- // sleep(1);
- break;
- default:
- print "What do I do with: {$pathinfo['extension']}\n";
- // print_r($pathinfo);
- }
- // print $file."\n";
- }
- }
- exit;
- $search = "cloud atlas";
- $data = getData($search);
- print __FILE__.':'.__LINE__.' '.print_r($data, true)."\n"; // EKT
- function getData($search)
- {
- $url = 'http://phact.generation-i.com/imdb/imdbWebService.php?m=' . urlencode($search) . '&o=json';
- $json = file_get_contents($url);
- $data = json_decode($json, true);
- if ($data === false) {
- print __FILE__.':'.__LINE__.' '.print_r($json, true)."\n"; // EKT
- print __FILE__.':'.__LINE__.' '.print_r($url, true)."\n"; // EKT
- return false;
- }
-
- return $data;
- }