PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/movies_get_images

https://bitbucket.org/thelin/tools
PHP | 145 lines | 134 code | 9 blank | 2 comment | 20 complexity | d53fbf50b68b12e2c9a0373104bc5d09 MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/env php
  2. <?php
  3. $dirs = array();
  4. if (count($argv) > 1) {
  5. for ($x=1; $x < count($argv); $x++) {
  6. $dirs[] = $argv[$x];
  7. }
  8. } else {
  9. $dirs[] = '/media/movies';
  10. }
  11. $options['recursive'] = true;
  12. while(count($dirs) > 0) {
  13. $dir = array_shift($dirs);
  14. if (!file_exists($dir) || !is_dir($dir)) {
  15. print "Error: Directory does not exist: $dir\n\n";
  16. continue;
  17. }
  18. print "dir=$dir\n";
  19. $dp = opendir($dir);
  20. while ($file = readdir($dp)) {
  21. if (substr($file, 0, 1) == '.') {
  22. continue;
  23. }
  24. if (is_dir($dir . '/' . $file)) {
  25. if ($options['recursive']) {
  26. $dirs[] = $dir . '/' . $file;
  27. print "adding $dir/$file as a directory\n";
  28. } else {
  29. print "$file is a directory, skipping\n";
  30. }
  31. continue;
  32. }
  33. // print $file."\n";
  34. $pathinfo = pathinfo($file);
  35. if (!isset($pathinfo['extension'])) {
  36. print_r($pathinfo);
  37. print "no extension\n";
  38. exit;
  39. }
  40. switch ($pathinfo['extension']) {
  41. case 'xml':
  42. case 'metathumb':
  43. continue;
  44. case 'jpg':
  45. continue;
  46. case 'mp4':
  47. case 'm4v':
  48. case 'avi':
  49. case 'mkv':
  50. case 'mpg':
  51. $coverFile = $dir . '/' . $pathinfo['filename'] . '.jpg';
  52. $missing = array();;
  53. if (!preg_match('/\([0-9]{4}\)/', $file)) {
  54. print "year Missing for $file\n";
  55. $missing['year'] = true;
  56. }
  57. if (file_exists($coverFile)) {
  58. if (!empty($options['verbose'])) {
  59. print "Cover found for $file\n";
  60. }
  61. } else {
  62. print "couldn't find: $coverFile\n";
  63. $missing['cover'] = true;
  64. print_r($pathinfo);
  65. $data = getData($pathinfo['filename']);
  66. if (isset($data['YEAR'])) {
  67. $year = $data['YEAR'];
  68. if (isset($missing['year'])) {
  69. print "\tFOUND year: $year\n";
  70. $origFile = $dir . '/' . $file;
  71. $newFilename = $pathinfo['filename'] . " ($year)";
  72. $newFile = $dir . '/' . $newFilename. '.' . $pathinfo['extension'];
  73. rename($origFile, $newFile);
  74. $pathinfo['filename'] = $newFilename;
  75. print "rename($origFile, $newFile)\n";
  76. $coverFile = $dir . '/' . $pathinfo['filename'] . '.jpg';
  77. // exit;
  78. unset($missing['year']);
  79. }
  80. }
  81. if (isset($missing['cover'])) {
  82. if (isset($data['POSTER_LARGE'])) {
  83. $poster = $data['POSTER_LARGE'];
  84. } elseif (isset($data['POSTER_FULL'])) {
  85. $poster = $data['POSTER_FULL'];
  86. } elseif (isset($data['POSTER'])) {
  87. $poster = $data['POSTER'];
  88. } else {
  89. print "poster not found for $file\n";
  90. exit;
  91. }
  92. if (!empty($poster)) {
  93. print "\tFOUND cover $poster\n";
  94. $posterData = file_get_contents($poster);
  95. $fp = fopen($coverFile, 'w');
  96. fwrite($fp, $posterData);
  97. fclose($fp);
  98. unset($missing['cover']);
  99. }
  100. }
  101. if (count($missing) > 0) {
  102. print_r($data);
  103. }
  104. // exit;
  105. }
  106. // sleep(1);
  107. break;
  108. default:
  109. print "What do I do with: {$pathinfo['extension']}\n";
  110. // print_r($pathinfo);
  111. }
  112. // print $file."\n";
  113. }
  114. }
  115. exit;
  116. $search = "cloud atlas";
  117. $data = getData($search);
  118. print __FILE__.':'.__LINE__.' '.print_r($data, true)."\n"; // EKT
  119. function getData($search)
  120. {
  121. $url = 'http://phact.generation-i.com/imdb/imdbWebService.php?m=' . urlencode($search) . '&o=json';
  122. $json = file_get_contents($url);
  123. $data = json_decode($json, true);
  124. if ($data === false) {
  125. print __FILE__.':'.__LINE__.' '.print_r($json, true)."\n"; // EKT
  126. print __FILE__.':'.__LINE__.' '.print_r($url, true)."\n"; // EKT
  127. return false;
  128. }
  129. return $data;
  130. }