+++ /dev/null
-<?php
-
-use Tmdb\ApiToken;
-use Tmdb\Client;
-
-$tmdbclient = null;
-
-/**
- * @return Client
- */
-function getTmbdClient()
-{
- global $tmdbclient;
- if (null === $tmdbclient) {
- $token = new ApiToken('319e5e721cf55cc1ffa3faa2f600be64');
- $tmdbclient = new Client($token);
- }
- return $tmdbclient;
-}
-
-function getTVShows()
-{
- return _getTVShows();
-}
-
-function _getMediaRecentAdded($force = false)
-{
- if ($force) {
- global $directories;
-
- $res = ['shortcuts' => [], 'subs' => []];
- $res['shortcuts']['size'] = 'poster';
- $res['shortcuts'][] = ['label' => 'Ajoutés récemment', 'type' => 'back'];
-
- $tvlibrary = new tvShowLibrary($directories);
- $tv = $tvlibrary->getRecents();
-
- $molibrary = new moviesLibrary($directories);
- $movies = $molibrary->getRecents();
-
- $medias = array_merge($tv, $movies);
- usort($medias, function ($a, $b) {
- return $b['mtime'] - $a['mtime'];
- });
-
- foreach ($medias as $media) {
- if ($media['type'] == 'tvshow') {
- $tvlibrary->shortcut($media, 'recent-', $res);
- } else if ($media['type'] == 'movie') {
- $molibrary->shortcut($media, 'recent-', $res['shortcuts']);
- }
- }
-
- connectRedis()->igbset('medialibrary.mediarecent', $res);
- return $res;
- }
- $res = connectRedis()->igbget('medialibrary.mediarecent');
- if ($res === false) {
- return _getMediaRecentAdded(true);
- }
- return $res;
-}
-
-function getMediaRecentAdded()
-{
- return _getMediaRecentAdded();
-}
-
-function _getTVShows($force = false, $forceapi = false)
-{
- if ($force) {
- global $directories;
- $library = new tvShowLibrary($directories, $forceapi);
- $res = $library->getShortcuts();
- connectRedis()->igbsetex('medialibrary.tvshows', 1800, $res);
- return $res;
- }
- $res = connectRedis()->igbget('medialibrary.tvshows');
- if ($res === false) {
- return _getTVShows(true);
- }
- return $res;
-}
-
-function getMovies()
-{
- return _getMovies();
-}
-
-function _getMovies($force = false, $forceapi = false)
-{
- if ($force) {
- global $directories;
- $library = new moviesLibrary($directories, $forceapi);
- $res = $library->getShortcuts();
- connectRedis()->igbsetex('medialibrary.movies', 1800, $res);
- return $res;
- }
- $res = connectRedis()->igbget('medialibrary.movies');
- if ($res === false) {
- return _getMovies(true);
- }
- return $res;
-}
-
-function tmdbToTvdb($tmdbid)
-{
- $cacheKey = 'tmdb_to_tvdb_' . $tmdbid;
- $cached = getState($cacheKey, null);
- if (null === $cached) {
- $client = getTmbdClient();
- $find = $client->getTvApi();
- $data = $find->getExternalIds($tmdbid);
- $cached = $data['tvdb_id'];
- setState($cacheKey, $cached);
- }
- return $cached;
-}
-
-function mb_strcasecmp($str1, $str2, $encoding = null)
-{
- if (null === $encoding) {
- $encoding = mb_internal_encoding();
- }
- return strcmp(mb_strtoupper($str1, $encoding), mb_strtoupper($str2, $encoding));
-}
-
-class tvShowLibrary extends mediaLibrary
-{
-
- protected $tvShows = [];
- protected $force = false;
- protected $tmdbclient;
- protected $find;
-
- public function __construct($directories, $force = false)
- {
- $this->force = $force;
-
- $this->tmdbclient = getTmbdClient();
- $this->find = $this->tmdbclient->getFindApi();
-
- foreach ($directories as $directory) {
- if ($directory['context'] != 'tvshow') {
- continue;
- }
- $d = '/volume1/Share/Videos/' . $directory['dir'];
- if (!file_exists($d) || !is_dir($d)) {
- continue;
- }
- $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($d));
- foreach ($it as $f) {
- /** @var $f SplFileInfo */
- if ($f->getFilename() == '.tmbd.id') {
- @rename($f->getPathname(), str_replace('.tmbd.id', '.tmdb.id', $f->getPathname()));
- continue;
- }
- if ($f->isDir() && !file_exists($f->getPath() . '/.tmdb.id') && strpos($f->getPath(), '.tt') !== false) {
- $e = explode('.', $f->getPath());
- $last = array_pop($e);
- $r = $this->find->findBy($last, ['external_source' => 'imdb_id']);
-
- foreach ($r['tv_results'] as $tv_result) {
- if (isset($tv_result['id'])) {
- file_put_contents($f->getPath() . '/.tmdb.id', $tv_result['id']);
- break;
- }
- }
- continue;
- }
- if ($f->isDir() || strpos($f->getFilename(), '.tmdb.id') === false) {
- continue;
- }
-
- $file = str_replace('.tmdb.id', '', $f->getPathname());
-
- $id = file_get_contents($f->getPathname() . '');
- $data = $this->getTVShowByID($id);
- $edate = explode('-', $data['last_air_date']);
- $mtime = gmmktime(12, 0, 0, $edate[1], $edate[2], $edate[0]);
-
- if (file_exists($file . '.netflix')) {
- $dir = 'netflix:' . file_get_contents($file . '.netflix');
- } else {
- $dir = file_exists($file) && is_file($file) ? file_get_contents($file) : $f->getPath();
- }
-
- $data = $this->getTVShowByID($id);
- $this->tvShows[] = ['type' => 'tvshow', 'dir' => $dir, 'data' => $data, 'id' => $id, 'mtime' => $mtime, 'status' => $data['kodi_status']];
- }
- }
-
- usort($this->tvShows, [$this, '_sortTVByName']);
- }
-
- public function getTVShowByID($id)
- {
- if (!$this->force) {
- try {
- $res = connectRedis()->igbget('tmdb.tv.' . $id);
- } catch (Exception $e) {
- $res = false;
- }
- } else {
- $res = false;
- }
-
- if ($res === false) {
- if (!hasNetwork()) {
- return null;
- }
- $client = getTmbdClient();
-
- try {
- $res = $client->getTvApi()->getTvshow($id, ['language' => 'fr-FR']);
- connectRedis()->igbsetex('tmdb.tv.' . $id, 259200, $res);
- } catch (Exception $e) {
- echo $e->getMessage();
- return null;
- }
-
- }
-
- $res = $this->addKodiStatus($id, $res);
- return $res;
- }
-
- public function addKodiStatus($id, $tv)
- {
- $tv['kodi_status'] = kodiGetTVShowStatus($id);
- return $tv;
- }
-
- protected function _sortTVByName($a, $b)
- {
- return mb_strcasecmp($a['data']['name'], $b['data']['name']);
- }
-
- public function getShortcuts()
- {
-
- $res = ['shortcuts' => [], 'subs' => []];
- $res['shortcuts']['size'] = 'poster';
- $res['shortcuts'][] = ['label' => 'Séries TV', 'type' => 'back'];
-
- $status = [0 => 'En cours', 1 => 'A voir', 2 => 'Vue', 3 => 'A venir'];
- foreach ($status as $s => $label) {
- $res['shortcuts'][] = ['label' => $label, 'type' => 'separator'];
- foreach ($this->tvShows as $tvShow) {
- if ($tvShow['status'] != $s) {
- continue;
- }
- $this->shortcut($tvShow, '', $res);
- }
- }
-
- return $res;
- }
-
- public function shortcut($tvShow, $prefix, &$res)
- {
- global $videoExt;
-
- set_time_limit(10);
-
- $e = explode(':', $tvShow['dir']);
- if ($e[0] === 'netflix') {
- $res['shortcuts'][] = ['label' => $tvShow['data']['name'], 'type' => 'netflix', 'url' => '/scripts/netflix.php?id=' . $e[1], 'poster' => $this->_poster($tvShow['data']['poster_path'])];
- return;
- }
-
- $subname = $prefix . 'tvshow-' . $tvShow['id'];
- $res['shortcuts'][] = ['label' => $tvShow['data']['name'], 'type' => 'sub', 'sub' => $subname, 'poster' => $this->_poster($tvShow['data']['poster_path'])];
-
- $res['subs']['sub-' . $subname] = ['size' => 'poster'];
- $res['subs']['sub-' . $subname][] = ['label' => $tvShow['data']['name'], 'type' => 'back'];
-
- $seasons = $tvShow['data']['seasons'];
-
- if (isset($seasons[0]['season_number']) && !$seasons[0]['season_number']) {
- $s = array_shift($seasons);
- array_push($seasons, $s);
- }
-
- foreach ($seasons as $season) {
- $closed = false;
- $missing = false;
- $ns = $this->_n($season['season_number'], 's');
- if ($season['season_number'] == 0) {
- if (file_exists($tvShow['dir'] . '/S00')) {
- $dir = $tvShow['dir'] . '/S00';
- } else {
- $dir = $tvShow['dir'] . '/Specials';
- }
- $closed = true;
- } else {
- $dir = $tvShow['dir'] . '/' . $ns;
- }
-
- if ($season['episode_count'] <= 0) {
- continue;
- }
-
- $accordionId = 'acc_' . sha1($subname . '--' . $ns);
- $res['subs']['sub-' . $subname][] = ['label' => $ns, 'type' => 'accordion', 'id' => $accordionId, 'closed' => accordionClosed($accordionId)];
- for ($i = 1; $i <= $season['episode_count']; $i++) {
- $es = $this->_n($i, 'e');
- $exists = false;
- $base = $dir . '/' . $es . '.';
- $bases = $dir . '/' . $ns . $es . '.';
- foreach ($videoExt as $ext) {
- $f = $base . $ext;
- $fs = $bases . $ext;
- if (file_exists($f)) {
- $exists = $f;
- break;
- }
- if (file_exists($fs)) {
- $exists = $fs;
- }
- }
-
- if ($exists) {
- $path = $this->_path($exists);
- $res['subs']['sub-' . $subname][] = ['label' => $es, 'type' => 'media', 'srt' => hasSRT($path), 'seen' => $this->_seen($path), 'path' => $path];
- } else {
- $res['subs']['sub-' . $subname][] = ['label' => $es, 'type' => 'missing'];
- }
-
- }
-
- $res['subs']['sub-' . $subname][] = ['type' => 'accordion-end'];
- }
- }
-
- protected function _n($number, $letter)
- {
- if ($number < 10) {
- $number = "0" . $number;
- }
- return mb_strtoupper($letter) . $number;
- }
-
- public function getTVShows()
- {
- return $this->tvShows;
- }
-
- public function getRecents()
- {
- $m = [];
- foreach ($this->tvShows as $k => $show) {
- if (!isset($show['status']) || $show['status'] >= 2) {
- continue;
- }
- $m[$k] = $show;
- }
- usort($m, function ($a, $b) {
- return $b['mtime'] - $a['mtime'];
- });
- return array_slice($m, 0, 20);
- }
-}
-
-class mediaLibrary
-{
- protected static $_seen = null;
-
- protected function _path($p)
- {
-// $e = explode('.', $p);
-// $ext = array_pop($e);
-//
-// array_push($e, 'x264');
-// array_push($e, $ext);
-// $x264 = implode('.', $e);
-// if (file_exists($x264)) {
-// $p = $x264;
-// }
- return str_replace('/volume1/Share', '', $p);
- }
-
- protected function _seen($p)
- {
- if (null === self::$_seen) {
- self::$_seen = connectRedis()->igbget('mediaseen');
- if (!is_array(self::$_seen)) {
- self::$_seen = [];
- }
- }
- return isset(self::$_seen['/volume1/Share' . $p]);
-
- }
-
- protected function dirfilemtime($dirName, $doRecursive)
- {
- $d = dir($dirName);
- $lastModified = 0;
- if ($d !== false) {
- while ($entry = $d->read()) {
- if ($entry != "." && $entry != "..") {
- if (!is_dir($dirName . "/" . $entry) && file_exists($dirName . "/" . $entry)) {
- $currentModified = filemtime($dirName . "/" . $entry);
- } else if ($doRecursive && is_dir($dirName . "/" . $entry)) {
- $currentModified = $this->dirfilemtime($dirName . "/" . $entry, true);
- }
- if (isset($currentModified) && $currentModified > $lastModified) {
- $lastModified = $currentModified;
- }
- }
- }
- $d->close();
- return $lastModified;
- }
- return $lastModified;
- }
-
- public function _poster($path, $alt = '')
- {
- if (null === $path || $path === '') {
- if ($alt === '') {
- return '';
- }
- return cacheMedia('/volume1/Share/' . $alt);
- } else {
- return cacheMedia('https://image.tmdb.org/t/p/w500' . $path);
- }
- }
-}
-
-class moviesLibrary extends mediaLibrary
-{
- protected $movies = [];
- protected $force = false;
-
- public function __construct($directories, $force = false)
- {
- $this->force = $force;
-
- foreach ($directories as $directory) {
- if ($directory['context'] != 'movie') {
- continue;
- }
- $d = '/volume1/Share/Videos/' . $directory['dir'];
- if (!file_exists($d) || !is_dir($d)) {
- continue;
- }
- $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($d));
- foreach ($it as $f) {
- /** @var $f SplFileInfo */
- if ($f->isDir() || !stristr($f->getFilename(), '.tmdb.id')) {
- continue;
- }
- $id = file_get_contents($f->getPathname());
- if ($id == 'unknwown') {
- continue;
- }
- $path = str_replace('.tmdb.id', '', $f->getPathname());
- if (!file_exists($path)) {
- unlink($f->getPathname());
- continue;
- }
- $mtime = 0;
- if (file_exists($path)) {
- $mtime = filemtime($path);
- }
- $this->movies[] = ['type' => 'movie', 'data' => $this->getMovieByID($id), 'path' => $path, 'mtime' => $mtime];
- }
- usort($this->movies, array($this, '_orderMovies'));
- }
-
- }
-
- public function getRecents()
- {
- $m = $this->movies;
- usort($m, function ($a, $b) {
- return $b['mtime'] - $a['mtime'];
- });
- return array_slice($m, 0, 60);
- }
-
- function getShortcuts()
- {
- $lists = [
- ['label' => 'Par titre', 'id' => 'alpha', 'list' => $this->getAlphaShortcuts()],
- ['label' => 'Par genre', 'id' => 'genre', 'list' => $this->getGenreShortcuts()],
- ['label' => 'Par langue', 'id' => 'language', 'list' => $this->getLanguagesShortcuts()],
- ['label' => 'Par année', 'id' => 'year', 'list' => $this->getYearsShortcuts()],
- ];
-
- $res = ['shortcuts' => [['label' => 'Films', 'type' => 'back']], 'subs' => []];
-
- foreach ($lists as $list) {
- $res['shortcuts'][] = ['label' => $list['label'], 'type' => 'sub', 'sub' => 'movies-' . $list['id']];
- $res['subs']['sub-movies-' . $list['id']] = $list['list']['shortcuts'];
- $res['subs'] = array_merge($res['subs'], $list['list']['subs']);
- }
-
- return $res;
- }
-
- public function getAlphaShortcuts()
- {
- $letters = [];
- foreach (str_split('#ABCDEFGHIJKLMNOPQRSTUVWXYZ') as $l) {
- $letters[$l] = [];
- }
-
- foreach ($this->movies as $movie) {
- $firstLetters = $this->_firstLetters([$movie['data']['title'], $movie['data']['original_title']]);
- foreach ($firstLetters as $firstLetter) {
- if (!isset($letters[$firstLetter])) {
- continue;
- }
- $letters[$firstLetter][] = $movie;
- }
- }
- return $this->_makeSub($letters, 'movies-alpha-', 'Films par titre', 'poster');
- }
-
- protected function _firstLetters($titles)
- {
- $res = [];
- $allTitles = [];
- foreach ($titles as $title) {
- $title = mb_strtolower($title);
- $title = $this->remove_accents($title);
- $title = trim($title, '\' ');
- $allTitles[] = $title;
- $allTitles[] = $this->_removeMeanlessWords($title);
- }
-
- $allTitles = array_unique($allTitles);
- foreach ($allTitles as $title) {
- $letter = mb_substr($title, 0, 1);
- $letter = mb_strtoupper($letter);
- $letter = preg_replace('|\d|', '#', $letter);
- $res[] = $letter;
- }
-
- return array_unique($res);
- }
-
- protected function _removeMeanlessWords($title)
- {
- $remove = ['un', 'une', 'le', 'la', 'l', 'the', 'a', 'der', 'die', 'das', 'ein', 'el', 'una'];
- $e = preg_split('/(\s|\')/', mb_strtolower($title));
- $res = [];
- foreach ($e as $item) {
- if (!in_array($item, $remove)) {
- $res[] = $item;
- }
- }
- return implode(' ', $res);
- }
-
- protected function remove_accents($string)
- {
- if (!preg_match('/[\x80-\xff]/', $string))
- return $string;
-
- $chars = array(
- // Decompositions for Latin-1 Supplement
- chr(195) . chr(128) => 'A', chr(195) . chr(129) => 'A',
- chr(195) . chr(130) => 'A', chr(195) . chr(131) => 'A',
- chr(195) . chr(132) => 'A', chr(195) . chr(133) => 'A',
- chr(195) . chr(135) => 'C', chr(195) . chr(136) => 'E',
- chr(195) . chr(137) => 'E', chr(195) . chr(138) => 'E',
- chr(195) . chr(139) => 'E', chr(195) . chr(140) => 'I',
- chr(195) . chr(141) => 'I', chr(195) . chr(142) => 'I',
- chr(195) . chr(143) => 'I', chr(195) . chr(145) => 'N',
- chr(195) . chr(146) => 'O', chr(195) . chr(147) => 'O',
- chr(195) . chr(148) => 'O', chr(195) . chr(149) => 'O',
- chr(195) . chr(150) => 'O', chr(195) . chr(153) => 'U',
- chr(195) . chr(154) => 'U', chr(195) . chr(155) => 'U',
- chr(195) . chr(156) => 'U', chr(195) . chr(157) => 'Y',
- chr(195) . chr(159) => 's', chr(195) . chr(160) => 'a',
- chr(195) . chr(161) => 'a', chr(195) . chr(162) => 'a',
- chr(195) . chr(163) => 'a', chr(195) . chr(164) => 'a',
- chr(195) . chr(165) => 'a', chr(195) . chr(167) => 'c',
- chr(195) . chr(168) => 'e', chr(195) . chr(169) => 'e',
- chr(195) . chr(170) => 'e', chr(195) . chr(171) => 'e',
- chr(195) . chr(172) => 'i', chr(195) . chr(173) => 'i',
- chr(195) . chr(174) => 'i', chr(195) . chr(175) => 'i',
- chr(195) . chr(177) => 'n', chr(195) . chr(178) => 'o',
- chr(195) . chr(179) => 'o', chr(195) . chr(180) => 'o',
- chr(195) . chr(181) => 'o', chr(195) . chr(182) => 'o',
- chr(195) . chr(182) => 'o', chr(195) . chr(185) => 'u',
- chr(195) . chr(186) => 'u', chr(195) . chr(187) => 'u',
- chr(195) . chr(188) => 'u', chr(195) . chr(189) => 'y',
- chr(195) . chr(191) => 'y',
- // Decompositions for Latin Extended-A
- chr(196) . chr(128) => 'A', chr(196) . chr(129) => 'a',
- chr(196) . chr(130) => 'A', chr(196) . chr(131) => 'a',
- chr(196) . chr(132) => 'A', chr(196) . chr(133) => 'a',
- chr(196) . chr(134) => 'C', chr(196) . chr(135) => 'c',
- chr(196) . chr(136) => 'C', chr(196) . chr(137) => 'c',
- chr(196) . chr(138) => 'C', chr(196) . chr(139) => 'c',
- chr(196) . chr(140) => 'C', chr(196) . chr(141) => 'c',
- chr(196) . chr(142) => 'D', chr(196) . chr(143) => 'd',
- chr(196) . chr(144) => 'D', chr(196) . chr(145) => 'd',
- chr(196) . chr(146) => 'E', chr(196) . chr(147) => 'e',
- chr(196) . chr(148) => 'E', chr(196) . chr(149) => 'e',
- chr(196) . chr(150) => 'E', chr(196) . chr(151) => 'e',
- chr(196) . chr(152) => 'E', chr(196) . chr(153) => 'e',
- chr(196) . chr(154) => 'E', chr(196) . chr(155) => 'e',
- chr(196) . chr(156) => 'G', chr(196) . chr(157) => 'g',
- chr(196) . chr(158) => 'G', chr(196) . chr(159) => 'g',
- chr(196) . chr(160) => 'G', chr(196) . chr(161) => 'g',
- chr(196) . chr(162) => 'G', chr(196) . chr(163) => 'g',
- chr(196) . chr(164) => 'H', chr(196) . chr(165) => 'h',
- chr(196) . chr(166) => 'H', chr(196) . chr(167) => 'h',
- chr(196) . chr(168) => 'I', chr(196) . chr(169) => 'i',
- chr(196) . chr(170) => 'I', chr(196) . chr(171) => 'i',
- chr(196) . chr(172) => 'I', chr(196) . chr(173) => 'i',
- chr(196) . chr(174) => 'I', chr(196) . chr(175) => 'i',
- chr(196) . chr(176) => 'I', chr(196) . chr(177) => 'i',
- chr(196) . chr(178) => 'IJ', chr(196) . chr(179) => 'ij',
- chr(196) . chr(180) => 'J', chr(196) . chr(181) => 'j',
- chr(196) . chr(182) => 'K', chr(196) . chr(183) => 'k',
- chr(196) . chr(184) => 'k', chr(196) . chr(185) => 'L',
- chr(196) . chr(186) => 'l', chr(196) . chr(187) => 'L',
- chr(196) . chr(188) => 'l', chr(196) . chr(189) => 'L',
- chr(196) . chr(190) => 'l', chr(196) . chr(191) => 'L',
- chr(197) . chr(128) => 'l', chr(197) . chr(129) => 'L',
- chr(197) . chr(130) => 'l', chr(197) . chr(131) => 'N',
- chr(197) . chr(132) => 'n', chr(197) . chr(133) => 'N',
- chr(197) . chr(134) => 'n', chr(197) . chr(135) => 'N',
- chr(197) . chr(136) => 'n', chr(197) . chr(137) => 'N',
- chr(197) . chr(138) => 'n', chr(197) . chr(139) => 'N',
- chr(197) . chr(140) => 'O', chr(197) . chr(141) => 'o',
- chr(197) . chr(142) => 'O', chr(197) . chr(143) => 'o',
- chr(197) . chr(144) => 'O', chr(197) . chr(145) => 'o',
- chr(197) . chr(146) => 'OE', chr(197) . chr(147) => 'oe',
- chr(197) . chr(148) => 'R', chr(197) . chr(149) => 'r',
- chr(197) . chr(150) => 'R', chr(197) . chr(151) => 'r',
- chr(197) . chr(152) => 'R', chr(197) . chr(153) => 'r',
- chr(197) . chr(154) => 'S', chr(197) . chr(155) => 's',
- chr(197) . chr(156) => 'S', chr(197) . chr(157) => 's',
- chr(197) . chr(158) => 'S', chr(197) . chr(159) => 's',
- chr(197) . chr(160) => 'S', chr(197) . chr(161) => 's',
- chr(197) . chr(162) => 'T', chr(197) . chr(163) => 't',
- chr(197) . chr(164) => 'T', chr(197) . chr(165) => 't',
- chr(197) . chr(166) => 'T', chr(197) . chr(167) => 't',
- chr(197) . chr(168) => 'U', chr(197) . chr(169) => 'u',
- chr(197) . chr(170) => 'U', chr(197) . chr(171) => 'u',
- chr(197) . chr(172) => 'U', chr(197) . chr(173) => 'u',
- chr(197) . chr(174) => 'U', chr(197) . chr(175) => 'u',
- chr(197) . chr(176) => 'U', chr(197) . chr(177) => 'u',
- chr(197) . chr(178) => 'U', chr(197) . chr(179) => 'u',
- chr(197) . chr(180) => 'W', chr(197) . chr(181) => 'w',
- chr(197) . chr(182) => 'Y', chr(197) . chr(183) => 'y',
- chr(197) . chr(184) => 'Y', chr(197) . chr(185) => 'Z',
- chr(197) . chr(186) => 'z', chr(197) . chr(187) => 'Z',
- chr(197) . chr(188) => 'z', chr(197) . chr(189) => 'Z',
- chr(197) . chr(190) => 'z', chr(197) . chr(191) => 's'
- );
-
- $string = strtr($string, $chars);
-
- return $string;
- }
-
- protected function _makeSub($list, $subbase, $parentName, $size = null)
- {
- $res = ['shortcuts' => [], 'subs' => []];
-
- if (null !== $size) {
- $res['shortcuts']['size'] = 'poster';
- }
- $res['shortcuts'][] = ['label' => $parentName, 'type' => 'back'];
- foreach ($list as $key => $movies) {
- $subkey = $subbase . md5($key);
- $res['shortcuts'][] = ['label' => $key, 'type' => 'sub', 'sub' => $subkey];
- $res['subs']['sub-' . $subkey] = $this->_makeShortcuts($movies, $key);
- }
- return $res;
- }
-
- protected function _makeShortcuts($movies, $key)
- {
- $res = ['size' => 'poster'];
- $res[] = ['label' => $key, 'type' => 'back'];
- foreach ($movies as $movie) {
- $this->shortcut($movie, '', $res);
- }
- return $res;
- }
-
- public function shortcut($movie, $prefix, &$res)
- {
- $path = $this->_path($movie['path']);
- $res[] = ['label' => $movie['data']['title'], 'type' => 'media', 'seen' => $this->_seen($path), 'srt' => hasSRT($path), 'path' => $path, 'poster' => $this->_poster($movie['data']['poster_path'], dirname($path) . '/poster.jpg')];
- }
-
-
- public function _orderMovies($a, $b)
- {
- return mb_strcasecmp($a['data']['title'], $b['data']['title']);
- }
-
- public function getGenreShortcuts()
- {
- $genres = [];
- if (is_array($this->movies)) {
- foreach ($this->movies as $movie) {
-
- foreach ($movie['data']['genres'] as $genre) {
- if (!isset($genres[$genre['name']])) {
- $genres[$genre['name']] = [];
- }
- $genres[$genre['name']][] = $movie;
- }
- }
- }
- ksort($genres);
-
- return $this->_makeSub($genres, 'movies-genre-', 'Films par genre');
-
- }
-
- public function getLanguagesShortcuts()
- {
- $languages = [];
- foreach ($this->movies as $movie) {
-
- $lang = mb_strtolower(Zend_Locale::getTranslation($movie['data']['original_language'], 'language', 'fr-FR'));
- if (!isset($languages[$lang])) {
- $languages[$lang] = [];
- }
- $languages[$lang][] = $movie;
- }
- ksort($languages);
-
- return $this->_makeSub($languages, 'movies-language-', 'Films par langue');
- }
-
- public function getYearsShortcuts()
- {
- $years = [];
- foreach ($this->movies as $movie) {
- $e = explode('-', $movie['data']['release_date']);
- $year = $e[0];
- if (!isset($years[$year])) {
- $years[$year] = [];
- }
- $years[$year][] = $movie;
- }
- krsort($years);
-
- return $this->_makeSub($years, 'movies-year-', 'Films par année');
- }
-
-
- public function getMovieByID($id)
- {
- if (!$this->force) {
- $res = connectRedis()->igbget('tmdb.movie.' . $id);
- } else {
- $res = false;
- }
- if ($res === false) {
- if (!hasNetwork()) {
- return null;
- }
- $client = getTmbdClient();
- try {
- $movie = $client->getMoviesApi()->getMovie($id, ['language' => 'fr-FR']);
- connectRedis()->igbsetex('tmdb.movie.' . $id, 259200, $movie);
- return $movie;
- } catch (Exception $e) {
- return null;
- }
- }
- return $res;
- }
-}
-
-
-function parseMoviesLibrary()
-{
- global $directories, $videoExt;
- $d='/volume1/Share/Videos/' . $directories['Films']['dir'];
- if(!file_exists($d) || !is_dir($d)){
- return;
- }
- $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($d));
- $moviesExtensions = $videoExt;
- $tmdbclient = getTmbdClient();
- $search = $tmdbclient->getSearchApi();
- $find = $tmdbclient->getFindApi();
-
- $i = 0;
-
- foreach ($it as $f) {
- /* @var $f SplFileInfo */
- if ($f->isDir()) {
- continue;
- }
-
- if ($f->getExtension() == 'id' && file_get_contents($f->getPathname()) == 'unknwown') {
- echo $f->getPathname() . "\n";
- }
-
- if (!in_array($f->getExtension(), $moviesExtensions)) {
- continue;
- }
-
- if (stristr($f->getPathname(), '.x264.')) {
- continue;
- }
-
- $tmdbfile = $f->getPathname() . '.tmdb.id';
- if (!file_exists($tmdbfile) || file_get_contents($tmdbfile) == 'unknwown') {
- $_basename = $f->getBasename('.' . $f->getExtension());
- $basename = str_replace('.', ' ', $_basename);
- $params = [];
- $e = explode('.', $_basename);
- $last = array_pop($e);
- echo $basename . ':' . $last;
-
- if (strpos($last, 'tt') === 0) {
- $f = $find->findBy($last, ['external_source' => 'imdb_id']);
- foreach ($f['movie_results'] as $movie_result) {
- if (isset($movie_result['id'])) {
- $tmdbid = $movie_result['id'];
- }
- }
- echo $tmdbid;
- } else {
- $s = $search->searchMovies($basename);
- if ($s['total_results'] == 0) {
- $tmdbid = 'unknwown';
- } else {
- $results = $s['results'];
- foreach ($results as $result) {
- if (isset($result['id'])) {
- $tmdbid = $result['id'];
- }
- }
- }
- }
-
- file_put_contents($tmdbfile, $tmdbid);
- $i++;
- }
- }
-}
\ No newline at end of file