plex.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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']) . "/servers?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. } else {
  218. $this->setAPIResponse('error', 'URL and/or Token not setup correctly', 422);
  219. return false;
  220. }
  221. } catch (Requests_Exception $e) {
  222. $this->setAPIResponse('error', $e->getMessage(), 500);
  223. return false;
  224. }
  225. } else {
  226. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  227. return 'URL and/or Token not setup';
  228. }
  229. }
  230. public function resolvePlexItem($item)
  231. {
  232. // Static Height & Width
  233. $height = $this->getCacheImageSize('h');
  234. $width = $this->getCacheImageSize('w');
  235. $nowPlayingHeight = $this->getCacheImageSize('nph');
  236. $nowPlayingWidth = $this->getCacheImageSize('npw');
  237. // Cache Directories
  238. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  239. $cacheDirectoryWeb = 'plugins/images/cache/';
  240. // Types
  241. switch ($item['type']) {
  242. case 'show':
  243. $plexItem['type'] = 'tv';
  244. $plexItem['title'] = (string)$item['title'];
  245. $plexItem['secondaryTitle'] = (string)$item['year'];
  246. $plexItem['summary'] = (string)$item['summary'];
  247. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  248. $plexItem['thumb'] = (string)$item['thumb'];
  249. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  250. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  251. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  252. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  253. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  254. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  255. break;
  256. case 'season':
  257. $plexItem['type'] = 'tv';
  258. $plexItem['title'] = (string)$item['parentTitle'];
  259. $plexItem['secondaryTitle'] = (string)$item['title'];
  260. $plexItem['summary'] = (string)$item['parentSummary'];
  261. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  262. $plexItem['thumb'] = (string)$item['thumb'];
  263. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  264. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  265. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  266. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  267. break;
  268. case 'episode':
  269. $plexItem['type'] = 'tv';
  270. $plexItem['title'] = (string)$item['grandparentTitle'];
  271. $plexItem['secondaryTitle'] = (string)$item['parentTitle'];
  272. $plexItem['summary'] = (string)$item['title'];
  273. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  274. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  275. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  276. $plexItem['nowPlayingThumb'] = (string)$item['grandparentArt'];
  277. $plexItem['nowPlayingKey'] = (string)$item['grandparentRatingKey'] . "-np";
  278. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  279. $plexItem['nowPlayingBottom'] = 'S' . (string)$item['parentIndex'] . ' · E' . (string)$item['index'];
  280. $plexItem['metadataKey'] = (string)$item['grandparentRatingKey'];
  281. break;
  282. case 'clip':
  283. $useImage = (isset($item['live']) ? "plugins/images/cache/livetv.png" : null);
  284. $plexItem['type'] = 'clip';
  285. $plexItem['title'] = (isset($item['live']) ? 'Live TV' : (string)$item['title']);
  286. $plexItem['secondaryTitle'] = '';
  287. $plexItem['summary'] = (string)$item['summary'];
  288. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  289. $plexItem['thumb'] = (string)$item['thumb'];
  290. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  291. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  292. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  293. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  294. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  295. break;
  296. case 'album':
  297. case 'track':
  298. $plexItem['type'] = 'music';
  299. $plexItem['title'] = (string)$item['parentTitle'];
  300. $plexItem['secondaryTitle'] = (string)$item['title'];
  301. $plexItem['summary'] = (string)$item['title'];
  302. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  303. $plexItem['thumb'] = (string)$item['thumb'];
  304. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  305. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  306. $plexItem['nowPlayingKey'] = (string)$item['parentRatingKey'] . "-np";
  307. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  308. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  309. $plexItem['metadataKey'] = isset($item['grandparentRatingKey']) ? (string)$item['grandparentRatingKey'] : (string)$item['parentRatingKey'];
  310. break;
  311. default:
  312. $plexItem['type'] = 'movie';
  313. $plexItem['title'] = (string)$item['title'];
  314. $plexItem['secondaryTitle'] = (string)$item['year'];
  315. $plexItem['summary'] = (string)$item['summary'];
  316. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  317. $plexItem['thumb'] = (string)$item['thumb'];
  318. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  319. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  320. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  321. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  322. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  323. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  324. }
  325. $plexItem['originalType'] = $item['type'];
  326. $plexItem['uid'] = (string)$item['ratingKey'];
  327. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  328. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  329. $plexItem['addedAt'] = isset($item['addedAt']) ? (int)$item['addedAt'] : null;
  330. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  331. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress'] - $plexItem['watched']) : '';
  332. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision'] : '';
  333. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  334. $plexItem['session'] = (string)$item->Session['id'];
  335. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  336. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  337. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  338. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  339. $plexItem['user'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['title'] : "";
  340. $plexItem['userThumb'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['thumb'] : "";
  341. $plexItem['userAddress'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->Player['address'] : "x.x.x.x";
  342. $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'];
  343. $plexItem['nowPlayingOriginalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '$' . $this->randString();
  344. $plexItem['originalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '$' . $this->randString();
  345. $plexItem['openTab'] = $this->config['plexTabURL'] && $this->config['plexTabName'] ? true : false;
  346. $plexItem['tabName'] = $this->config['plexTabName'] ? $this->config['plexTabName'] : '';
  347. // Stream info
  348. $plexItem['userStream'] = array(
  349. 'platform' => (string)$item->Player['platform'],
  350. 'product' => (string)$item->Player['product'],
  351. 'device' => (string)$item->Player['device'],
  352. 'stream' => isset($item->Media) ? (string)$item->Media->Part['decision'] . ($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)' : '') : '',
  353. 'videoResolution' => (string)$item->Media['videoResolution'],
  354. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  355. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  356. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  357. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  358. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  359. 'videoDecision' => $this->streamType((string)$item->TranscodeSession['videoDecision']),
  360. 'audioDecision' => $this->streamType((string)$item->TranscodeSession['audioDecision']),
  361. 'container' => (string)$item->TranscodeSession['container'],
  362. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  363. );
  364. // Genre catch all
  365. if ($item->Genre) {
  366. $genres = array();
  367. foreach ($item->Genre as $key => $value) {
  368. $genres[] = (string)$value['tag'];
  369. }
  370. }
  371. // Actor catch all
  372. if ($item->Role) {
  373. $actors = array();
  374. foreach ($item->Role as $key => $value) {
  375. if ($value['thumb']) {
  376. $actors[] = array(
  377. 'name' => (string)$value['tag'],
  378. 'role' => (string)$value['role'],
  379. 'thumb' => (string)$value['thumb']
  380. );
  381. }
  382. }
  383. }
  384. // Metadata information
  385. $plexItem['metadata'] = array(
  386. 'guid' => (string)$item['guid'],
  387. 'summary' => (string)$item['summary'],
  388. 'rating' => (string)$item['rating'],
  389. 'duration' => (string)$item['duration'],
  390. 'originallyAvailableAt' => (string)$item['originallyAvailableAt'],
  391. 'year' => (string)$item['year'],
  392. 'studio' => (string)$item['studio'],
  393. 'tagline' => (string)$item['tagline'],
  394. 'genres' => ($item->Genre) ? $genres : '',
  395. 'actors' => ($item->Role) ? $actors : ''
  396. );
  397. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  398. $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb . $plexItem['nowPlayingKey'] . '.jpg';
  399. }
  400. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  401. $plexItem['imageURL'] = $cacheDirectoryWeb . $plexItem['key'] . '.jpg';
  402. }
  403. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  404. $plexItem['nowPlayingImageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '';
  405. }
  406. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['key'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  407. $plexItem['imageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '';
  408. }
  409. if (!$plexItem['nowPlayingThumb']) {
  410. $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png";
  411. $plexItem['nowPlayingKey'] = "no-np";
  412. }
  413. if (!$plexItem['thumb'] || $plexItem['addedAt'] >= (time() - 300)) {
  414. $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/cache/no-list.png";
  415. $plexItem['key'] = "no-list";
  416. }
  417. if (isset($useImage)) {
  418. $plexItem['useImage'] = $useImage;
  419. }
  420. return $plexItem;
  421. }
  422. public function getPlexHomepageStreams()
  423. {
  424. if (!$this->config['homepagePlexEnabled']) {
  425. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  426. return false;
  427. }
  428. if (!$this->config['homepagePlexStreams']) {
  429. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  430. return false;
  431. }
  432. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  433. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  434. return false;
  435. }
  436. if (!$this->qualifyRequest($this->config['homepagePlexStreamsAuth'])) {
  437. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  438. return false;
  439. }
  440. if (empty($this->config['plexURL'])) {
  441. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  442. return false;
  443. }
  444. if (empty($this->config['plexToken'])) {
  445. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  446. return false;
  447. }
  448. if (empty($this->config['plexID'])) {
  449. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  450. return false;
  451. }
  452. $ignore = array();
  453. $resolve = true;
  454. $url = $this->qualifyURL($this->config['plexURL']);
  455. $url = $url . "/status/sessions?X-Plex-Token=" . $this->config['plexToken'];
  456. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  457. $response = Requests::get($url, array(), $options);
  458. libxml_use_internal_errors(true);
  459. if ($response->success) {
  460. $items = array();
  461. $plex = simplexml_load_string($response->body);
  462. foreach ($plex as $child) {
  463. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  464. $items[] = $this->resolvePlexItem($child);
  465. }
  466. }
  467. $api['content'] = ($resolve) ? $items : $plex;
  468. $api['plexID'] = $this->config['plexID'];
  469. $api['showNames'] = true;
  470. $api['group'] = '1';
  471. $this->setAPIResponse('success', null, 200, $api);
  472. return $api;
  473. }
  474. }
  475. public function getPlexHomepageRecent()
  476. {
  477. if (!$this->config['homepagePlexEnabled']) {
  478. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  479. return false;
  480. }
  481. if (!$this->config['homepagePlexRecent']) {
  482. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  483. return false;
  484. }
  485. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  486. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  487. return false;
  488. }
  489. if (!$this->qualifyRequest($this->config['homepagePlexRecentAuth'])) {
  490. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  491. return false;
  492. }
  493. if (empty($this->config['plexURL'])) {
  494. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  495. return false;
  496. }
  497. if (empty($this->config['plexToken'])) {
  498. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  499. return false;
  500. }
  501. if (empty($this->config['plexID'])) {
  502. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  503. return false;
  504. }
  505. $ignore = array();
  506. $resolve = true;
  507. $url = $this->qualifyURL($this->config['plexURL']);
  508. $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";
  509. $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";
  510. $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";
  511. foreach ($urls as $k => $v) {
  512. $options = ($this->localURL($v)) ? array('verify' => false) : array();
  513. $response = Requests::get($v, array(), $options);
  514. libxml_use_internal_errors(true);
  515. if ($response->success) {
  516. $items = array();
  517. $plex = simplexml_load_string($response->body);
  518. foreach ($plex as $child) {
  519. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  520. $items[] = $this->resolvePlexItem($child);
  521. }
  522. }
  523. if (isset($api)) {
  524. $api['content'] = array_merge($api['content'], ($resolve) ? $items : $plex);
  525. } else {
  526. $api['content'] = ($resolve) ? $items : $plex;
  527. }
  528. }
  529. }
  530. if (isset($api['content'])) {
  531. usort($api['content'], function ($a, $b) {
  532. return $b['addedAt'] <=> $a['addedAt'];
  533. });
  534. }
  535. $api['plexID'] = $this->config['plexID'];
  536. $api['showNames'] = true;
  537. $api['group'] = '1';
  538. $this->setAPIResponse('success', null, 200, $api);
  539. return $api;
  540. }
  541. public function getPlexHomepageMetadata($array)
  542. {
  543. if (!$this->config['homepagePlexEnabled']) {
  544. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  545. return false;
  546. }
  547. if (!$this->config['homepagePlexStreams']) {
  548. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  549. return false;
  550. }
  551. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  552. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  553. return false;
  554. }
  555. if (!$this->qualifyRequest($this->config['homepagePlexStreamsAuth'])) {
  556. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  557. return false;
  558. }
  559. if (empty($this->config['plexURL'])) {
  560. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  561. return false;
  562. }
  563. if (empty($this->config['plexToken'])) {
  564. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  565. return false;
  566. }
  567. if (empty($this->config['plexID'])) {
  568. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  569. return false;
  570. }
  571. $key = $array['key'] ?? null;
  572. if (!$key) {
  573. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  574. return false;
  575. }
  576. $ignore = array();
  577. $resolve = true;
  578. $url = $this->qualifyURL($this->config['plexURL']);
  579. $url = $url . "/library/metadata/" . $key . "?X-Plex-Token=" . $this->config['plexToken'];
  580. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  581. $response = Requests::get($url, array(), $options);
  582. libxml_use_internal_errors(true);
  583. if ($response->success) {
  584. $items = array();
  585. $plex = simplexml_load_string($response->body);
  586. foreach ($plex as $child) {
  587. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  588. $items[] = $this->resolvePlexItem($child);
  589. }
  590. }
  591. $api['content'] = ($resolve) ? $items : $plex;
  592. $api['plexID'] = $this->config['plexID'];
  593. $api['showNames'] = true;
  594. $api['group'] = '1';
  595. $this->setAPIResponse('success', null, 200, $api);
  596. return $api;
  597. }
  598. }
  599. public function getPlexHomepagePlaylists()
  600. {
  601. if (!$this->config['homepagePlexEnabled']) {
  602. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  603. return false;
  604. }
  605. if (!$this->config['homepagePlexPlaylist']) {
  606. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  607. return false;
  608. }
  609. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  610. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  611. return false;
  612. }
  613. if (!$this->qualifyRequest($this->config['homepagePlexPlaylistAuth'])) {
  614. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  615. return false;
  616. }
  617. if (empty($this->config['plexURL'])) {
  618. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  619. return false;
  620. }
  621. if (empty($this->config['plexToken'])) {
  622. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  623. return false;
  624. }
  625. if (empty($this->config['plexID'])) {
  626. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  627. return false;
  628. }
  629. $url = $this->qualifyURL($this->config['plexURL']);
  630. $url = $url . "/playlists?X-Plex-Token=" . $this->config['plexToken'];
  631. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  632. $response = Requests::get($url, array(), $options);
  633. libxml_use_internal_errors(true);
  634. if ($response->success) {
  635. $items = array();
  636. $plex = simplexml_load_string($response->body);
  637. foreach ($plex as $child) {
  638. if ($child['playlistType'] == "video" && strpos(strtolower($child['title']), 'private') === false) {
  639. $playlistTitleClean = preg_replace("/(\W)+/", "", (string)$child['title']);
  640. $playlistURL = $this->qualifyURL($this->config['plexURL']);
  641. $playlistURL = $playlistURL . $child['key'] . "?X-Plex-Token=" . $this->config['plexToken'];
  642. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  643. $playlistResponse = Requests::get($playlistURL, array(), $options);
  644. if ($playlistResponse->success) {
  645. $playlistResponse = simplexml_load_string($playlistResponse->body);
  646. $items[$playlistTitleClean]['title'] = (string)$child['title'];
  647. foreach ($playlistResponse->Video as $playlistItem) {
  648. $items[$playlistTitleClean][] = $this->resolvePlexItem($playlistItem);
  649. }
  650. }
  651. }
  652. }
  653. $api['content'] = $items;
  654. $api['plexID'] = $this->config['plexID'];
  655. $api['showNames'] = true;
  656. $api['group'] = '1';
  657. $this->setAPIResponse('success', null, 200, $api);
  658. return $api;
  659. } else {
  660. $this->setAPIResponse('error', 'Plex API error', 500);
  661. return false;
  662. }
  663. }
  664. public function getPlexHomepageSearch($query)
  665. {
  666. if (!$this->config['homepagePlexEnabled']) {
  667. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  668. return false;
  669. }
  670. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  671. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  672. return false;
  673. }
  674. if (empty($this->config['plexURL'])) {
  675. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  676. return false;
  677. }
  678. if (empty($this->config['plexToken'])) {
  679. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  680. return false;
  681. }
  682. if (empty($this->config['plexID'])) {
  683. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  684. return false;
  685. }
  686. $query = $query ?? null;
  687. if (!$query) {
  688. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  689. return false;
  690. }
  691. $ignore = array('artist', 'episode');
  692. $resolve = true;
  693. $url = $this->qualifyURL($this->config['plexURL']);
  694. $url = $url . "/search?query=" . rawurlencode($query) . "&X-Plex-Token=" . $this->config['plexToken'];
  695. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  696. $response = Requests::get($url, array(), $options);
  697. libxml_use_internal_errors(true);
  698. if ($response->success) {
  699. $items = array();
  700. $plex = simplexml_load_string($response->body);
  701. foreach ($plex as $child) {
  702. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  703. $items[] = $this->resolvePlexItem($child);
  704. }
  705. }
  706. $api['content'] = ($resolve) ? $items : $plex;
  707. $api['plexID'] = $this->config['plexID'];
  708. $api['showNames'] = true;
  709. $api['group'] = '1';
  710. $this->setAPIResponse('success', null, 200, $api);
  711. return $api;
  712. }
  713. }
  714. }