movieScraper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 function scrapeFolder($sourceID) {
  23. if(!file_exists("img/posters")) {
  24. mkdir("img/posters");
  25. }
  26. $source = $GLOBALS['db']->getAllAssoc("sources", "id", $sourceID);
  27. $fileList = scandir($source[0]['path']);
  28. $fileList = array_diff($fileList, array('.', '..', 'formatting.txt', '.Trash-1000'));
  29. foreach($fileList as $file) {
  30. $this->outputText .= "<b>" . self::getMovieNameFromFilename($file) . "</b><br>" . PHP_EOL;
  31. if($GLOBALS['db']->countRows("movies", "path", $file) > 0) {
  32. $this->outputText .= "Exists, skipping..<br><br>" . PHP_EOL;
  33. continue;
  34. }
  35. $movie = json_decode(curl_download($this->apiURL . urlencode(self::getMovieNameFromFilename($file)) . "&language=de&include_image_language=de"), true);
  36. if($movie['total_results'] == 1) {
  37. $this->outputText .= "Found 1 movie, downloading...<br>" . PHP_EOL;
  38. $this->downloadMovieByID($movie['results'][0]['id'], $file, $sourceID);
  39. } else if($movie['total_results'] < 1) {
  40. $this->outputText .= "<span style=\"color: red;\">Not found!!</span><br>" . PHP_EOL;
  41. } else { // multiple search results
  42. usort($movie['results'], array("MovieScraper", "sortByPopularity")); // sort results by popularity, so that you don't have to scroll like 500000x
  43. foreach($movie['results'] as $result) {
  44. $this->outputText .= "<pre>" . print_r($result, true) . "</pre>" . PHP_EOL;
  45. $this->outputText .= "<a href=\"?view=scrape&action=scrapeSingleMovie&moviedbID=" . $result['id'] . "&path=" . urlencode($file) . "&sourceID=" . $sourceID . "\" target=\"_blank\">Load</a><br>" . PHP_EOL;
  46. }
  47. }
  48. $this->outputText .= "<br>" . PHP_EOL;
  49. /*
  50. Array
  51. (
  52. [page] => 1
  53. [results] => Array
  54. (
  55. [0] => Array
  56. (
  57. [poster_path] => /hDlezfMSw8oXIFNZ7B9fFLrV8kd.jpg
  58. [popularity] => 1.006821
  59. [id] => 15826
  60. [backdrop_path] => /meQN6iuOulLwOV8LNO6S9z3bJBY.jpg
  61. [vote_average] => 7
  62. [overview] => 1000 Ways to Die is an anthology television series that [...]
  63. [first_air_date] => 2009-02-04
  64. [origin_country] => Array
  65. (
  66. [0] => US
  67. )
  68. [genre_ids] => Array
  69. (
  70. [0] => 99
  71. )
  72. [original_language] => en
  73. [vote_count] => 1
  74. [name] => 1000 Ways to Die
  75. [original_name] => 1000 Ways to Die
  76. )
  77. )
  78. [total_results] => 1
  79. [total_pages] => 1
  80. )
  81. */
  82. }
  83. }
  84. public function downloadMovieByID($movieID, $path, $sourceID) {
  85. $movie = json_decode(curl_download("http://api.themoviedb.org/3/movie/" . $movieID . "?api_key=a39779a38e0619f8ae58b09f64522597&language=de&include_image_language=de"), true);
  86. $cols = array(
  87. "moviedb-id",
  88. "name",
  89. "path",
  90. "poster",
  91. "backdrop",
  92. "overview",
  93. "source"
  94. );
  95. $vals = array(
  96. $movie['id'],
  97. $movie['title'],
  98. $path,
  99. $movie['poster_path'],
  100. $movie['backdrop_path'],
  101. $movie['overview'],
  102. $sourceID
  103. );
  104. // Download poster, backdrop
  105. if(!file_exists("img/posters" . $movie['poster_path'])) {
  106. file_put_contents("img/posters" . $movie['poster_path'], fopen($this->bannerURL . $movie['poster_path'], 'r'));
  107. }
  108. if(!file_exists("img/posters" . $movie['backdrop_path'])) {
  109. file_put_contents("img/posters" . $movie['backdrop_path'], fopen($this->bannerURL . $movie['backdrop_path'], 'r'));
  110. }
  111. $GLOBALS['db']->insertRow("movies", $cols, $vals);
  112. $this->outputText .= "Done.";
  113. }
  114. }