movieScraper.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. ignore_user_abort(true);
  3. class MovieScraper {
  4. private $apiURL = "http://api.themoviedb.org/3/search/movie?api_key=a39779a38e0619f8ae58b09f64522597&query=";
  5. private $bannerURL = "https://image.tmdb.org/t/p/original";
  6. private $outputText = "";
  7. public function __construct() {
  8. }
  9. public function getOutputText() {
  10. return $this->outputText;
  11. }
  12. public static function getMovieNameFromFilename($filename) {
  13. preg_match("/([a-zA-Z0-9äöüÄÖÜ.]+).([0-9][0-9][0-9][0-9]).([a-z0-9]+)/", $filename, $output);
  14. return str_replace(".", " ", $output[1]);
  15. }
  16. public static function sortByPopularity($movieA, $movieB) {
  17. if($movieA['popularity'] == $movieB['popularity']) {
  18. return 0;
  19. }
  20. return ($movieA['popularity'] < $movieB['popularity']) ? 1 : -1;
  21. }
  22. public static function remove($movieID) {
  23. $movie = $GLOBALS['db']->getAllAssoc("movies", "id", $movieID);
  24. unlink("img/posters/" . $movie[0]['poster']);
  25. unlink("img/posters/" . $movie[0]['backdrop']);
  26. $GLOBALS['db']->deleteRows("movies", "id", $movieID);
  27. header("Location: " . $GLOBALS['conf']['baseURL'] . "?view=admin");
  28. }
  29. public static function update($movieID) {
  30. $movie = $GLOBALS['db']->getAllAssoc("movies", "id", $movieID);
  31. self::remove($movieID);
  32. $this->downloadMovieByID($movie['moviedb-id'], $movie['path'], $movie['source']);
  33. header("Location: " . $GLOBALS['conf']['baseURL'] . "?view=admin");
  34. }
  35. public function scrapeFolder($sourceID) {
  36. if(!file_exists("img/posters")) {
  37. mkdir("img/posters");
  38. }
  39. $source = $GLOBALS['db']->getAllAssoc("sources", "id", $sourceID);
  40. $fileList = scandir($source[0]['path']);
  41. $fileList = array_diff($fileList, array('.', '..', 'formatting.txt', '.Trash-1000'));
  42. foreach($fileList as $file) {
  43. $this->outputText .= "<b>" . self::getMovieNameFromFilename($file) . "</b><br>" . PHP_EOL;
  44. if($GLOBALS['db']->countRows("movies", "path", $file) > 0) {
  45. $this->outputText .= "Exists, skipping..<br><br>" . PHP_EOL;
  46. continue;
  47. }
  48. $movie = json_decode(curl_download($this->apiURL . urlencode(self::getMovieNameFromFilename($file)) . "&language=de&include_image_language=de"), true);
  49. if($movie['total_results'] == 1) {
  50. $this->outputText .= "Found 1 movie, downloading...<br>" . PHP_EOL;
  51. $this->downloadMovieByID($movie['results'][0]['id'], $file, $sourceID);
  52. } else if($movie['total_results'] < 1) {
  53. $this->outputText .= "<span style=\"color: red;\">Not found!!</span><br>" . PHP_EOL;
  54. } else { // multiple search results
  55. usort($movie['results'], array("MovieScraper", "sortByPopularity")); // sort results by popularity, so that you don't have to scroll like 500000x
  56. foreach($movie['results'] as $result) {
  57. $this->outputText .= "<pre>" . print_r($result, true) . "</pre>" . PHP_EOL;
  58. $this->outputText .= "<a href=\"?view=scrape&action=scrapeSingleMovie&moviedbID=" . $result['id'] . "&path=" . urlencode($file) . "&sourceID=" . $sourceID . "\" target=\"_blank\">Load</a><br /><br />" . PHP_EOL;
  59. }
  60. }
  61. $this->outputText .= "<br>" . PHP_EOL;
  62. /*
  63. Array
  64. (
  65. [page] => 1
  66. [results] => Array
  67. (
  68. [0] => Array
  69. (
  70. [poster_path] => /hDlezfMSw8oXIFNZ7B9fFLrV8kd.jpg
  71. [popularity] => 1.006821
  72. [id] => 15826
  73. [backdrop_path] => /meQN6iuOulLwOV8LNO6S9z3bJBY.jpg
  74. [vote_average] => 7
  75. [overview] => 1000 Ways to Die is an anthology television series that [...]
  76. [first_air_date] => 2009-02-04
  77. [origin_country] => Array
  78. (
  79. [0] => US
  80. )
  81. [genre_ids] => Array
  82. (
  83. [0] => 99
  84. )
  85. [original_language] => en
  86. [vote_count] => 1
  87. [name] => 1000 Ways to Die
  88. [original_name] => 1000 Ways to Die
  89. )
  90. )
  91. [total_results] => 1
  92. [total_pages] => 1
  93. )
  94. */
  95. }
  96. }
  97. public function downloadMovieByID($movieID, $path, $sourceID) {
  98. $movie = json_decode(curl_download("http://api.themoviedb.org/3/movie/" . $movieID . "?api_key=a39779a38e0619f8ae58b09f64522597&language=de&include_image_language=de"), true);
  99. $cols = array(
  100. "moviedb-id",
  101. "name",
  102. "path",
  103. "poster",
  104. "backdrop",
  105. "overview",
  106. "source"
  107. );
  108. $vals = array(
  109. $movie['id'],
  110. $movie['title'],
  111. $path,
  112. ltrim($movie['poster_path'], "/"),
  113. ltrim($movie['backdrop_path'], "/"),
  114. $movie['overview'],
  115. $sourceID
  116. );
  117. // Download poster, backdrop
  118. if(!file_exists("img/posters" . $movie['poster_path'])) {
  119. file_put_contents("img/posters" . $movie['poster_path'], fopen($this->bannerURL . $movie['poster_path'], 'r'));
  120. }
  121. if(!file_exists("img/posters" . $movie['backdrop_path'])) {
  122. file_put_contents("img/posters" . $movie['backdrop_path'], fopen($this->bannerURL . $movie['backdrop_path'], 'r'));
  123. }
  124. $GLOBALS['db']->insertRow("movies", $cols, $vals);
  125. $this->outputText .= "Done.";
  126. }
  127. }