| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- ignore_user_abort(true);
- class SeriesScraper {
- private $apiURL = "http://api.themoviedb.org/3/search/tv?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 getEpisodeNumberByFilename($filename) {
- preg_match("/([a-zA-Z0-9äöüÄÖÜß\-\.\,\w]+)-[S|s]([0-9]+)[E|e]([0-9]+)-([a-zA-Z0-9äöüÄÖÜß\-\.\,\w\:]+)\.([a-zA-Z0-9\(\).*\w\,]+)/", $filename, $output);
- return ltrim($output[3], 0);
- }
- public static function remove($seriesID) {
- $series = Model::getSeriesBySeriesID($seriesID);
- $seasons = Model::getSeasonsBySeriesID($seriesID);
- foreach($seasons as $season) {
- $GLOBALS['db']->deleteRows("series-episodes", "season-id", $season['id']);
- }
- $GLOBALS['db']->deleteRows("series-seasons", "series-id", $seriesID);
- $GLOBALS['db']->deleteRows("series", "id", $seriesID);
- // TODO: Remove files
- }
- public function update($seriesID) {
- $series = Model::getSeriesBySeriesID($seriesID);
- self::remove($seriesID);
- $this->downloadSeriesByID($series[0]['moviedb-id'], $series[0]['path'], $series[0]['source']);
- }
- public function scrapeFolder($sourceID) {
- if(!file_exists("img/posters")) {
- mkdir("img/posters");
- }
- $source = $GLOBALS['db']->getAllAssoc("sources", "id", $sourceID);
- $dirList = scandir($source[0]['path']);
- $dirList = array_diff($dirList, array('.', '..', 'formatting.txt', '.Trash-1000'));
- foreach($dirList as $dir) {
- $this->outputText .= "<b>" . $dir . "</b><br>" . PHP_EOL;
- if($GLOBALS['db']->countRows("series", "path", $dir) > 0) {
- $this->outputText .= "Exists, skipping..<br><br>" . PHP_EOL;
- continue;
- }
- $series = json_decode(curl_download($this->apiURL . urlencode($dir) . "&language=de&include_image_language=de"), true);
- if($series['total_results'] == 1) {
- $this->outputText .= "Found 1 series, downloading...<br>" . PHP_EOL;
- $this->downloadSeriesByID($series['results'][0]['id'], $dir, $sourceID);
- } 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
- 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>" . PHP_EOL; // TODO: this
- }
- }
- $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 downloadSeriesByID($seriesID, $path, $sourceID) {
- $source = $GLOBALS['db']->getAllAssoc("sources", "id", $sourceID);
- $series = json_decode(curl_download("http://api.themoviedb.org/3/tv/" . $seriesID . "?api_key=a39779a38e0619f8ae58b09f64522597&language=de&include_image_language=de"), true);
- //$this->outputText .= "<pre>" . print_r($series, true) . "</pre>";
- // Download poster, backdrop
- if(!file_exists("img/posters" . $series['poster_path'])) {
- file_put_contents("img/posters" . $series['poster_path'], fopen($this->bannerURL . $series['poster_path'], 'r'));
- }
- if(!file_exists("img/posters" . $series['backdrop_path'])) {
- file_put_contents("img/posters" . $series['backdrop_path'], fopen($this->bannerURL . $series['backdrop_path'], 'r'));
- }
- $cols = array(
- "moviedb-id",
- "name",
- "path",
- "poster",
- "backdrop",
- "overview",
- "source"
- );
- $vals = array(
- $series['id'],
- $series['name'],
- $path,
- $series['poster_path'],
- $series['backdrop_path'],
- $series['overview'],
- $sourceID
- );
- $dbSeriesID = $GLOBALS['db']->insertRow("series", $cols, $vals);
- $localSeasons = scandir($source[0]['path'] . "/" . $path);
- $localSeasons = array_diff($localSeasons, array('.', '..', 'formatting.txt', '.Trash-1000'));
- foreach($localSeasons as $localSeason) {
- $season = array_search(ltrim(ltrim($localSeason, "S"), 0), array_column($series['seasons'], 'season_number'));
- $cols = array(
- "series-id",
- "number",
- "poster"
- );
- $vals = array(
- $dbSeriesID,
- $series['seasons'][$season]['season_number'],
- $series['seasons'][$season]['poster_path']
- );
- $dbSeasonID = $GLOBALS['db'] -> insertRow("series-seasons", $cols, $vals);
- if(!file_exists("img/posters" . $series['seasons'][$season]['poster_path'])) {
- file_put_contents("img/posters" . $series['seasons'][$season]['poster_path'], fopen($this->bannerURL . $series['seasons'][$season]['poster_path'], 'r'));
- }
- $localEpisodes = scandir($source[0]['path'] . "/" . $path . "/" . $localSeason);
- $localEpisodes = array_diff($localEpisodes, array('.', '..', 'formatting.txt', '.Trash-1000'));
- $dbEpisodes = get_object_vars(json_decode(curl_download("http://api.themoviedb.org/3/tv/" . $seriesID . "/season/" . ltrim(ltrim($localSeason, "S"), 0) . "?api_key=a39779a38e0619f8ae58b09f64522597&language=de&include_image_language=de")));
- $episodes = array();
- foreach($dbEpisodes['episodes'] as $episode) {
- $episodes[] = get_object_vars($episode);
- }
- foreach($localEpisodes as $localEpisode) {
- $episode = array_search($this::getEpisodeNumberByFilename($localEpisode), array_column($episodes, 'episode_number'));
- $cols = array(
- "season-id",
- "number",
- "name",
- "thumb",
- "path"
- );
- $vals = array(
- $dbSeasonID,
- $episodes[$episode]['episode_number'],
- $episodes[$episode]['name'],
- $episodes[$episode]['still_path'],
- $localEpisode
- );
- $GLOBALS['db'] -> insertRow("series-episodes", $cols, $vals);
- if(!file_exists("img/posters" . $episodes[$episode]['still_path'])) {
- file_put_contents("img/posters" . $episodes[$episode]['still_path'], fopen($this->bannerURL . $episodes[$episode]['still_path'], 'r'));
- }
- }
- }
- $this->outputText .= "Done.";
- }
- }
|