plex.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. <?php
  2. trait PlexHomepageItem
  3. {
  4. public function plexSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'Plex',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/plex.png',
  10. 'category' => 'Media Server',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepagePlexEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepagePlexEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepagePlexAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepagePlexAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'plexURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['plexURL'],
  33. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  34. 'placeholder' => 'http(s)://hostname:port'
  35. ),
  36. array(
  37. 'type' => 'password-alt',
  38. 'name' => 'plexToken',
  39. 'label' => 'Token',
  40. 'value' => $this->config['plexToken']
  41. ),
  42. array(
  43. 'type' => 'password-alt',
  44. 'name' => 'plexID',
  45. 'label' => 'Plex Machine',
  46. 'value' => $this->config['plexID']
  47. )
  48. ),
  49. 'Active Streams' => array(
  50. array(
  51. 'type' => 'switch',
  52. 'name' => 'homepagePlexStreams',
  53. 'label' => 'Enable',
  54. 'value' => $this->config['homepagePlexStreams']
  55. ),
  56. array(
  57. 'type' => 'select',
  58. 'name' => 'homepagePlexStreamsAuth',
  59. 'label' => 'Minimum Authorization',
  60. 'value' => $this->config['homepagePlexStreamsAuth'],
  61. 'options' => $this->groupOptions
  62. ),
  63. array(
  64. 'type' => 'switch',
  65. 'name' => 'homepageShowStreamNames',
  66. 'label' => 'User Information',
  67. 'value' => $this->config['homepageShowStreamNames']
  68. ),
  69. array(
  70. 'type' => 'select',
  71. 'name' => 'homepageShowStreamNamesAuth',
  72. 'label' => 'Minimum Authorization',
  73. 'value' => $this->config['homepageShowStreamNamesAuth'],
  74. 'options' => $this->groupOptions
  75. ),
  76. array(
  77. 'type' => 'select',
  78. 'name' => 'homepageStreamRefresh',
  79. 'label' => 'Refresh Seconds',
  80. 'value' => $this->config['homepageStreamRefresh'],
  81. 'options' => $this->timeOptions()
  82. ),
  83. ),
  84. 'Recent Items' => array(
  85. array(
  86. 'type' => 'switch',
  87. 'name' => 'homepagePlexRecent',
  88. 'label' => 'Enable',
  89. 'value' => $this->config['homepagePlexRecent']
  90. ),
  91. array(
  92. 'type' => 'select',
  93. 'name' => 'homepagePlexRecentAuth',
  94. 'label' => 'Minimum Authorization',
  95. 'value' => $this->config['homepagePlexRecentAuth'],
  96. 'options' => $this->groupOptions
  97. ),
  98. array(
  99. 'type' => 'number',
  100. 'name' => 'homepageRecentLimit',
  101. 'label' => 'Item Limit',
  102. 'value' => $this->config['homepageRecentLimit'],
  103. ),
  104. array(
  105. 'type' => 'select',
  106. 'name' => 'homepageRecentRefresh',
  107. 'label' => 'Refresh Seconds',
  108. 'value' => $this->config['homepageRecentRefresh'],
  109. 'options' => $this->timeOptions()
  110. ),
  111. ),
  112. 'Media Search' => array(
  113. array(
  114. 'type' => 'switch',
  115. 'name' => 'mediaSearch',
  116. 'label' => 'Enable',
  117. 'value' => $this->config['mediaSearch']
  118. ),
  119. array(
  120. 'type' => 'select',
  121. 'name' => 'mediaSearchAuth',
  122. 'label' => 'Minimum Authorization',
  123. 'value' => $this->config['mediaSearchAuth'],
  124. 'options' => $this->groupOptions
  125. ),
  126. array(
  127. 'type' => 'select',
  128. 'name' => 'mediaSearchType',
  129. 'label' => 'Media Server',
  130. 'value' => $this->config['mediaSearchType'],
  131. 'options' => $this->mediaServerOptions()
  132. ),
  133. ),
  134. 'Playlists' => array(
  135. array(
  136. 'type' => 'switch',
  137. 'name' => 'homepagePlexPlaylist',
  138. 'label' => 'Enable',
  139. 'value' => $this->config['homepagePlexPlaylist']
  140. ),
  141. array(
  142. 'type' => 'select',
  143. 'name' => 'homepagePlexPlaylistAuth',
  144. 'label' => 'Minimum Authorization',
  145. 'value' => $this->config['homepagePlexPlaylistAuth'],
  146. 'options' => $this->groupOptions
  147. ),
  148. ),
  149. 'Misc Options' => array(
  150. array(
  151. 'type' => 'input',
  152. 'name' => 'plexTabName',
  153. 'label' => 'Plex Tab Name',
  154. 'value' => $this->config['plexTabName'],
  155. 'placeholder' => 'Only use if you have Plex in a reverse proxy'
  156. ),
  157. array(
  158. 'type' => 'input',
  159. 'name' => 'plexTabURL',
  160. 'label' => 'Plex Tab WAN URL',
  161. 'value' => $this->config['plexTabURL'],
  162. 'placeholder' => 'http(s)://hostname:port'
  163. ),
  164. array(
  165. 'type' => 'select',
  166. 'name' => 'cacheImageSize',
  167. 'label' => 'Image Cache Size',
  168. 'value' => $this->config['cacheImageSize'],
  169. 'options' => array(
  170. array(
  171. 'name' => 'Low',
  172. 'value' => '.5'
  173. ),
  174. array(
  175. 'name' => '1x',
  176. 'value' => '1'
  177. ),
  178. array(
  179. 'name' => '2x',
  180. 'value' => '2'
  181. ),
  182. array(
  183. 'name' => '3x',
  184. 'value' => '3'
  185. )
  186. )
  187. )
  188. ),
  189. 'Test Connection' => array(
  190. array(
  191. 'type' => 'blank',
  192. 'label' => 'Please Save before Testing'
  193. ),
  194. array(
  195. 'type' => 'button',
  196. 'label' => '',
  197. 'icon' => 'fa fa-flask',
  198. 'class' => 'pull-right',
  199. 'text' => 'Test Connection',
  200. 'attr' => 'onclick="testAPIConnection(\'plex\')"'
  201. ),
  202. )
  203. )
  204. );
  205. }
  206. public function testConnectionPlex()
  207. {
  208. if (!empty($this->config['plexURL']) && !empty($this->config['plexToken'])) {
  209. $url = $this->qualifyURL($this->config['plexURL']) . "/?X-Plex-Token=" . $this->config['plexToken'];
  210. try {
  211. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  212. $response = Requests::get($url, array(), $options);
  213. libxml_use_internal_errors(true);
  214. if ($response->success) {
  215. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  216. return true;
  217. }
  218. } catch (Requests_Exception $e) {
  219. $this->setAPIResponse('error', $e->getMessage(), 500);
  220. return false;
  221. };
  222. } else {
  223. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  224. return 'URL and/or Token not setup';
  225. }
  226. }
  227. public function resolvePlexItem($item)
  228. {
  229. // Static Height & Width
  230. $height = $this->getCacheImageSize('h');
  231. $width = $this->getCacheImageSize('w');
  232. $nowPlayingHeight = $this->getCacheImageSize('nph');
  233. $nowPlayingWidth = $this->getCacheImageSize('npw');
  234. // Cache Directories
  235. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  236. $cacheDirectoryWeb = 'plugins/images/cache/';
  237. // Types
  238. switch ($item['type']) {
  239. case 'show':
  240. $plexItem['type'] = 'tv';
  241. $plexItem['title'] = (string)$item['title'];
  242. $plexItem['secondaryTitle'] = (string)$item['year'];
  243. $plexItem['summary'] = (string)$item['summary'];
  244. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  245. $plexItem['thumb'] = (string)$item['thumb'];
  246. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  247. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  248. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  249. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  250. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  251. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  252. break;
  253. case 'season':
  254. $plexItem['type'] = 'tv';
  255. $plexItem['title'] = (string)$item['parentTitle'];
  256. $plexItem['secondaryTitle'] = (string)$item['title'];
  257. $plexItem['summary'] = (string)$item['parentSummary'];
  258. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  259. $plexItem['thumb'] = (string)$item['thumb'];
  260. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  261. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  262. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  263. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  264. break;
  265. case 'episode':
  266. $plexItem['type'] = 'tv';
  267. $plexItem['title'] = (string)$item['grandparentTitle'];
  268. $plexItem['secondaryTitle'] = (string)$item['parentTitle'];
  269. $plexItem['summary'] = (string)$item['title'];
  270. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  271. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  272. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  273. $plexItem['nowPlayingThumb'] = (string)$item['grandparentArt'];
  274. $plexItem['nowPlayingKey'] = (string)$item['grandparentRatingKey'] . "-np";
  275. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  276. $plexItem['nowPlayingBottom'] = 'S' . (string)$item['parentIndex'] . ' · E' . (string)$item['index'];
  277. $plexItem['metadataKey'] = (string)$item['grandparentRatingKey'];
  278. break;
  279. case 'clip':
  280. $useImage = (isset($item['live']) ? "plugins/images/cache/livetv.png" : null);
  281. $plexItem['type'] = 'clip';
  282. $plexItem['title'] = (isset($item['live']) ? 'Live TV' : (string)$item['title']);
  283. $plexItem['secondaryTitle'] = '';
  284. $plexItem['summary'] = (string)$item['summary'];
  285. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  286. $plexItem['thumb'] = (string)$item['thumb'];
  287. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  288. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  289. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  290. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  291. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  292. break;
  293. case 'album':
  294. case 'track':
  295. $plexItem['type'] = 'music';
  296. $plexItem['title'] = (string)$item['parentTitle'];
  297. $plexItem['secondaryTitle'] = (string)$item['title'];
  298. $plexItem['summary'] = (string)$item['title'];
  299. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  300. $plexItem['thumb'] = (string)$item['thumb'];
  301. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  302. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  303. $plexItem['nowPlayingKey'] = (string)$item['parentRatingKey'] . "-np";
  304. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  305. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  306. $plexItem['metadataKey'] = isset($item['grandparentRatingKey']) ? (string)$item['grandparentRatingKey'] : (string)$item['parentRatingKey'];
  307. break;
  308. default:
  309. $plexItem['type'] = 'movie';
  310. $plexItem['title'] = (string)$item['title'];
  311. $plexItem['secondaryTitle'] = (string)$item['year'];
  312. $plexItem['summary'] = (string)$item['summary'];
  313. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  314. $plexItem['thumb'] = (string)$item['thumb'];
  315. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  316. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  317. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  318. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  319. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  320. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  321. }
  322. $plexItem['originalType'] = $item['type'];
  323. $plexItem['uid'] = (string)$item['ratingKey'];
  324. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  325. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  326. $plexItem['addedAt'] = isset($item['addedAt']) ? (int)$item['addedAt'] : null;
  327. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  328. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress'] - $plexItem['watched']) : '';
  329. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision'] : '';
  330. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  331. $plexItem['session'] = (string)$item->Session['id'];
  332. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  333. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  334. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  335. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  336. $plexItem['user'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['title'] : "";
  337. $plexItem['userThumb'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['thumb'] : "";
  338. $plexItem['userAddress'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->Player['address'] : "x.x.x.x";
  339. $plexItem['address'] = $this->config['plexTabURL'] ? $this->config['plexTabURL'] . "/web/index.html#!/server/" . $this->config['plexID'] . "/details?key=/library/metadata/" . $item['ratingKey'] : "https://app.plex.tv/web/app#!/server/" . $this->config['plexID'] . "/details?key=/library/metadata/" . $item['ratingKey'];
  340. $plexItem['nowPlayingOriginalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '$' . $this->randString();
  341. $plexItem['originalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '$' . $this->randString();
  342. $plexItem['openTab'] = $this->config['plexTabURL'] && $this->config['plexTabName'] ? true : false;
  343. $plexItem['tabName'] = $this->config['plexTabName'] ? $this->config['plexTabName'] : '';
  344. // Stream info
  345. $plexItem['userStream'] = array(
  346. 'platform' => (string)$item->Player['platform'],
  347. 'product' => (string)$item->Player['product'],
  348. 'device' => (string)$item->Player['device'],
  349. 'stream' => isset($item->Media) ? (string)$item->Media->Part['decision'] . ($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)' : '') : '',
  350. 'videoResolution' => (string)$item->Media['videoResolution'],
  351. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  352. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  353. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  354. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  355. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  356. 'videoDecision' => $this->streamType((string)$item->TranscodeSession['videoDecision']),
  357. 'audioDecision' => $this->streamType((string)$item->TranscodeSession['audioDecision']),
  358. 'container' => (string)$item->TranscodeSession['container'],
  359. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  360. );
  361. // Genre catch all
  362. if ($item->Genre) {
  363. $genres = array();
  364. foreach ($item->Genre as $key => $value) {
  365. $genres[] = (string)$value['tag'];
  366. }
  367. }
  368. // Actor catch all
  369. if ($item->Role) {
  370. $actors = array();
  371. foreach ($item->Role as $key => $value) {
  372. if ($value['thumb']) {
  373. $actors[] = array(
  374. 'name' => (string)$value['tag'],
  375. 'role' => (string)$value['role'],
  376. 'thumb' => (string)$value['thumb']
  377. );
  378. }
  379. }
  380. }
  381. // Metadata information
  382. $plexItem['metadata'] = array(
  383. 'guid' => (string)$item['guid'],
  384. 'summary' => (string)$item['summary'],
  385. 'rating' => (string)$item['rating'],
  386. 'duration' => (string)$item['duration'],
  387. 'originallyAvailableAt' => (string)$item['originallyAvailableAt'],
  388. 'year' => (string)$item['year'],
  389. 'studio' => (string)$item['studio'],
  390. 'tagline' => (string)$item['tagline'],
  391. 'genres' => ($item->Genre) ? $genres : '',
  392. 'actors' => ($item->Role) ? $actors : ''
  393. );
  394. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  395. $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb . $plexItem['nowPlayingKey'] . '.jpg';
  396. }
  397. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  398. $plexItem['imageURL'] = $cacheDirectoryWeb . $plexItem['key'] . '.jpg';
  399. }
  400. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  401. $plexItem['nowPlayingImageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '';
  402. }
  403. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['key'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  404. $plexItem['imageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '';
  405. }
  406. if (!$plexItem['nowPlayingThumb']) {
  407. $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png";
  408. $plexItem['nowPlayingKey'] = "no-np";
  409. }
  410. if (!$plexItem['thumb'] || $plexItem['addedAt'] >= (time() - 300)) {
  411. $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/cache/no-list.png";
  412. $plexItem['key'] = "no-list";
  413. }
  414. if (isset($useImage)) {
  415. $plexItem['useImage'] = $useImage;
  416. }
  417. return $plexItem;
  418. }
  419. public function getPlexHomepageStreams()
  420. {
  421. if (!$this->config['homepagePlexEnabled']) {
  422. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  423. return false;
  424. }
  425. if (!$this->config['homepagePlexStreams']) {
  426. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  427. return false;
  428. }
  429. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  430. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  431. return false;
  432. }
  433. if (!$this->qualifyRequest($this->config['homepagePlexStreamsAuth'])) {
  434. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  435. return false;
  436. }
  437. if (empty($this->config['plexURL'])) {
  438. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  439. return false;
  440. }
  441. if (empty($this->config['plexToken'])) {
  442. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  443. return false;
  444. }
  445. if (empty($this->config['plexID'])) {
  446. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  447. return false;
  448. }
  449. $ignore = array();
  450. $resolve = true;
  451. $url = $this->qualifyURL($this->config['plexURL']);
  452. $url = $url . "/status/sessions?X-Plex-Token=" . $this->config['plexToken'];
  453. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  454. $response = Requests::get($url, array(), $options);
  455. libxml_use_internal_errors(true);
  456. if ($response->success) {
  457. $items = array();
  458. $plex = simplexml_load_string($response->body);
  459. foreach ($plex as $child) {
  460. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  461. $items[] = $this->resolvePlexItem($child);
  462. }
  463. }
  464. $api['content'] = ($resolve) ? $items : $plex;
  465. $api['plexID'] = $this->config['plexID'];
  466. $api['showNames'] = true;
  467. $api['group'] = '1';
  468. $this->setAPIResponse('success', null, 200, $api);
  469. return $api;
  470. }
  471. }
  472. public function getPlexHomepageRecent()
  473. {
  474. if (!$this->config['homepagePlexEnabled']) {
  475. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  476. return false;
  477. }
  478. if (!$this->config['homepagePlexRecent']) {
  479. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  480. return false;
  481. }
  482. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  483. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  484. return false;
  485. }
  486. if (!$this->qualifyRequest($this->config['homepagePlexRecentAuth'])) {
  487. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  488. return false;
  489. }
  490. if (empty($this->config['plexURL'])) {
  491. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  492. return false;
  493. }
  494. if (empty($this->config['plexToken'])) {
  495. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  496. return false;
  497. }
  498. if (empty($this->config['plexID'])) {
  499. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  500. return false;
  501. }
  502. $ignore = array();
  503. $resolve = true;
  504. $url = $this->qualifyURL($this->config['plexURL']);
  505. $urls['movie'] = $url . "/hubs/home/recentlyAdded?X-Plex-Token=" . $this->config['plexToken'] . "&X-Plex-Container-Start=0&X-Plex-Container-Size=" . $this->config['homepageRecentLimit'] . "&type=1";
  506. $urls['tv'] = $url . "/hubs/home/recentlyAdded?X-Plex-Token=" . $this->config['plexToken'] . "&X-Plex-Container-Start=0&X-Plex-Container-Size=" . $this->config['homepageRecentLimit'] . "&type=2";
  507. $urls['music'] = $url . "/hubs/home/recentlyAdded?X-Plex-Token=" . $this->config['plexToken'] . "&X-Plex-Container-Start=0&X-Plex-Container-Size=" . $this->config['homepageRecentLimit'] . "&type=8";
  508. foreach ($urls as $k => $v) {
  509. $options = ($this->localURL($v)) ? array('verify' => false) : array();
  510. $response = Requests::get($v, array(), $options);
  511. libxml_use_internal_errors(true);
  512. if ($response->success) {
  513. $items = array();
  514. $plex = simplexml_load_string($response->body);
  515. foreach ($plex as $child) {
  516. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  517. $items[] = $this->resolvePlexItem($child);
  518. }
  519. }
  520. if (isset($api)) {
  521. $api['content'] = array_merge($api['content'], ($resolve) ? $items : $plex);
  522. } else {
  523. $api['content'] = ($resolve) ? $items : $plex;
  524. }
  525. }
  526. }
  527. if (isset($api['content'])) {
  528. usort($api['content'], function ($a, $b) {
  529. return $b['addedAt'] <=> $a['addedAt'];
  530. });
  531. }
  532. $api['plexID'] = $this->config['plexID'];
  533. $api['showNames'] = true;
  534. $api['group'] = '1';
  535. $this->setAPIResponse('success', null, 200, $api);
  536. return $api;
  537. }
  538. public function getPlexHomepageMetadata($array)
  539. {
  540. if (!$this->config['homepagePlexEnabled']) {
  541. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  542. return false;
  543. }
  544. if (!$this->config['homepagePlexStreams']) {
  545. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  546. return false;
  547. }
  548. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  549. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  550. return false;
  551. }
  552. if (!$this->qualifyRequest($this->config['homepagePlexStreamsAuth'])) {
  553. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  554. return false;
  555. }
  556. if (empty($this->config['plexURL'])) {
  557. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  558. return false;
  559. }
  560. if (empty($this->config['plexToken'])) {
  561. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  562. return false;
  563. }
  564. if (empty($this->config['plexID'])) {
  565. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  566. return false;
  567. }
  568. $key = $array['key'] ?? null;
  569. if (!$key) {
  570. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  571. return false;
  572. }
  573. $ignore = array();
  574. $resolve = true;
  575. $url = $this->qualifyURL($this->config['plexURL']);
  576. $url = $url . "/library/metadata/" . $key . "?X-Plex-Token=" . $this->config['plexToken'];
  577. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  578. $response = Requests::get($url, array(), $options);
  579. libxml_use_internal_errors(true);
  580. if ($response->success) {
  581. $items = array();
  582. $plex = simplexml_load_string($response->body);
  583. foreach ($plex as $child) {
  584. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  585. $items[] = $this->resolvePlexItem($child);
  586. }
  587. }
  588. $api['content'] = ($resolve) ? $items : $plex;
  589. $api['plexID'] = $this->config['plexID'];
  590. $api['showNames'] = true;
  591. $api['group'] = '1';
  592. $this->setAPIResponse('success', null, 200, $api);
  593. return $api;
  594. }
  595. }
  596. public function getPlexHomepagePlaylists()
  597. {
  598. if (!$this->config['homepagePlexEnabled']) {
  599. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  600. return false;
  601. }
  602. if (!$this->config['homepagePlexPlaylist']) {
  603. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  604. return false;
  605. }
  606. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  607. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  608. return false;
  609. }
  610. if (!$this->qualifyRequest($this->config['homepagePlexPlaylistAuth'])) {
  611. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  612. return false;
  613. }
  614. if (empty($this->config['plexURL'])) {
  615. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  616. return false;
  617. }
  618. if (empty($this->config['plexToken'])) {
  619. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  620. return false;
  621. }
  622. if (empty($this->config['plexID'])) {
  623. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  624. return false;
  625. }
  626. $url = $this->qualifyURL($this->config['plexURL']);
  627. $url = $url . "/playlists?X-Plex-Token=" . $this->config['plexToken'];
  628. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  629. $response = Requests::get($url, array(), $options);
  630. libxml_use_internal_errors(true);
  631. if ($response->success) {
  632. $items = array();
  633. $plex = simplexml_load_string($response->body);
  634. foreach ($plex as $child) {
  635. if ($child['playlistType'] == "video" && strpos(strtolower($child['title']), 'private') === false) {
  636. $playlistTitleClean = preg_replace("/(\W)+/", "", (string)$child['title']);
  637. $playlistURL = $this->qualifyURL($this->config['plexURL']);
  638. $playlistURL = $playlistURL . $child['key'] . "?X-Plex-Token=" . $this->config['plexToken'];
  639. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  640. $playlistResponse = Requests::get($playlistURL, array(), $options);
  641. if ($playlistResponse->success) {
  642. $playlistResponse = simplexml_load_string($playlistResponse->body);
  643. $items[$playlistTitleClean]['title'] = (string)$child['title'];
  644. foreach ($playlistResponse->Video as $playlistItem) {
  645. $items[$playlistTitleClean][] = $this->resolvePlexItem($playlistItem);
  646. }
  647. }
  648. }
  649. }
  650. $api['content'] = $items;
  651. $api['plexID'] = $this->config['plexID'];
  652. $api['showNames'] = true;
  653. $api['group'] = '1';
  654. $this->setAPIResponse('success', null, 200, $api);
  655. return $api;
  656. } else {
  657. $this->setAPIResponse('error', 'Plex API error', 500);
  658. return false;
  659. }
  660. }
  661. public function getPlexHomepageSearch($query)
  662. {
  663. if (!$this->config['homepagePlexEnabled']) {
  664. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  665. return false;
  666. }
  667. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  668. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  669. return false;
  670. }
  671. if (empty($this->config['plexURL'])) {
  672. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  673. return false;
  674. }
  675. if (empty($this->config['plexToken'])) {
  676. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  677. return false;
  678. }
  679. if (empty($this->config['plexID'])) {
  680. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  681. return false;
  682. }
  683. $query = $query ?? null;
  684. if (!$query) {
  685. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  686. return false;
  687. }
  688. $ignore = array('artist', 'episode');
  689. $resolve = true;
  690. $url = $this->qualifyURL($this->config['plexURL']);
  691. $url = $url . "/search?query=" . rawurlencode($query) . "&X-Plex-Token=" . $this->config['plexToken'];
  692. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  693. $response = Requests::get($url, array(), $options);
  694. libxml_use_internal_errors(true);
  695. if ($response->success) {
  696. $items = array();
  697. $plex = simplexml_load_string($response->body);
  698. foreach ($plex as $child) {
  699. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  700. $items[] = $this->resolvePlexItem($child);
  701. }
  702. }
  703. $api['content'] = ($resolve) ? $items : $plex;
  704. $api['plexID'] = $this->config['plexID'];
  705. $api['showNames'] = true;
  706. $api['group'] = '1';
  707. $this->setAPIResponse('success', null, 200, $api);
  708. return $api;
  709. }
  710. }
  711. }