Ver Fonte

seriesScraper now updating data of cached series/seasons/episodes

Moritz Schmidt há 9 anos atrás
pai
commit
44059fec07
3 ficheiros alterados com 67 adições e 44 exclusões
  1. 6 0
      inc/controller.php
  2. 4 2
      inc/database.php
  3. 57 42
      inc/seriesScraper.php

+ 6 - 0
inc/controller.php

@@ -18,6 +18,8 @@ class Controller {
 		$GLOBALS['db'] = new Database($GLOBALS['conf']);
 
 		if(!empty($request['action']) && $request['action'] === "scrape") {
+			ignore_user_abort(true);
+			set_time_limit(0);
 			$source = $GLOBALS['db']->getAllAssoc("sources", "id", $request['sourceID']);
 
 			if($source[0]['type'] == 0) { // Series
@@ -30,11 +32,15 @@ class Controller {
 		}
 
 		if(!empty($request['action']) && $request['action'] === "scrapeSingleTV") {
+			ignore_user_abort(true);
+			set_time_limit(0);
 			$this->scraper = new SeriesScraper();
 			$this->scraper->downloadSeriesByID($request['moviedbID'], urldecode($request['path']), $request['sourceID']);
 		}
 
 		if(!empty($request['action']) && $request['action'] === "scrapeSingleMovie") {
+			ignore_user_abort(true);
+			set_time_limit(0);
 			$this->scraper = new MovieScraper();
 			$this->scraper->downloadMovieByID($request['moviedbID'], urldecode($request['path']), $request['sourceID']);
 		}

+ 4 - 2
inc/database.php

@@ -44,6 +44,7 @@ class Database {
       trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
       exit(1);
     } else if($res->num_rows < 1) {
+      error_log($query);
       error_log("This shouldn't happen..2");
       //exit(1);
     }
@@ -165,7 +166,7 @@ class Database {
     $res = $this->handle->query($query);
     if($res === false) {
       echo "DB failed.";
-      trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
+      trigger_error('Database failed: '  . $this->handle->error, E_USER_ERROR);
       exit(1);
     } else {
       return true;
@@ -178,7 +179,8 @@ class Database {
     $res = $this->handle->query($query);
     if($res === false) {
       echo "DB failed.";
-      trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
+      trigger_error('Database failed: '  . $this->handle->error, E_USER_ERROR);
+      error_log("Query: " . $query);
       exit(1);
     } else {
       return true;

+ 57 - 42
inc/seriesScraper.php

@@ -69,7 +69,8 @@ class SeriesScraper {
       $this->outputText .= "<b>" . $dir . "</b><br>" . PHP_EOL;
 
       if($GLOBALS['db']->countRows("series", "path", $dir) > 0) {
-        $this->outputText .= "Exists, skipping..<br><br>" . PHP_EOL;
+        $this->outputText .= "Exists, updating..<br><br>" . PHP_EOL;
+        self::updateByID($GLOBALS['db']->getString("id", "series", "path", $dir));
         continue;
       }
 
@@ -82,7 +83,7 @@ class SeriesScraper {
       } else if($series['total_results'] < 1) {
         $this->outputText .= "<span style=\"color: red;\">Not found!!</span><br>" . PHP_EOL;
       } else { // multiple search results
-        usort($series['results'], array("MovieScraper", "sortByPopularity")); // sort results by popularity, so that you don't have to scroll like 500000x
+        usort($series['results'], array("MovieScraper", "sortByPopularity")); // sort results by popularity, so that you don't have to scroll 500000x
         foreach($series['results'] as $result) {
           $this->outputText .= "<pre>" . print_r($result, true) . "</pre>" . PHP_EOL;
           $this->outputText .= "<a href=\"?view=scrape&action=scrapeSingleTV&moviedbID=" . $result['id'] . "&path=" . urlencode($dir) . "&sourceID=" . $sourceID . "\" target=\"_blank\">Load</a><br /><br />" . PHP_EOL; // TODO: this
@@ -90,46 +91,6 @@ class SeriesScraper {
       }
 
       $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
-        )
-
-        */
     }
 
   }
@@ -237,4 +198,58 @@ class SeriesScraper {
 
     $this->outputText .= "Done.";
   }
+
+  public function updateByID($seriesID) {
+    /*if($seriesID != 208) {
+      return;
+    }*/
+
+    error_log($seriesID);
+
+    $series   = $GLOBALS['db']->getAllAssoc("series", "id", $seriesID);
+    $seriesDB = json_decode(curl_download("http://api.themoviedb.org/3/tv/" . $series[0]['moviedb-id'] . "?api_key=a39779a38e0619f8ae58b09f64522597&language=de&include_image_language=de"), true);
+
+    if($series[0]['poster'] != ltrim($seriesDB['poster_path'], "/")) { // poster
+      error_log("New Poster, old: " . $series[0]['poster'] . " new: " . ltrim($seriesDB['poster_path'], "/"));
+      unlink("img/posters/" . $series[0]['poster']);
+      file_put_contents("img/posters" . $seriesDB['poster_path'], fopen($this->bannerURL . $seriesDB['poster_path'], 'r'));
+      $GLOBALS['db']->updateRow("series", "poster", '"' . ltrim($seriesDB['poster_path'], "/") . '"', "id", $seriesID);
+    }
+
+    if($series[0]['backdrop'] != ltrim($seriesDB['backdrop_path'], "/")) { // backdrop
+      error_log("New Backdrop, old: " . $series[0]['backdrop'] . " new: " . ltrim($seriesDB['backdrop_path'], "/"));
+      unlink("img/posters/" . $series[0]['backdrop']);
+      file_put_contents("img/posters" . $seriesDB['backdrop_path'], fopen($this->bannerURL . $seriesDB['backdrop_path'], 'r'));
+      $GLOBALS['db']->updateRow("series", "backdrop", '"' . ltrim($seriesDB['backdrop_path'], "/") . '"', "id", $seriesID);
+    }
+
+    $seasons = $GLOBALS['db']->getAllAssoc("series-seasons", "series-id", $seriesID);
+
+    foreach($seasons as $season) { // TODO: check for new seasons
+      $seasonDB = array_search(ltrim($season['number'], 0), array_column($seriesDB['seasons'], 'season_number'));
+      $seasonDB = $seriesDB['seasons'][$seasonDB];
+
+      if($season['poster'] != ltrim($seasonDB['poster_path'], "/")) {
+        error_log("New Season-Poster, old: " . $season['poster'] . " new: " . ltrim($seasonDB['poster_path'], "/"));
+        unlink("img/posters/" . $season['poster']);
+        file_put_contents("img/posters" . $seasonDB['poster_path'], fopen($this->bannerURL . $seasonDB['poster_path'], 'r'));
+        $GLOBALS['db']->updateRow("series-seasons", "poster", '"' . ltrim($seasonDB['poster_path'], "/") . '"', "id", $season['id']);
+      }
+
+      $episodesDB = json_decode(curl_download("http://api.themoviedb.org/3/tv/" . $series[0]['moviedb-id'] . "/season/" . $season['number'] . "?api_key=a39779a38e0619f8ae58b09f64522597&language=de&include_image_language=de"), true);
+
+      $episodes = $GLOBALS['db']->getAllAssoc("series-episodes", "season-id", $season['id']);
+      foreach($episodes as $episode) {
+        $episodeDB = array_search($episode['number'], array_column($episodesDB['episodes'], 'episode_number'));
+        $episodeDB = $episodesDB['episodes'][$episodeDB];
+
+        if($episode['thumb'] != ltrim($episodeDB['still_path'], "/")) {
+          error_log("New Episode-Poster, old: " . $episode['thumb'] . " new: " . ltrim($episodeDB['still_path'], "/"));
+          unlink("img/posters/" . $episode['thumb']);
+          file_put_contents("img/posters" . $episodeDB['still_path'], fopen($this->bannerURL . $episodeDB['still_path'], 'r'));
+          $GLOBALS['db']->updateRow("series-episodes", "thumb", '"' . ltrim($episodeDB['still_path'], "/") . '"', "id", $episode['id']);
+        }
+      }
+    }
+  }
 }