From 2aababe10ff90c27d12db38ab481136c02abaf04 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Fri, 7 Aug 2015 22:14:15 +0200 Subject: [PATCH] Pull more info from Trakt for movies, change trailers for traileraddict. --- newznab/controllers/Film.php | 260 +++++++++++------- newznab/utility/Utility.php | 7 +- resources/db/patches/0170~movieinfo.sql | 1 + resources/db/patches/0171~settings.sql | 23 ++ www/admin/movie-edit.php | 15 +- www/pages/details.php | 41 ++- .../views/admin/site-edit.tpl | 28 +- 7 files changed, 249 insertions(+), 126 deletions(-) create mode 100644 resources/db/patches/0170~movieinfo.sql create mode 100644 resources/db/patches/0171~settings.sql diff --git a/newznab/controllers/Film.php b/newznab/controllers/Film.php index 046315abe..5859d8f02 100644 --- a/newznab/controllers/Film.php +++ b/newznab/controllers/Film.php @@ -399,6 +399,99 @@ class Film return $browseBy; } + /** + * @var null|TraktTv + */ + public $traktTv = null; + + /** + * Get trailer using IMDB Id. + * + * @param int $imdbID + * + * @return bool|string + */ + public function getTrailer($imdbID) + { + if (!is_numeric($imdbID)) { + return false; + } + + $trailer = $this->pdo->queryOneRow("SELECT trailer FROM movieinfo WHERE imdbid = 'tt$imdbID'"); + if ($trailer) { + return $trailer['trailer']; + } + + if (is_null($this->traktTv)) { + $this->traktTv = new TraktTv(['Settings' => $this->pdo]); + } + + $data = $this->traktTv->movieSummary('tt' . $imdbID, 'full'); + if ($data) { + $trailer = false; + if (isset($data['trailer']) && !empty($data['trailer'])) { + $data['trailer'] = $trailer = str_ireplace( + 'http://', 'https://', str_ireplace('watch?v=', 'embed/', $data['trailer']) + ); + } + $this->parseTraktTv($data); + if ($trailer) { + return $trailer; + } + } + + $trailer = Utility::imdb_trailers($imdbID); + if ($trailer) { + $this->pdo->queryExec( + 'UPDATE movieinfo SET trailer = ' . $this->pdo->escapeString($trailer) . ' WHERE imdbid = ' . $imdbID + ); + return $trailer; + } + return false; + } + + /** + * Parse trakt info, insert into DB. + * + * @param array $data + */ + public function parseTraktTv($data) + { + $this->update([ + 'genres' => $this->checkTraktValue($data['genres']), + 'imdbid' => $this->checkTraktValue(str_ireplace('tt', '', $data['ids']['imdb'])), + 'language' => $this->checkTraktValue($data['language']), + 'plot' => $this->checkTraktValue($data['overview']), + 'rating' => $this->checkTraktValue($data['rating']), + 'tagline' => $this->checkTraktValue($data['tagline']), + 'title' => $this->checkTraktValue($data['title']), + 'tmdbid' => $this->checkTraktValue($data['ids']['tmdb']), + 'trailer' => $this->checkTraktValue($data['trailer']), + 'year' => $this->checkTraktValue($data['year']) + ]); + } + + /** + * Checks if the value is set and not empty, returns it, else empty string. + * + * @param mixed $value + * + * @return string + */ + private function checkTraktValue($value) + { + if (is_array($value)) { + $temp = ''; + foreach($value as $val) { + if (!is_array($val) && !is_object($val)) { + $temp .= (string)$val; + } + } + $value = $temp; + } + return (isset($value) && !empty($value) ? $value : ''); + } + /** * Create click-able links to IMDB actors/genres/directors/etc.. * @@ -429,49 +522,57 @@ class Film return implode(', ', $newArr); } + /** + * Get array of column keys, for inserting / updating. + * @return array + */ + public function getColumnKeys() + { + return [ + 'actors','backdrop','cover','director','genre','imdbid','language', + 'plot','rating','tagline','title','tmdbid', 'trailer','type','year' + ]; + } + /** * Update movie on movie-edit page. * - * @param $id - * @param $title - * @param $tagLine - * @param $plot - * @param $year - * @param $rating - * @param $genre - * @param $director - * @param $actors - * @param $language - * @param $cover - * @param $backdrop + * @param array $values Array of keys/values to update. See $validKeys + * @return int|bool */ - public function update( - $id = '', $title = '', $tagLine = '', $plot = '', $year = '', $rating = '', $genre = '', $director = '', - $actors = '', $language = '', $cover = '', $backdrop = '' - ) - { - if (!empty($id)) { - - $this->pdo->queryExec( - sprintf(" - UPDATE movieinfo - SET %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %d, updateddate = NOW() - WHERE imdbid = %d", - (empty($title) ? '' : 'title = ' . $this->pdo->escapeString($title)), - (empty($tagLine) ? '' : 'tagline = ' . $this->pdo->escapeString($tagLine)), - (empty($plot) ? '' : 'plot = ' . $this->pdo->escapeString($plot)), - (empty($year) ? '' : 'year = ' . $this->pdo->escapeString($year)), - (empty($rating) ? '' : 'rating = ' . $this->pdo->escapeString($rating)), - (empty($genre) ? '' : 'genre = ' . $this->pdo->escapeString($genre)), - (empty($director) ? '' : 'director = ' . $this->pdo->escapeString($director)), - (empty($actors) ? '' : 'actors = ' . $this->pdo->escapeString($actors)), - (empty($language) ? '' : 'language = ' . $this->pdo->escapeString($language)), - (empty($cover) ? '' : 'cover = ' . $cover), - (empty($backdrop) ? '' : 'backdrop = ' . $backdrop), - $id - ) - ); + public function update(array $values) { + if (!count($values)) { + return false; } + + $validKeys = $this->getColumnKeys(); + + $query = [ + '0' => 'INSERT INTO movieinfo (updateddate, createddate, ', + '1' => ' VALUES (NOW(), NOW(), ', + '2' => 'ON DUPLICATE KEY UPDATE updateddate = NOW(), ' + ]; + $found = 0; + foreach ($values as $key => $value) { + if (in_array($key, $validKeys) && !empty($value)) { + $found++; + $query[0] .= "$key, "; + if (in_array($key, ['genre', 'language'])) { + $value = substr($value, 0, 64); + } + $value = $this->pdo->escapeString($value); + $query[1] .= "$value, "; + $query[2] .= "$key = $value, "; + } + } + if (!$found) { + return false; + } + foreach ($query as $key => $value) { + $query[$key] = rtrim($value, ', '); + } + + return $this->pdo->queryInsert($query[0] . ') ' . $query[1] . ') ' . $query[2]); } /** @@ -593,58 +694,24 @@ class Film } $mov['title'] = html_entity_decode($mov['title'] , ENT_QUOTES, 'UTF-8'); - $mov['plot'] = html_entity_decode(preg_replace('/\s+See full summary »/', ' ', $mov['plot']), ENT_QUOTES, 'UTF-8'); - $mov['tagline'] = html_entity_decode($mov['tagline'] , ENT_QUOTES, 'UTF-8'); - $mov['genre'] = html_entity_decode($mov['genre'] , ENT_QUOTES, 'UTF-8'); - $mov['director'] = html_entity_decode($mov['director'], ENT_QUOTES, 'UTF-8'); - $mov['actors'] = html_entity_decode($mov['actors'] , ENT_QUOTES, 'UTF-8'); - $mov['language'] = html_entity_decode($mov['language'], ENT_QUOTES, 'UTF-8'); - - $mov['type'] = html_entity_decode(ucwords(preg_replace('/[\.\_]/', ' ', $mov['type'])), ENT_QUOTES, 'UTF-8'); $mov['title'] = str_replace(array('/', '\\'), '', $mov['title']); - $movieID = $this->pdo->queryInsert( - sprintf(" - INSERT INTO movieinfo - (imdbid, tmdbID, title, rating, tagline, plot, year, genre, type, - director, actors, language, cover, backdrop, banner, createddate, updateddate) - VALUES - (%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %d, %d, NOW(), NOW()) - ON DUPLICATE KEY UPDATE - imdbid = %d, tmdbID = %s, title = %s, rating = %s, tagline = %s, plot = %s, year = %s, genre = %s, - type = %s, director = %s, actors = %s, language = %s, cover = %d, backdrop = %d, banner = %d, updateddate = NOW()", - $mov['imdbid'], - $mov['tmdbid'], - $this->pdo->escapeString($mov['title']), - $this->pdo->escapeString($mov['rating']), - $this->pdo->escapeString($mov['tagline']), - $this->pdo->escapeString($mov['plot']), - $this->pdo->escapeString($mov['year']), - $this->pdo->escapeString(substr($mov['genre'], 0, 64)), - $this->pdo->escapeString($mov['type']), - $this->pdo->escapeString($mov['director']), - $this->pdo->escapeString($mov['actors']), - $this->pdo->escapeString(substr($mov['language'], 0, 64)), - $mov['cover'], - $mov['backdrop'], - $mov['banner'], - $mov['imdbid'], - $mov['tmdbid'], - $this->pdo->escapeString($mov['title']), - $this->pdo->escapeString($mov['rating']), - $this->pdo->escapeString($mov['tagline']), - $this->pdo->escapeString($mov['plot']), - $this->pdo->escapeString($mov['year']), - $this->pdo->escapeString(substr($mov['genre'], 0, 64)), - $this->pdo->escapeString($mov['type']), - $this->pdo->escapeString($mov['director']), - $this->pdo->escapeString($mov['actors']), - $this->pdo->escapeString(substr($mov['language'], 0, 64)), - $mov['cover'], - $mov['backdrop'], - $mov['banner'] - ) - ); + $movieID = $this->update([ + 'actors' => html_entity_decode($mov['actors'] , ENT_QUOTES, 'UTF-8'), + 'backdrop' => $mov['backdrop'], + 'cover' => $mov['cover'], + 'director' => html_entity_decode($mov['director'], ENT_QUOTES, 'UTF-8'), + 'genre' => html_entity_decode($mov['genre'] , ENT_QUOTES, 'UTF-8'), + 'imdbid' => $mov['imdb_id'], + 'language' => html_entity_decode($mov['language'], ENT_QUOTES, 'UTF-8'), + 'plot' => html_entity_decode(preg_replace('/\s+See full summary »/', ' ', $mov['plot']), ENT_QUOTES, 'UTF-8'), + 'rating' => $mov['rating'], + 'tagline' => html_entity_decode($mov['tagline'] , ENT_QUOTES, 'UTF-8'), + 'title' => $mov['title'], + 'tmdbid' => $mov['tmdb_id'], + 'type' => html_entity_decode(ucwords(preg_replace('/[\.\_]/', ' ', $mov['type'])), ENT_QUOTES, 'UTF-8'), + 'year' => $mov['year'] + ]); if ($this->echooutput && $this->service !== '') { $this->pdo->log->doEcho( @@ -936,7 +1003,6 @@ class Film if ($lookupIMDB == 0) { return; } - $trakTv = new \TraktTv(['Settings' => $this->pdo]); // Get all releases without an IMDB id. $res = $this->pdo->query( @@ -957,6 +1023,9 @@ class Film $movieCount = count($res); if ($movieCount > 0) { + if (is_null($this->traktTv)) { + $this->trakTv = new TraktTv(['Settings' => $this->pdo]); + } if ($this->echooutput && $movieCount > 1) { $this->pdo->log->doEcho($this->pdo->log->header("Processing " . $movieCount . " movie releases.")); } @@ -1013,11 +1082,14 @@ class Film } // Check on trakt. - $getIMDBid = $trakTv->movieSummary($movieName); - if ($getIMDBid !== false) { - $imdbID = $this->doMovieUpdate($getIMDBid, 'Trakt', $arr['id']); - if ($imdbID !== false) { - continue; + $data = $this->trakTv->movieSummary($movieName, 'full'); + if ($data !== false) { + $this->parseTraktTv($data); + if (isset($data['ids']['imdb'])) { + $imdbID = $this->doMovieUpdate($data['ids']['imdb'], 'Trakt', $arr['id']); + if ($imdbID !== false) { + continue; + } } } diff --git a/newznab/utility/Utility.php b/newznab/utility/Utility.php index 2c0f605a3..2f592394f 100644 --- a/newznab/utility/Utility.php +++ b/newznab/utility/Utility.php @@ -704,13 +704,12 @@ class Utility */ public static function imdb_trailers($imdbID) { - $xml = Utility::getUrl(['http://api.traileraddict.com/?imdb=' . $imdbID]); + $xml = Utility::getUrl(['url' => 'http://api.traileraddict.com/?imdb=' . $imdbID]); if ($xml !== false) { - if (preg_match('/()/i', $xml, $html)) { - return $html[1]; + if (preg_match('#(v\.traileraddict\.com/\d+)#i', $xml, $html)) { + return 'https://' . $html[1]; } } - return ''; } diff --git a/resources/db/patches/0170~movieinfo.sql b/resources/db/patches/0170~movieinfo.sql new file mode 100644 index 000000000..23dbf5d9e --- /dev/null +++ b/resources/db/patches/0170~movieinfo.sql @@ -0,0 +1 @@ +ALTER TABLE movieinfo ADD COLUMN trailer VARCHAR(255) NOT NULL DEFAULT ''; \ No newline at end of file diff --git a/resources/db/patches/0171~settings.sql b/resources/db/patches/0171~settings.sql new file mode 100644 index 000000000..4bf473e06 --- /dev/null +++ b/resources/db/patches/0171~settings.sql @@ -0,0 +1,23 @@ +INSERT IGNORE INTO settings (section, subsection, name, value, hint, setting) +VALUES ( + 'site', + 'trailers', + 'trailers_display', + '1', + 'Display trailers on the details page?', + 'trailers_display' +), ( + 'site', + 'trailers', + 'trailers_size_x', + '480', + 'Width of the displayed trailer. 480 by default.', + 'trailers_size_x' +), ( + 'site', + 'trailers', + 'trailers_size_y', + '345', + 'Height of the displayed trailer. 345 by default.', + 'trailers_size_y' +); diff --git a/www/admin/movie-edit.php b/www/admin/movie-edit.php index 74a06dace..97f2af5b1 100644 --- a/www/admin/movie-edit.php +++ b/www/admin/movie-edit.php @@ -47,7 +47,20 @@ if (isset($_REQUEST["id"])) $_POST['cover'] = (file_exists($coverLoc)) ? 1 : 0; $_POST['backdrop'] = (file_exists($backdropLoc)) ? 1 : 0; - $movie->update($id, $_POST["title"], $_POST['tagline'], $_POST["plot"], $_POST["year"], $_POST["rating"], $_POST["genre"], $_POST["director"], $_POST["actors"], $_POST["language"], $_POST["cover"], $_POST['backdrop']); + $movie->update([ + 'actors' => $_POST["actors"], + 'backdrop' => $_POST['backdrop'], + 'cover' => $_POST["cover"], + 'director' => $_POST["director"], + 'genre' => $_POST["genre"], + 'imdbid' => $id, + 'language' => $_POST["language"], + 'plot' => $_POST["plot"], + 'rating' => $_POST["rating"], + 'tagline' => $_POST['tagline'], + 'title' => $_POST["title"], + 'year' => $_POST["year"] + ]); header("Location:".WWW_TOP."/movie-list.php"); die(); diff --git a/www/pages/details.php b/www/pages/details.php index 2be14428c..3e38b1d4b 100644 --- a/www/pages/details.php +++ b/www/pages/details.php @@ -72,33 +72,24 @@ if (isset($_GET["id"])) $mov = ''; if ($data['imdbid'] != '' && $data['imdbid'] != 0000000) { - $movie = new Film(); - $mov = $movie->getMovieInfo($data['imdbid']); - - $trakt = new TraktTv(); - $traktSummary = $trakt->movieSummary('tt' . $data['imdbid'], 'full'); - if ($traktSummary !== false && - isset($traktSummary['trailer']) && - $traktSummary['trailer'] !== '' && - preg_match('/[\/?]v[\/\=](\w+)$/i', $traktSummary['trailer'], $youtubeM) - ) { - $mov['trailer'] = - ''; - } else { - $mov['trailer'] = \newznab\utility\Utility::imdb_trailers($data['imdbid']); - } - + $movie = new Film(['Settings' => $page->settings]); + $mov = $movie->getMovieInfo($data['imdbid']); if ($mov && isset($mov['title'])) { - $mov['title'] = str_replace(array('/', '\\'), '', $mov['title']); - $mov['actors'] = $movie->makeFieldLinks($mov, 'actors'); - $mov['genre'] = $movie->makeFieldLinks($mov, 'genre'); + $mov['title'] = str_replace(['/', '\\'], '', $mov['title']); + $mov['actors'] = $movie->makeFieldLinks($mov, 'actors'); + $mov['genre'] = $movie->makeFieldLinks($mov, 'genre'); $mov['director'] = $movie->makeFieldLinks($mov, 'director'); - } else if ($traktSummary !== false) { - $mov['title'] = str_replace(array('/', '\\'), '', $traktSummary['title']); - } else { - $mov = false; + if ($page->settings->getSetting('trailers_display')) { + $trailer = (!isset($mov['trailer']) || empty($mov['trailer']) ? $movie->getTrailer($data['imdbid']) : $mov['trailer']); + if ($trailer) { + $mov['trailer'] = sprintf( + "", + $page->settings->getSetting('trailers_size_x'), + $page->settings->getSetting('trailers_size_y'), + $trailer + ); + } + } } } diff --git a/www/templates_shared/views/admin/site-edit.tpl b/www/templates_shared/views/admin/site-edit.tpl index 79ba43212..1d74d72f1 100644 --- a/www/templates_shared/views/admin/site-edit.tpl +++ b/www/templates_shared/views/admin/site-edit.tpl @@ -1156,7 +1156,6 @@ usually on XXX releases.
- @@ -1226,7 +1225,32 @@ - +
+ Movie Trailer settings + + + + + + + + + + + + + +
+ {html_radios id="trailers_display" name='trailers_display' values=$yesno_ids output=$yesno_names selected=$site->trailers_display separator='
'} +
Fetch and display trailers from TraktTV (Requires API key) and/or TrailerAddict on the details page?
+
+ +
Maximum width in pixels for the trailer window. (Default: 480)
+
+ +
Maximum height in pixels for the trailer window. (Default: 345)
+
+
RequestID Settings