plex.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. trait PlexHomepageItem
  3. {
  4. public function testConnectionPlex()
  5. {
  6. if (!empty($this->config['plexURL']) && !empty($this->config['plexToken'])) {
  7. $url = $this->qualifyURL($this->config['plexURL']) . "/?X-Plex-Token=" . $this->config['plexToken'];
  8. try {
  9. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  10. $response = Requests::get($url, array(), $options);
  11. libxml_use_internal_errors(true);
  12. if ($response->success) {
  13. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  14. return true;
  15. }
  16. } catch (Requests_Exception $e) {
  17. $this->setAPIResponse('error', $e->getMessage(), 500);
  18. return false;
  19. };
  20. } else {
  21. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  22. return 'URL and/or Token not setup';
  23. }
  24. }
  25. public function resolvePlexItem($item)
  26. {
  27. // Static Height & Width
  28. $height = $this->getCacheImageSize('h');
  29. $width = $this->getCacheImageSize('w');
  30. $nowPlayingHeight = $this->getCacheImageSize('nph');
  31. $nowPlayingWidth = $this->getCacheImageSize('npw');
  32. // Cache Directories
  33. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  34. $cacheDirectoryWeb = 'plugins/images/cache/';
  35. // Types
  36. switch ($item['type']) {
  37. case 'show':
  38. $plexItem['type'] = 'tv';
  39. $plexItem['title'] = (string)$item['title'];
  40. $plexItem['secondaryTitle'] = (string)$item['year'];
  41. $plexItem['summary'] = (string)$item['summary'];
  42. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  43. $plexItem['thumb'] = (string)$item['thumb'];
  44. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  45. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  46. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  47. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  48. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  49. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  50. break;
  51. case 'season':
  52. $plexItem['type'] = 'tv';
  53. $plexItem['title'] = (string)$item['parentTitle'];
  54. $plexItem['secondaryTitle'] = (string)$item['title'];
  55. $plexItem['summary'] = (string)$item['parentSummary'];
  56. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  57. $plexItem['thumb'] = (string)$item['thumb'];
  58. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  59. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  60. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  61. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  62. break;
  63. case 'episode':
  64. $plexItem['type'] = 'tv';
  65. $plexItem['title'] = (string)$item['grandparentTitle'];
  66. $plexItem['secondaryTitle'] = (string)$item['parentTitle'];
  67. $plexItem['summary'] = (string)$item['title'];
  68. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  69. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  70. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  71. $plexItem['nowPlayingThumb'] = (string)$item['grandparentArt'];
  72. $plexItem['nowPlayingKey'] = (string)$item['grandparentRatingKey'] . "-np";
  73. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  74. $plexItem['nowPlayingBottom'] = 'S' . (string)$item['parentIndex'] . ' · E' . (string)$item['index'];
  75. $plexItem['metadataKey'] = (string)$item['grandparentRatingKey'];
  76. break;
  77. case 'clip':
  78. $useImage = (isset($item['live']) ? "plugins/images/cache/livetv.png" : null);
  79. $plexItem['type'] = 'clip';
  80. $plexItem['title'] = (isset($item['live']) ? 'Live TV' : (string)$item['title']);
  81. $plexItem['secondaryTitle'] = '';
  82. $plexItem['summary'] = (string)$item['summary'];
  83. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  84. $plexItem['thumb'] = (string)$item['thumb'];
  85. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  86. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  87. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  88. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  89. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  90. break;
  91. case 'album':
  92. case 'track':
  93. $plexItem['type'] = 'music';
  94. $plexItem['title'] = (string)$item['parentTitle'];
  95. $plexItem['secondaryTitle'] = (string)$item['title'];
  96. $plexItem['summary'] = (string)$item['title'];
  97. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  98. $plexItem['thumb'] = (string)$item['thumb'];
  99. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  100. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  101. $plexItem['nowPlayingKey'] = (string)$item['parentRatingKey'] . "-np";
  102. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  103. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  104. $plexItem['metadataKey'] = isset($item['grandparentRatingKey']) ? (string)$item['grandparentRatingKey'] : (string)$item['parentRatingKey'];
  105. break;
  106. default:
  107. $plexItem['type'] = 'movie';
  108. $plexItem['title'] = (string)$item['title'];
  109. $plexItem['secondaryTitle'] = (string)$item['year'];
  110. $plexItem['summary'] = (string)$item['summary'];
  111. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  112. $plexItem['thumb'] = (string)$item['thumb'];
  113. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  114. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  115. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  116. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  117. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  118. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  119. }
  120. $plexItem['originalType'] = $item['type'];
  121. $plexItem['uid'] = (string)$item['ratingKey'];
  122. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  123. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  124. $plexItem['addedAt'] = isset($item['addedAt']) ? (int)$item['addedAt'] : null;
  125. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  126. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress'] - $plexItem['watched']) : '';
  127. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision'] : '';
  128. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  129. $plexItem['session'] = (string)$item->Session['id'];
  130. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  131. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  132. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  133. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  134. $plexItem['user'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['title'] : "";
  135. $plexItem['userThumb'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['thumb'] : "";
  136. $plexItem['userAddress'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->Player['address'] : "x.x.x.x";
  137. $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'];
  138. $plexItem['nowPlayingOriginalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '$' . $this->randString();
  139. $plexItem['originalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '$' . $this->randString();
  140. $plexItem['openTab'] = $this->config['plexTabURL'] && $this->config['plexTabName'] ? true : false;
  141. $plexItem['tabName'] = $this->config['plexTabName'] ? $this->config['plexTabName'] : '';
  142. // Stream info
  143. $plexItem['userStream'] = array(
  144. 'platform' => (string)$item->Player['platform'],
  145. 'product' => (string)$item->Player['product'],
  146. 'device' => (string)$item->Player['device'],
  147. 'stream' => isset($item->Media) ? (string)$item->Media->Part['decision'] . ($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)' : '') : '',
  148. 'videoResolution' => (string)$item->Media['videoResolution'],
  149. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  150. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  151. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  152. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  153. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  154. 'videoDecision' => $this->streamType((string)$item->TranscodeSession['videoDecision']),
  155. 'audioDecision' => $this->streamType((string)$item->TranscodeSession['audioDecision']),
  156. 'container' => (string)$item->TranscodeSession['container'],
  157. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  158. );
  159. // Genre catch all
  160. if ($item->Genre) {
  161. $genres = array();
  162. foreach ($item->Genre as $key => $value) {
  163. $genres[] = (string)$value['tag'];
  164. }
  165. }
  166. // Actor catch all
  167. if ($item->Role) {
  168. $actors = array();
  169. foreach ($item->Role as $key => $value) {
  170. if ($value['thumb']) {
  171. $actors[] = array(
  172. 'name' => (string)$value['tag'],
  173. 'role' => (string)$value['role'],
  174. 'thumb' => (string)$value['thumb']
  175. );
  176. }
  177. }
  178. }
  179. // Metadata information
  180. $plexItem['metadata'] = array(
  181. 'guid' => (string)$item['guid'],
  182. 'summary' => (string)$item['summary'],
  183. 'rating' => (string)$item['rating'],
  184. 'duration' => (string)$item['duration'],
  185. 'originallyAvailableAt' => (string)$item['originallyAvailableAt'],
  186. 'year' => (string)$item['year'],
  187. 'studio' => (string)$item['studio'],
  188. 'tagline' => (string)$item['tagline'],
  189. 'genres' => ($item->Genre) ? $genres : '',
  190. 'actors' => ($item->Role) ? $actors : ''
  191. );
  192. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  193. $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb . $plexItem['nowPlayingKey'] . '.jpg';
  194. }
  195. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  196. $plexItem['imageURL'] = $cacheDirectoryWeb . $plexItem['key'] . '.jpg';
  197. }
  198. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  199. $plexItem['nowPlayingImageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '';
  200. }
  201. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['key'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  202. $plexItem['imageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '';
  203. }
  204. if (!$plexItem['nowPlayingThumb']) {
  205. $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png";
  206. $plexItem['nowPlayingKey'] = "no-np";
  207. }
  208. if (!$plexItem['thumb'] || $plexItem['addedAt'] >= (time() - 300)) {
  209. $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/cache/no-list.png";
  210. $plexItem['key'] = "no-list";
  211. }
  212. if (isset($useImage)) {
  213. $plexItem['useImage'] = $useImage;
  214. }
  215. return $plexItem;
  216. }
  217. public function getPlexHomepageStreams()
  218. {
  219. if (!$this->config['homepagePlexEnabled']) {
  220. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  221. return false;
  222. }
  223. if (!$this->config['homepagePlexStreams']) {
  224. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  225. return false;
  226. }
  227. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  228. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  229. return false;
  230. }
  231. if (!$this->qualifyRequest($this->config['homepagePlexStreamsAuth'])) {
  232. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  233. return false;
  234. }
  235. if (empty($this->config['plexURL'])) {
  236. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  237. return false;
  238. }
  239. if (empty($this->config['plexToken'])) {
  240. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  241. return false;
  242. }
  243. if (empty($this->config['plexID'])) {
  244. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  245. return false;
  246. }
  247. $ignore = array();
  248. $resolve = true;
  249. $url = $this->qualifyURL($this->config['plexURL']);
  250. $url = $url . "/status/sessions?X-Plex-Token=" . $this->config['plexToken'];
  251. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  252. $response = Requests::get($url, array(), $options);
  253. libxml_use_internal_errors(true);
  254. if ($response->success) {
  255. $items = array();
  256. $plex = simplexml_load_string($response->body);
  257. foreach ($plex as $child) {
  258. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  259. $items[] = $this->resolvePlexItem($child);
  260. }
  261. }
  262. $api['content'] = ($resolve) ? $items : $plex;
  263. $api['plexID'] = $this->config['plexID'];
  264. $api['showNames'] = true;
  265. $api['group'] = '1';
  266. $this->setAPIResponse('success', null, 200, $api);
  267. return $api;
  268. }
  269. }
  270. public function getPlexHomepageRecent()
  271. {
  272. if (!$this->config['homepagePlexEnabled']) {
  273. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  274. return false;
  275. }
  276. if (!$this->config['homepagePlexRecent']) {
  277. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  278. return false;
  279. }
  280. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  281. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  282. return false;
  283. }
  284. if (!$this->qualifyRequest($this->config['homepagePlexRecentAuth'])) {
  285. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  286. return false;
  287. }
  288. if (empty($this->config['plexURL'])) {
  289. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  290. return false;
  291. }
  292. if (empty($this->config['plexToken'])) {
  293. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  294. return false;
  295. }
  296. if (empty($this->config['plexID'])) {
  297. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  298. return false;
  299. }
  300. $ignore = array();
  301. $resolve = true;
  302. $url = $this->qualifyURL($this->config['plexURL']);
  303. $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";
  304. $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";
  305. $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";
  306. foreach ($urls as $k => $v) {
  307. $options = ($this->localURL($v)) ? array('verify' => false) : array();
  308. $response = Requests::get($v, array(), $options);
  309. libxml_use_internal_errors(true);
  310. if ($response->success) {
  311. $items = array();
  312. $plex = simplexml_load_string($response->body);
  313. foreach ($plex as $child) {
  314. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  315. $items[] = $this->resolvePlexItem($child);
  316. }
  317. }
  318. if (isset($api)) {
  319. $api['content'] = array_merge($api['content'], ($resolve) ? $items : $plex);
  320. } else {
  321. $api['content'] = ($resolve) ? $items : $plex;
  322. }
  323. }
  324. }
  325. if (isset($api['content'])) {
  326. usort($api['content'], function ($a, $b) {
  327. return $b['addedAt'] <=> $a['addedAt'];
  328. });
  329. }
  330. $api['plexID'] = $this->config['plexID'];
  331. $api['showNames'] = true;
  332. $api['group'] = '1';
  333. $this->setAPIResponse('success', null, 200, $api);
  334. return $api;
  335. }
  336. public function getPlexHomepageMetadata($array)
  337. {
  338. if (!$this->config['homepagePlexEnabled']) {
  339. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  340. return false;
  341. }
  342. if (!$this->config['homepagePlexStreams']) {
  343. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  344. return false;
  345. }
  346. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  347. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  348. return false;
  349. }
  350. if (!$this->qualifyRequest($this->config['homepagePlexStreamsAuth'])) {
  351. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  352. return false;
  353. }
  354. if (empty($this->config['plexURL'])) {
  355. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  356. return false;
  357. }
  358. if (empty($this->config['plexToken'])) {
  359. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  360. return false;
  361. }
  362. if (empty($this->config['plexID'])) {
  363. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  364. return false;
  365. }
  366. $key = $array['key'] ?? null;
  367. if (!$key) {
  368. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  369. return false;
  370. }
  371. $ignore = array();
  372. $resolve = true;
  373. $url = $this->qualifyURL($this->config['plexURL']);
  374. $url = $url . "/library/metadata/" . $key . "?X-Plex-Token=" . $this->config['plexToken'];
  375. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  376. $response = Requests::get($url, array(), $options);
  377. libxml_use_internal_errors(true);
  378. if ($response->success) {
  379. $items = array();
  380. $plex = simplexml_load_string($response->body);
  381. foreach ($plex as $child) {
  382. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  383. $items[] = $this->resolvePlexItem($child);
  384. }
  385. }
  386. $api['content'] = ($resolve) ? $items : $plex;
  387. $api['plexID'] = $this->config['plexID'];
  388. $api['showNames'] = true;
  389. $api['group'] = '1';
  390. $this->setAPIResponse('success', null, 200, $api);
  391. return $api;
  392. }
  393. }
  394. public function getPlexHomepagePlaylists()
  395. {
  396. if (!$this->config['homepagePlexEnabled']) {
  397. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  398. return false;
  399. }
  400. if (!$this->config['homepagePlexPlaylist']) {
  401. $this->setAPIResponse('error', 'Plex homepage module is not enabled', 409);
  402. return false;
  403. }
  404. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  405. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  406. return false;
  407. }
  408. if (!$this->qualifyRequest($this->config['homepagePlexPlaylistAuth'])) {
  409. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  410. return false;
  411. }
  412. if (empty($this->config['plexURL'])) {
  413. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  414. return false;
  415. }
  416. if (empty($this->config['plexToken'])) {
  417. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  418. return false;
  419. }
  420. if (empty($this->config['plexID'])) {
  421. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  422. return false;
  423. }
  424. $url = $this->qualifyURL($this->config['plexURL']);
  425. $url = $url . "/playlists?X-Plex-Token=" . $this->config['plexToken'];
  426. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  427. $response = Requests::get($url, array(), $options);
  428. libxml_use_internal_errors(true);
  429. if ($response->success) {
  430. $items = array();
  431. $plex = simplexml_load_string($response->body);
  432. foreach ($plex as $child) {
  433. if ($child['playlistType'] == "video" && strpos(strtolower($child['title']), 'private') === false) {
  434. $playlistTitleClean = preg_replace("/(\W)+/", "", (string)$child['title']);
  435. $playlistURL = $this->qualifyURL($this->config['plexURL']);
  436. $playlistURL = $playlistURL . $child['key'] . "?X-Plex-Token=" . $this->config['plexToken'];
  437. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  438. $playlistResponse = Requests::get($playlistURL, array(), $options);
  439. if ($playlistResponse->success) {
  440. $playlistResponse = simplexml_load_string($playlistResponse->body);
  441. $items[$playlistTitleClean]['title'] = (string)$child['title'];
  442. foreach ($playlistResponse->Video as $playlistItem) {
  443. $items[$playlistTitleClean][] = $this->resolvePlexItem($playlistItem);
  444. }
  445. }
  446. }
  447. }
  448. $api['content'] = $items;
  449. $api['plexID'] = $this->config['plexID'];
  450. $api['showNames'] = true;
  451. $api['group'] = '1';
  452. $this->setAPIResponse('success', null, 200, $api);
  453. return $api;
  454. } else {
  455. $this->setAPIResponse('error', 'Plex API error', 500);
  456. return false;
  457. }
  458. }
  459. public function getPlexHomepageSearch($query)
  460. {
  461. if (!$this->config['homepagePlexEnabled']) {
  462. $this->setAPIResponse('error', 'Plex homepage item is not enabled', 409);
  463. return false;
  464. }
  465. if (!$this->qualifyRequest($this->config['homepagePlexAuth'])) {
  466. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  467. return false;
  468. }
  469. if (empty($this->config['plexURL'])) {
  470. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  471. return false;
  472. }
  473. if (empty($this->config['plexToken'])) {
  474. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  475. return false;
  476. }
  477. if (empty($this->config['plexID'])) {
  478. $this->setAPIResponse('error', 'Plex Id is not defined', 422);
  479. return false;
  480. }
  481. $query = $query ?? null;
  482. if (!$query) {
  483. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  484. return false;
  485. }
  486. $ignore = array('artist', 'episode');
  487. $resolve = true;
  488. $url = $this->qualifyURL($this->config['plexURL']);
  489. $url = $url . "/search?query=" . rawurlencode($query) . "&X-Plex-Token=" . $this->config['plexToken'];
  490. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  491. $response = Requests::get($url, array(), $options);
  492. libxml_use_internal_errors(true);
  493. if ($response->success) {
  494. $items = array();
  495. $plex = simplexml_load_string($response->body);
  496. foreach ($plex as $child) {
  497. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  498. $items[] = $this->resolvePlexItem($child);
  499. }
  500. }
  501. $api['content'] = ($resolve) ? $items : $plex;
  502. $api['plexID'] = $this->config['plexID'];
  503. $api['showNames'] = true;
  504. $api['group'] = '1';
  505. $this->setAPIResponse('success', null, 200, $api);
  506. return $api;
  507. }
  508. }
  509. }