| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- ignore_user_abort(true);
- class MovieScraper {
- private $apiURL = "http://api.themoviedb.org/3/search/movie?api_key=a39779a38e0619f8ae58b09f64522597&query=";
- private $bannerURL = "https://image.tmdb.org/t/p/original";
- private $outputText = "";
- public function __construct() {
- }
- public function getOutputText() {
- return $this->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 static function remove($movieID) {
- $movie = $GLOBALS['db']->getAllAssoc("movies", "id", $movieID);
- unlink("img/posters/" . $movie[0]['poster']);
- unlink("img/posters/" . $movie[0]['backdrop']);
- $GLOBALS['db']->deleteRows("movies", "id", $movieID);
- header("Location: " . $GLOBALS['conf']['baseURL'] . "?view=admin");
- }
- public static function update($movieID) {
- $movie = $GLOBALS['db']->getAllAssoc("movies", "id", $movieID);
- self::remove($movieID);
- $this->downloadMovieByID($movie['moviedb-id'], $movie['path'], $movie['source']);
- header("Location: " . $GLOBALS['conf']['baseURL'] . "?view=admin");
- }
- 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 .= "<b>" . self::getMovieNameFromFilename($file) . "</b><br>" . PHP_EOL;
- if($GLOBALS['db']->countRows("movies", "path", $file) > 0) {
- $this->outputText .= "Exists, skipping..<br><br>" . 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...<br>" . PHP_EOL;
- $this->downloadMovieByID($movie['results'][0]['id'], $file, $sourceID);
- } else if($movie['total_results'] < 1) {
- $this->outputText .= "<span style=\"color: red;\">Not found!!</span><br>" . 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 .= "<pre>" . print_r($result, true) . "</pre>" . PHP_EOL;
- $this->outputText .= "<a href=\"?view=scrape&action=scrapeSingleMovie&moviedbID=" . $result['id'] . "&path=" . urlencode($file) . "&sourceID=" . $sourceID . "\" target=\"_blank\">Load</a><br /><br />" . PHP_EOL;
- }
- }
- $this->outputText .= "<br>" . 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,
- ltrim($movie['poster_path'], "/"),
- ltrim($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.";
- }
- }
|