Pull more info from Trakt for movies, change trailers for traileraddict.

This commit is contained in:
DariusIII
2015-08-07 22:14:15 +02:00
parent e2f547188c
commit 2aababe10f
7 changed files with 249 additions and 126 deletions
+166 -94
View File
@@ -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;
}
}
}
+3 -4
View File
@@ -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('/(<iframe.+?<\/iframe>)/i', $xml, $html)) {
return $html[1];
if (preg_match('#(v\.traileraddict\.com/\d+)#i', $xml, $html)) {
return 'https://' . $html[1];
}
}
return '';
}
+1
View File
@@ -0,0 +1 @@
ALTER TABLE movieinfo ADD COLUMN trailer VARCHAR(255) NOT NULL DEFAULT '';
+23
View File
@@ -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'
);
+14 -1
View File
@@ -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();
+16 -25
View File
@@ -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'] =
'<embed width="480" height="345" src="' .
'https://www.youtube.com/v/' . $youtubeM[1] .
'" type="application/x-shockwave-flash"></embed>';
} 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(
"<iframe width=\"%d\" height=\"%d\" src=\"%s\"></iframe>",
$page->settings->getSetting('trailers_size_x'),
$page->settings->getSetting('trailers_size_y'),
$trailer
);
}
}
}
}
+26 -2
View File
@@ -1156,7 +1156,6 @@
usually on XXX releases.<br/></div>
</td>
</tr>
</tr>
<tr>
<td style="width:180px;"><label for="processthumbnails">Process Video Thumbnails:</label></td>
<td>
@@ -1226,7 +1225,32 @@
</tr>
</table>
</fieldset>
<fieldset id="site_movietrailerset">
<legend>Movie Trailer settings</legend>
<table class="input">
<tr>
<td style="width:180px;"><label for="trailers_display">Fetch/Display Movie Trailers:</label></td>
<td>
{html_radios id="trailers_display" name='trailers_display' values=$yesno_ids output=$yesno_names selected=$site->trailers_display separator='<br />'}
<div class="hint">Fetch and display trailers from TraktTV (Requires API key) and/or TrailerAddict on the details page?</div>
</td>
</tr>
<tr>
<td style="width:180px;"><label for="trailers_size_x">Trailers width:</label></td>
<td>
<input class="short" id="trailers_size_x" name="trailers_size_x" type="text" value="{$site->trailers_size_x}"/>
<div class="hint">Maximum width in pixels for the trailer window. (Default: 480)</div>
</td>
</tr>
<tr>
<td style="width:180px;"><label for="trailers_size_y">Trailers height:</label></td>
<td>
<input class="short" id="trailers_size_y" name="trailers_size_y" type="text" value="{$site->trailers_size_y}"/>
<div class="hint">Maximum height in pixels for the trailer window. (Default: 345)</div>
</td>
</tr>
</table>
</fieldset>
<fieldset id="site_requidset">
<legend>RequestID Settings</legend>
<table class="input">