outputText; } public static function getMovieNameFromFilename($filename) { preg_match("/([a-zA-Z0-9äöüÄÖÜ.]+).([0-9][0-9][0-9][0-9]).([a-z0-9]+)/", $filename, $output); return str_replace(".", " ", $output[1]); } public static function sortByPopularity($movieA, $movieB) { if($movieA['popularity'] == $movieB['popularity']) { return 0; } return ($movieA['popularity'] < $movieB['popularity']) ? 1 : -1; } public function scrapeFolder($sourceID) { if(!file_exists("img/posters")) { mkdir("img/posters"); } $source = $GLOBALS['db']->getAllAssoc("sources", "id", $sourceID); $fileList = scandir($source[0]['path']); $fileList = array_diff($fileList, array('.', '..', 'formatting.txt', '.Trash-1000')); foreach($fileList as $file) { $this->outputText .= "" . self::getMovieNameFromFilename($file) . "
" . PHP_EOL; if($GLOBALS['db']->countRows("movies", "path", $file) > 0) { $this->outputText .= "Exists, skipping..

" . PHP_EOL; continue; } $movie = json_decode(curl_download($this->apiURL . urlencode(self::getMovieNameFromFilename($file)) . "&language=de&include_image_language=de"), true); if($movie['total_results'] == 1) { $this->outputText .= "Found 1 movie, downloading...
" . PHP_EOL; $this->downloadMovieByID($movie['results'][0]['id'], $file, $sourceID); } else if($movie['total_results'] < 1) { $this->outputText .= "Not found!!
" . PHP_EOL; } else { // multiple search results usort($movie['results'], array("MovieScraper", "sortByPopularity")); // sort results by popularity, so that you don't have to scroll like 500000x foreach($movie['results'] as $result) { $this->outputText .= "
" . print_r($result, true) . "
" . PHP_EOL; $this->outputText .= "Load
" . PHP_EOL; } } $this->outputText .= "
" . PHP_EOL; /* Array ( [page] => 1 [results] => Array ( [0] => Array ( [poster_path] => /hDlezfMSw8oXIFNZ7B9fFLrV8kd.jpg [popularity] => 1.006821 [id] => 15826 [backdrop_path] => /meQN6iuOulLwOV8LNO6S9z3bJBY.jpg [vote_average] => 7 [overview] => 1000 Ways to Die is an anthology television series that [...] [first_air_date] => 2009-02-04 [origin_country] => Array ( [0] => US ) [genre_ids] => Array ( [0] => 99 ) [original_language] => en [vote_count] => 1 [name] => 1000 Ways to Die [original_name] => 1000 Ways to Die ) ) [total_results] => 1 [total_pages] => 1 ) */ } } public function downloadMovieByID($movieID, $path, $sourceID) { $movie = json_decode(curl_download("http://api.themoviedb.org/3/movie/" . $movieID . "?api_key=a39779a38e0619f8ae58b09f64522597&language=de&include_image_language=de"), true); $cols = array( "moviedb-id", "name", "path", "poster", "backdrop", "overview", "source" ); $vals = array( $movie['id'], $movie['title'], $path, $movie['poster_path'], $movie['backdrop_path'], $movie['overview'], $sourceID ); // Download poster, backdrop if(!file_exists("img/posters" . $movie['poster_path'])) { file_put_contents("img/posters" . $movie['poster_path'], fopen($this->bannerURL . $movie['poster_path'], 'r')); } if(!file_exists("img/posters" . $movie['backdrop_path'])) { file_put_contents("img/posters" . $movie['backdrop_path'], fopen($this->bannerURL . $movie['backdrop_path'], 'r')); } $GLOBALS['db']->insertRow("movies", $cols, $vals); $this->outputText .= "Done."; } }